Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Generate form from validation rules
  • Hi, let's say I want a form with a "brand" and "type" textfield. I've added these validation rules in the controller:
    $val = Validation::forge();
    $val->add('brand', 'The brand')->add_rule('required');
    $val->add('type', 'The type')->add_rule('required');
    
    I know I can generate a form with the Fieldset class, but I don't want to use that. If possible, I would like to be able to loop through the added fields in the Validation class (in this case brand and type) and create the textfields, something like (in a view):
    foreach($fields as $key => $value)
    {
    echo "{$value}: <input type='text' name='{$key}' value='".Input::post($key)."' /><br />";
    }
    
    Gives:
    The brand: <input type='text' name='brand' value='...' /><br />
    The type: <input type='text' name='type' value='...' /><br />
    
    The fields are saved in $val->fields but that's a protected property so I can't access it in my view.
  • I couldn't find it in the docs, is there a similar method for adding select's and radios with their options in the validation class? e.g. $val->add('selectfield', 'The selectfield', array('option' => 'Option1'))->set_type('select')->add_rule('required');
  • Got it:
    foreach($val->field() as $field) {
        var_dump($field->name);
    }
    
  • Answering my own question here. To get a selet inside the validation object with it's options for building a form: $val->add('selectfield', 'The Selectfield'))->set_type('select')->set_options(array('option' => 'option name')->add_rule('required');
  • Note that the Validation class uses the Fieldset class to model the fields it has to validate. This means you can also set field types and other attributes while generating the fields on the Validation instance. For example you can do this:
    $val->add('brand', 'The brand')->set_type('text')->add_rule('required');
    

    Now you can generate the field using just this code in your view (provided the validation instance is referenced in $val).
    echo $val->field('brand');
    

    You can also do $val->fieldset()->repopulate() after a failed validation to have all the fields repopulated with the posted values.
  • Thanks Jelmer, exactly what I was looking for (setting the form element type in the validation class). Very useful.
  • I have still one question. If I use:
    echo $val->field('brand');
    

    I get a complete table row with label and textfield etc. But I only want the code for the form element, because I don't use tables. Is that possible? Off course I can use the functions from the Form class in the loop but I hope there is an easier way to do this.
  • You can create your own form.php config file by copying it from the core into your app/config folder and changing the default layout. A template can also be set upon specific fieldset instances and upon a field:
    $fieldset = $val->fieldset();
    
    // change a template for a specific fieldset instance:
    $fieldset->set_config('field_template', '{field}');
    
    // change a template for a specific field:
    $fieldset->field('brand')->set_template('<p>{label}<br />{field}</p>');
    
  • Really nice! Thanks~

Howdy, Stranger!

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

In this Discussion