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...
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With