Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Populating Fieldset when fetching the model - not working?
  • Edit: Sorted. I believe it is a documentation error. I'm just wondering if I am doing this right, or just misunderstanding the documentation. I have an ORM model object, and am creating a form from it. In the controller:
    $DefaultValues = array('Field1' => 'Value1', 'Field2' => 'Value2');
    
    $fieldset = Fieldset::forge('FieldsetName')
        ->add_model('MyModel', $DefaultValues);
    
    $form = $fieldset->form();
    

    Then in my view render it like this:
    <?php echo $Form->build(); ?>
    

    Now, that works to a point. What doesn't happen is that the form is not populated with the DefaultValues, when I think it should be. I can work around this by adding this in the controller, before extracting the form from the fieldset:
    $fieldset->populate($DefaultValues);
    

    I know it is a tiny thing, and the workaround gives me a way forward, but I'm curious as to whether that second parameter to add_model() is working?
  • Is the model an ORM model or not? Don't know why it's not working, but I'd specify the default values in the model, not in your controller. If you want to populate the form with values from a database (editing an entity) you use populate().
  • Yes, it is an ORM model, initially generated from a database table using an oil plug-in (an "event" ) Perhaps I used the wrong word: yes, there are *default* values in the model, such as "" and 0. However, it is the *initial* values that I suppose I am talking about. These come from a third-party CRM over small SOAP-based library I have written, wrapped into a FuelPHP package. The controller coordinates all this stuff (the model has no knowledge of the CRM, and the CRM library has no knowledge of the model - the controller provides the glue). Anyway - sounds it should work as I originally described, but like all things FuelPHP, there are many ways to the same result, so I'll just chain the actions together (create the fieldset first, then load its initial values). It could be that the documentation is wrong, rather than the fieldset class. The documentation says it should work like that, but I cannot find one single code example to show that being done, i.e. the fieldset being created from the model and populated in one call. Anyway - I'll leave it there, and carry on. Thanks.

    Edit: this is what I used to generate the initial model: https://github.com/SicoAnimal/fuel-model-generator The model it generates is quite verbose, but it is a great starting point to work from.
  • Maybe a FuelPHP developer can make this clear.
    JasonJudge wrote on Monday 28th of May 2012:
    Edit: this is what I used to generate the initial model: https://github.com/SicoAnimal/fuel-model-generator The model it generates is quite verbose, but it is a great starting point to work from.

    Yeah that's a great tool, I use it too.
  • add_model() doesn't accept an array of default values, I don't know where you got it from. It does have the option to define a model method (by default called 'set_form_fields') which allow you to do all kinds of custom stuff with the fieldset.
  • Harro Verton wrote on Monday 28th of May 2012:
    add_model() doesn't accept an array of default values, I don't know where you got it from.

    Looks like the wording of the documentation was a bit ambigous: http://docs.fuelphp.com/classes/fieldset.html The $instance parameter (the second parameter) has this description: "Array or object that has the exactly same named properties to populate the fields. (Takes the field names from the model and fetches the remaining parameters from this array/object." I'm guessing it doesn't really take the properties from the array or object passed in? The third parameter could be useful, as I do need to pull multiple different fieldsets out of a single model, so a number of custom functions to spit out the relevant fields would be good.

Howdy, Stranger!

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

In this Discussion