Error message to check

I am working on an authorization system for Kohana. I do this only for education ...

This is what my controller looks like, which checks for 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 am looking for a way to display a different error message for each rule added. My goal is to pass it on (one or all (if occurs)) in order to view and display them there.

Pseudo Code:

errorFor( 'username', 'not_empty' ) => 'Username is required! Try again...';

How to determine a different error for each rule? I can not find anything clear to me in the documents ...

+3
source share
2 answers

You have:

$validation = ...

So, first you have to check if the variables pass:

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;
+2

.

: http://kohanaframework.org/3.1/guide/orm/examples/validation , .

, .

KO3.1 ( ) Validation , . catch (ORM_Validation_Exception $e), $e->errors('some_directory'), , messages/some_directory/model_name.php , .

0

All Articles