Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation in Controller_Template and Controller_Api
  • Hi All, So I've got a rather large Validation which I would like to share between two controllers. What would you do for this? Would you put it in the Model? Rob
  • I put all model related validation into my models.
  • How where would you put your statements that set the values? a la $item->title = $val->validated('title'); Would you put those in the model too?? At the moment I have something like $val = \Model_News::validator(); if($val->run()){ ...etc in the controller...
  • Most of the time I use Orm models with the Observer_Validation, check out the examples in the docs.
    If you want to use the validation without the observer I'd just run validation based on the Model and when validation succeeded use $val->validated() in the same way as I just demonstrated Input::post(). When the $_properties setting is fully configured you can add all the fields with their configured rules using $val->add_model('Model_Example'). If you're not using the Orm you can still use add_model() but you need to give your model a "set_form_fields()" method that takes the Fieldset instance as an arguement and sets all the model's fields upon it:
    public function set_form_fields($fieldset, $instance = null)
    {
        $fieldset->add('name', 'Name')->set_type('text')->add_rule('required');
        // etc... for all the other fields
        // optionally pass an array or model instance as the second param to use for initial values:
        $fieldset->add('email', 'Email')->set_type('text')->set_value($instance->email);
    }
    

    Check the code to see how add_model() works.

Howdy, Stranger!

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

In this Discussion