Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
set_message for specific field?
  • Okay, I'm trying to validate the username and I have a match_pattern rule on it to only allow letters and numbers. But I"m confused on how to set the error message if they have spaces or underscores or whatever.
    I know it would be like $val->set_message('match_pattern', 'pattern not matched').. but how do I specifically say 'Username must be letters and numbers only'?? Because what if there is another field where I'm only allowing letters and underscores? I need to set_message for a SPECIFIC field.

    This is my code so far:
    //matching fields
            $form->field('password')->add_rule('match_value', $form->field('password2')->get_attribute('value'));
            $form->field('pin')->add_rule('match_value', $form->field('pin2')->get_attribute('value'));
            $form->field('email')->add_rule('match_value', $form->field('email2')->get_attribute('value'));
            $form->field('username')->add_rule('match_pattern', '/(A-Za-z0-9]+/');
            //validate form
            $val = $form->validation();
            //set error messages
            $val->set_message('required', 'The field :field is required');
            $val->set_message('valid_email', 'The field :field must be a valid email');
            $val->set_message('match_value', 'The passwords do not match');
            $val->set_message('min_length', 'The field :field is too short in length.');
            $val->set_message('max_length', 'The field :field is too long in length.');
            $val->set_message('match_pattern', 'The field :field is invalid');
            if($val->run())
  • HarroHarro
    Accepted Answer
    That is currently not possible, messages are linked to rules, not to field names. Adding this feature is on the roadmap, but probably not before 2.0.

    As a workaround you can create a custom validation rule method, and set the message in there.

Howdy, Stranger!

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

In this Discussion