Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
set message within callable validation method
  • i have created this function to return validation object in orm model:

    public static function get_validation(\Validation $val)
        {
            $val->add_callable('\Model_Tgpardakht');
            $val->add_field('user_id', 'User ID', 'required');
            $val->add_field('shserial', 'Shomare Serial', 'required|min_length[5]|max_length[20]|is_upper');
            return $val;
        }

    also have this method in same model:

         public static function _validation_is_upper($value)
        {
            return $value === strtoupper($value);
        }

    how i can set an error message for this method (inside _validation_in_upper function) if it returns false?
  • sorry , it is documented ,
    i need just add one line code to _validation_is_upper,

         public static function _validation_is_upper($val)
        {
            validation::active()->set_message('is_upper','bluh bluh');
            return $val === strtoupper($val);
        }

    and in controller i can use this for getting error messages:

            foreach($val->error() as $field => $error)
            {
                echo $error->get_message().'<br/>';
            }
  • One nice alternative way to do this is to copy fuel/core/lang/en/validation.php to fuel/app/lang/en/validation.php and then add your custom rule message to that. This way you can set your custom message in other languages, should you need to do so.
    For example:

    fuel/app/classes/passwordvalidation.php
    <?php
    class PasswordValidation
    {
       public static function _validation_password($val)
       {
         return \Validation::_empty($val) || preg_match(\Config::get('regex.password'), $val) > 0;
       }

       public static function _validation_password_is_changed($val, $old_val)
       {
         return !($val === Validation::active()->input($old_val));
       }
    }

    fuel/app/lang/en/validation.php
    <?php
    return array(
       // Overrides for standard validator messages
       'required' => ':label must be completed.',
      ...
      // Custom validation rules
      'password' => ':label must contain at least one letter, at least one capital letter and at least one number.',
      'password_is_changed' => ':label must be different from old password.'
    );
  • We do the same. set_message() is more for one-time custom messages or messages you want to set from your custom validation rules which can't be in a global file, for example in case of module code.

Howdy, Stranger!

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

In this Discussion