Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to make Edit form
  • Hello!
    One more question from me. How can I output data from DB in form fields?
    I gues, it's simple, but I don't know what class/method should I look.
  • oh! It seems that I know how to do it. I should djust read fields and output as a value
  • Are you using ORM?
  • Then you can also use a Fieldset to generate Crud forums, using the models property definitions.
  • Can you give me a simple example how to use Fieldset to fill fields from the database.

    For example, we have in database in table "contacts" field "skype". In the field, where id="1" skype="myskype".
    When I want to edit skype I open url /contacts/edit and see old value "myskype". Edit skype to "mynewskype" and push button Save.

    Please, help me to write controller and view for this case.
  • Use oil to geneate an scaffold example, see http://docs.fuelphp.com/packages/oil/generate.html#scaffolding

    Then you'll have all components (controller, ORM model, migration and all crud views). The code generated is not the best, but it gives you a good example to work with.
  • Thank you! This is realy good advice!
    I saw this example. But I have a question.
    In example public static function validate($factory) uses for validation.
    I've written $_properties array where I set validation.

    How can I make validation in my situation?
    In example (separate validate function):

    $val = Model_Monkey::validate('edit');
    if ($val->run())
    {
        ...
    }
  • Yeah, I know. It's done this way because Oil generated code need to support both ORM and Model_Crud. And only ORM has validation built-in.

    If your using ORM, I would go for the validation as part of $_properties, use the validation feature from the Fieldset, and not use this validate() method:

    $fieldset = \Fieldset::forge('create_monkey');
    $fieldset->form()->add_csrf();
    $fieldset->add_model('Model_Monkey');

    if (Input::post()
    {
        $fieldset->validation()->run();

        if ( ! $form->validation()->error())
        {
            // save the data
        }
        else
        {
            // do something with the errors
        }
    }

    ...
  • Is your example good for using this way to set fields in the wiev

    <?php echo Form::input('skype', $advert->skype); ?>


  • With a fieldset, you can either generate the entire form in one go using

    echo $fieldset->build();

    in the form (you need to pass the fieldset object to the view).

    You can also generate the form a field at the time, using

    echo $fieldset->field('fieldname')->build();

    You can also create the fieldset manually, without using add_model();

    $fieldset = Fieldset::forge('newform');
    $fieldset->add('gender', __('newform.gender'), array('type' => 'select', 'options' => __('global.gender')))->add_rule('required');
  • It is not clear for me how does it work... OK, let's try to deal step by step.

    <?php echo $fieldset->field('content')->build(); ?>
    How can I add to this construction output of the field content. (Because this is edit form :) - something like this
    <?php echo Form::textarea('content', $advert->content); ?>
    )
  • Building a fieldset generates the input field, the label, the default value, and everything around it, so it completely replaces manual form building using the Form class.

    A snippet from one of my controllers: http://www.snipr.it/~Dr
  • Thank you very much!

    Now I have a question, how to set checkbox checked or unchecked (If I type as was shown in example

    'value' => 'true'

    doesn't work. If I write 'checked' in this array - checkboks become checked.)


    I've made additional function in Model_Advert
     public static function getcheckboxstate($advert_id, $optionname)
        {
            $advert = Model_Advert::find($advert_id);
            if ($advert->$optionname == 1)
            {
                return 'checked';
            }
        }
    It works, but I don't know does it make query to DB every time to get the field value?

    (I have in controller        

     $advert = Model_Advert::find($advert_id);

    and the same string in Model. I gues it is not good... But I have no idea how to make "checked" another way.

    Is my question clear? If not - Just show me the way how to set checkbox "checked" if in database field value 1.
  • i've checked the code in one of our apps, but we don't do anything special.

    If you define an ORM property as a checkbox, the Fieldset will automatically check it if the field has a non-zero value.
  • OK... Lets try to do smaller steps ))))

    This is my property (from $_properties array) for checkbox:

     'check' => array(
                'form' => array('type' => 'checkbox',
                    'label' => 'MyCheckbox')
            ),

    Is it correct?

    If yes, I cant find in your example where should use

     $advert = Model_Advert::find($advert_id);

    this construction? I gues that it should be required.
    Or how can I show another way which row should I find in my table?
  • Yes, that looks fine.

    Create your fieldset like this:

    $advert = Model_Advert::find($advert_id);
    $fieldset = Fieldset::forge('advert_form')->add_model('Model_Advert')->populate($advert);

    and in your view:

    echo $fieldset->build();
  • Thank's. If I do this way every field is filled but checkboxes are empty :(
  • I think i've got the reason
    http://fuelphp.com/forums/discussion/8807/checkboxes-and-orm

    'options' => array(1 => 'accept') - this string gives what I want.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion