TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Forums are in read-only mode while we transition to new software.
Custom validation error messages in Kohana 3.
  • I'm attempting to load custom error messages to use when validating my 'sign up' form.

    User model:

    https://github.com/ashleyconnor/Egotist/blob/master/classes/model/user.php

    Account Controller:

    https://github.com/ashleyconnor/Egotist/blob/master/classes/controller/user/account.php

    Sign Up View:

    https://github.com/ashleyconnor/Egotist/blob/master/views/account/signup.php

    I then placed user.php in /messages/models/user.php but my new error messages are not being displayed in the form.

    <?php defined('SYSPATH') or die('No direct script access.');
    
      return array
      (
         'username' => array
         (
            'not_empty' => 'your message',
            'max_length' => 'your message',
            'alpha_dash' => 'your message',
            'default' => 'default message'
         ),
      );
    
    ?>
    

    https://github.com/ashleyconnor/Egotist/blob/master/messages/models/user.php

    So submitting an empty form gives the following error messages:

    • Username must not be empty
    • Email Address must not be empty
    • Password must not be empty

    Which are the default ones.

  • Man, I hated that Packt book. Only half-useful thanks to all the typos and omissions, plus the version's out-of-date.

    I know this is an old, unanswered question, but I just figured out Kohana 3.2 custom validation errors in conjunction with ORM this morning, and I came across this question while Googling (unsuccessfully) for a short-cut, so here goes. Maybe it'll help you out, maybe it'll help others in the future.

    The problem's primarily with this call in your sign-up view:

    $errors = $post->errors('signup');

    The errors() method in the Validation class makes four attempts to find error messages. The first three of them looks for the filename, passed as a parameter, in your messages directory. The above call would imply that your custom error messages live in application/messages/signup.php. Since you don't in fact have a file at messages/signup.php, the errors() method defaults to the fourth attempt - which looks for a hard-coded 'validation' file, and finds it (and the defaults) at system/messages/validation.php. That's why you're getting the defaults.

    When you use ORM, you get some syntactic sugar with it - the model name automatically gets appended to the filename you provide the errors() method. So when you make your above call using ORM, I believe it's looking for a file at application/messages/signup/users.php. (Or maybe application/messages/signup.users.php - I didn't test this out.)

    In my application using Kohana 3.2 & ORM, I store my user model's custom error messages in a file at application/messages/users.php. To access those messages, I make this call:

    $errors = $post->errors('');

    The blank string as a parameter is necessary - if you call the error() method without any parameters you'll just get back the raw errors array without any error messages attached.

    Note that I have no idea whether this works in Kohana 3.0, the elderly version of Kohana used in that Packt book. It works fine in Kohana 3.2.

    I'm a total noob at this stuff and still prone to stupid mistakes, so if anyone more experienced than me has corrections or suggestions on how to do it better, correct and/or suggest away.

  • Big thanks to @gyardley, I encountered the same problem. A workaround would be to use this method:

    $user->validation()->check();

    Getting errors in this fashion:

    $user->validation()->errors('here_is_my_filename_in_messages');

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion