views:

893

answers:

3

I need to add some errors to the Validation helper in Kohana 3.

Here is what I start with:

            // validate form
             $post = Validate::factory($_POST)
            // Trim all fields
            ->filter(TRUE, 'trim')
            // Rules for name
            ->rule('first-name', 'not_empty')
            ->rule('last-name', 'not_empty')

            // Rules for email address
            ->rule('email', 'not_empty')
            ->rule('email', 'email')

            // Rules for address stuff
            ->rule('address', 'not_empty')
            ->rule('suburb', 'not_empty')
            ->rule('state', 'not_empty')
            ->rule('postcode', 'not_empty')

            // Rules for misc
            ->rule('phone', 'not_empty')
            ->rule('company', 'not_empty')
            ->rule('abn', 'not_empty');

Now, I also check some things and add errors if a problem is encountered

         if ( ! in_array($post['state'], array_keys($states))) {
                $post->error('state', 'not_found');
            }


            if ( $this->userModel->doesEmailExist($post['email'])) {
                $post->error('email', 'already_exists');
        }

I've done some var_dump() on these and they are returning values which should add the error!

However, when I call $post->check(), it only seems to validate above the rules I added in the first code block above.

I have matching values also in my /application/messages/join.php

<?php defined('SYSPATH') or die('No direct script access.');

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'        => array(
        'email' => 'You must enter a valid email.',
        'already_exists' => 'This email is already associated with an account'
    ),

    'name'         => 'You must enter your name.',
);

Am I doing something wrong here? Thanks

Update

I just did a few quick debugging things in the Validation library, namely dumping the _errors property after every call to the error method.

What I can see, is that my errors are being added, but are then being overwritten (perhaps conflicting with the rules I added above). Is this normal?

A: 

This makes me feel wrong, as I hacked a core file. I am still seeking an answer though as to why it did not work.

In system/classes/Kohana/validate.php, I changed this

    $data = $this->_errors = array();

to

    $data = array();

and it worked great.

alex
+3  A: 

As an alternative way (if you don't want to hack core), you could use callback validators instead. Then your code will look like:

    $post->callback('state', array($this, 'doesStateExist'));
    $post->callback('email', array($this->userModel, 'doesEmailExist'));
meze
Yeh, I considered this too, but I didn't want to clutter the controller when I have gotten a way similar to above working in Kohana 2.3
alex
A: 

You should always run $validate->check() before doing your own checks and adding errors. meze's answer would be better.

shadowhand
Thanks for stopping by shadowhand and giving an authoritative answer. Got a whole lot of questions tagged [kohana-3], but I *think* most have good answers now.
alex
Actually, if I call `$validate->check()` before adding my own errors, how can I something like this? `if ($post->check()) { // succeed }` Perhaps I'm missing something...
alex