Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation of some fields
  • How to do a background check only some of the fields in the form?

    for example, there is in the admin panel, a few fields, username, email, password

    But if I filled out the field password, then you need to check this field, and if the field is not filled, then no need to update the data in the database

    how to do it?

  • You could write your validation rules with conditional statements:


    if( $_POST['form_password'] )
    {
    $val->add_field('password', 'Password', 'min_length[8]|max_length[64]');
    }
  • Assuming you're used hand-coded forms, and only use a validation object, you can do it like this. But the if isn't really needed, as long as you don't put a required rule on it, it's ok if the value isn't there.
  • here is my html form ....

    <div class="control-group">

    <?=\Form::label('Email', 'email', array('class' => 'control-label'))?>
    <div class="controls">
    <?=\Form::input('email', $user->email, array('required' => 'required'))?>
    </div>
    </div>

    <div class="control-group">
      <?=\Form::label('New password', 'password', array('class' => 'control-label'))?>
    <div class="controls">
    <?=\Form::input('password', null)?>
    </div>

    </div>

    users controller

    $val = Model_User::validate('edit');

    if (\Input::method() == 'POST')
    {
    if ($user->email == \Input::post('email'))
    {
    $val->field('email')->delete_rule('unique');
    }




    if ($val->run(null, true))
      {  ....

    I know that you can do something like so

     if ($val->run(array('password' => null), true))
      {  ....

    but if a lot of fields, may have some ability to disable the audited field

  • HarroHarro
    Accepted Answer
    You can just add the field to the validation object. As long as you don't add the rule "required", it will pass if the value isn't posted.

    If you have more complex forms, it's worth looking at fieldsets, which allows you to generate the form, and do built-in validation.

Howdy, Stranger!

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

In this Discussion