Reputation: 21333
I'm working on authorization system for Kohana. I'm doing it just for education...
This is how looks my controller that checks submitted fields:
$validation =
Validation::factory( $_POST )
->rule( 'username', 'not_empty' )
->rule( 'username', 'max_length', array( ':value', 32 ) )
->rule( 'username', 'alpha_dash', array( ':value', true ) )
->rule( 'password', 'not_empty' )
->rule( 'password', 'min_length', array( ':value', 6 ) )
->rule( 'password', 'max_length', array( ':value', 255 ) )
->rule( 'passwordRepeatedly', 'not_empty' )
->rule( 'passwordRepeatedly', 'matches', array( ':validation', 'passwordRepeatedly', 'password' ) )
->rule( 'email', 'not_empty' )
->rule( 'email', 'email' );
I'm looking for the way to display different error message for each added rule. My goal is then pass it (one or all (if occurs)) to view and display them there.
Pseudo-code:
errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';
How to define different error for each rule? I can't find anything understandable for me in the docs...
Upvotes: 3
Views: 309
Reputation: 947
Validation rules make use of messages folder in your application directory.
Check this: http://kohanaframework.org/3.1/guide/orm/examples/validation for full validation example, where the messages file is at the very bottom of the page.
The things to note are the directory and the filename of the message file.
In KO3.1 (I believe) the Validation throws an exception whenever if fails. If you catch it with catch (ORM_Validation_Exception $e)
you can use $e->errors('some_directory')
to catch error messages, which are then pulled from messages/some_directory/model_name.php
in the form of array, just like in the example from the link above.
Upvotes: 0
Reputation: 699
You have:
$validation = ...
So, first you should check if variables pass validation:
if($validation->check())
{
// no errors
}
else
{
$errors = $validation->errors('user');
}
Then you should have user.php file in application/messages
<?php defined('SYSPATH') or die('No direct script access.');
return array
(
'input_name' => array
(
'rule' => 'your message',
'default' => 'default message'
),
'username' => array
(
'not_empty' => 'your message',
'max_length' => 'your message',
'alpha_dash' => 'your message',
'default' => 'default message'
),
);
?>
To display errors:
foreach($errors as $input_field => $message)
echo $message;
Upvotes: 2