Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to handle fieldset, multiple models and validations
  • Hi, I am new developing fuelPHP and I have 4 questions about it:
    1. I am building a form which uses 2 models, I do this:



      $fieldset = Fieldset::forge('register_form');
      $fieldset->add_model('Model_Ntk_User');
      $fieldset->add_model('Model_Ntk_Users_Social_Network');


      But some fields has same name, how can I solve that?. I found a method set_form_fields, but not much info about it.

    2. In Yii, in example you can build a model using CActiveRecord (if
      linked to a table) or CFormModel to build Contact forms in example, how
      this is done with fuelPHP, model_crud?

    3. If I use html form (not build using fieldset) how can I use
      validation without adding the validation rules in the controller, since
      they are already set on $_properties in the model?

    4. I have created an Observer which adds values to fields like
      ip_added and date_added, but first is triggered the Observers: Typing
      and Validation, since those fields are set to "not null" Typing observer
      throw an exception. So far I have fix it making those fields "null" but
      I think that is bad idea.

  • HarroHarro
    Accepted Answer
    1. The problem here is multiple columns with the same name.

    This is an issue that can't be avoided when using add_model. The easiest way out is to use two fieldsets, one for each model, and send them both to the view for form generation.

    An alternative might be, like you say, use the set_form_fields method, which contains the logic behind add_model(). The default logic is in the \Orm\Model::set_form_fields() method, so you can use that as the start of your custom code. For example, you can prefix the generated fields with the model name to make them unique.

    2. I don't know Yii, what do you mean by "build a model"?

    3. You can't without a fieldset.

    You can get the validation object from a fieldset using the validation() method of the fieldset object. So you could generate the fieldset as you do, but not use it for form generation, and only to get the generated validation object.

    Note that if you have more complex forms, you can still use a fieldset, as you can render individual fields of the fieldset seperately. So where you would use an <input> tag, you could render an individual fieldset form field.

    4. Observers are triggered in the order in which they are defined.

    So if you need a specific order of execution, make sure you define them in that order.
  • Hi Harro,

    Thanks for your answers.

Howdy, Stranger!

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

In this Discussion