Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Incomprehensible work validation class
  • Hello, please, explain why it is not clear working class automatic form generation and validation

    For example I have a users table 50 fields, the model user, I fully described all the validation rules

     <?phpclass Model_User extends \Orm\Model
    {
    protected static $_properties = array(
      'id','username' => array(
    'data_type' => 'string',
    'validation' => array('required', 'trim', 'min_length'=>array(3), 'max_length'=>array(30)),
    'label' => array('label'=>'Username', 'class'=>'control-label'),
    ),
    'password' => array(
    'data_type' => 'string',
    'validation' => array('required', 'trim', 'min_length'=>array(6), 'max_length'=>array(30)),
    'label' => array('label'=>'Password', 'class'=>'control-label'),
      ), .....?>

    Now I can create a form with the required I immediately checks validation class

    <?php

    $user = Model_User::forge();
    $form->form()->add_model($user)->repopulate();

    ....

    ?>

    but there is one problem you will see all the fields that are in the table, and if I need to do some optional fields, username, password, email

    You can display a field in a class method fieldset add

    <?php
      $fieldset = Fieldset::forge()->repopulate();

    $form = $fieldset->form()->set_attribute('class', 'form-horizontal');

      $form->add('username, 'Username'......')?>

    but there is another problem why the need to re-describe the validation rules for the fields, although they have been described in the user model

    may have some other way to allow you to selectively display the required fields and validation rules that were taken from the model?

  • HarroHarro
    Accepted Answer
    To start with your last question: you don't. All settings defined on the property are used in constructing the fieldset, including all validation rules. If that doesn't work for you, you're doing something wrong.

    If you want to exclude specific properties from the fieldset, you can set the 'type' (not 'data_type'!) to false. If you want to generate more complex forms, and you can't do that by defining a custom layout. you can create the form manually:

    echo Form::open();
    echo $fieldset->field('some_field')->build();
    echo Form::close();

    Add you can use the add() methods of the fieldset to add additional fields to a generated fieldset, that is not a problem.
  • I am doing right?
    <?php<br />echo Form::open(array('class' => 'form-horizontal'));

    $fieldset = Fieldset::forge()->add_model('Model_User')->repopulate();

    echo $fieldset->field('username')->build();
    echo $fieldset->field('password')->build();

    echo Form::button('submit', 'Sign in', array('type' => 'submit', 'class' => 'btn btn-primary'));

    echo Form::close();
    ?>
  • HarroHarro
    Accepted Answer
    That should work.

    You will have to think about how to handle validation, since the model has rules for all fields, but not all field values are posted. So it could be that validation will fail on that.

    If it's only these two fields, you might be better of constructing the fieldset manually in your controller, add the two fields and their validation rules, and use that.
  • Thank you very much helped me

Howdy, Stranger!

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

In this Discussion