Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Kohana 3's validation errors inherit?

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?

like image 479
alex Avatar asked Nov 30 '25 06:11

alex


2 Answers

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'));
like image 183
akira Avatar answered Dec 02 '25 19:12

akira


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.

like image 36
antpaw Avatar answered Dec 02 '25 20:12

antpaw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!