Tutorial :Can Kohana 3's validation errors inherit?



Question:

I've created a bunch of errors in a file under APPPATH/messages/validate.php with a bunch of common messages such as...

return array(      'not_empty'    => ':field must not be empty.',      'matches'      => ':field must be the same as :param1',      'regex'        => ':field does not match the required format',      'exact_length' => ':field must be exactly :param1 characters long',      'min_length'   => ':field must be at least :param1 characters long',      'max_length'   => ':field must be less than :param1 characters long',      'in_array'     => ':field must be one of the available options',      'digit'        => ':field must be a digit',      'email'        => 'You must enter a valid email.',      'name'         => 'You must enter your name.',      'enquiry'      => 'You must enter your enquiry.',      'captcha' => array (          'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',          'not_empty' => 'You must enter the characters from the image.'      )   );  

This works great when I get errors like $errors = $post->errors('validate').

Is there a way to use these errors as base errors, and if I have a separate form which needs more, I can use a separate file with only the differences in it, for example it may look like

return array(      'permissions'    => 'Please agree to the permissions',  );  

So obviously, any email error message will come from validate.php (inherited), but any permissions error will come from the new file with the error definition for permissions.

I named the file validate.php because the inherit behaviour seems to work with the system folder and that is what it is called under SYSPATH/messages/validate.php (see it on GitHub).

Can my error messages inherit from a base file, or should I just copy all the error messages per form?


Solution:1

Inheritance works automatically, follow this pattern:

  1. Search for a field+error specific message in the given file
  2. Search for a field+default message in the given file
  3. Search for a generic message in the given file
  4. Search for a generic message in validate file

So, if you overload the validate file and change the default messages, then inheritance will work as expected.


Solution:2

common errors: APPPATH/messages/validate.php

return array(      'email'        => 'You must enter a valid email.',      'name'         => 'You must enter your name.',      'enquiry'      => 'You must enter your enquiry.',      'captcha' => array (          'Captcha::valid' => 'The characters you entered did not match the image. Please try again.',          'not_empty' => 'You must enter the characters from the image.'      )  );  

specific errors: APPPATH/messages/specific.php

return array(      'permissions'    => 'Please agree to the permissions',  );  

Kohana uses these sequence to find the message: APPPATH/messages/specific.php, APPPATH/messages/validate.php and SYSPATH/messages/validate.php

print_r(validate->errors('specific'));  


Solution:3

without "hacks":

    $orm->values($form[$this->name])->check();        $not_model_errors = Validate::factory(array())->rule(NULL, 'permissions_rules'); // doesn't matter what args you send here, just meet the vartype      // add test error      $not_model_errors->error(NULL, 'test_error', array());        $this->template->errors = $orm->validate()->errors('validation') + $not_model_errors->errors('permissions');  

your model shouldn't validate your businesses logic.


Solution:4

This is hacky, but it works!

$commonErrors = include APPPATH .  'messages/validate.php';    $errors =  array(      'permission' => array(          'not_empty' => 'You must give permission to continue.'      )  );    return array_merge($commonErrors, $errors);  

Basically, it auto inherits the base rules for you!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »