开发者

Problem with adding errors to Validation library in Kohana 3

开发者 https://www.devze.com 2022-12-21 05:36 出处:网络
I need to add some errors to the Validation helper in Kohana 3. Here is what I start with: // validate form

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 alre开发者_JS百科ady 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?


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'));


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


I found another way to append error messages:

$errors = array();

if (!$post->check()) {
   $errors += $post->errors();
}

if (!isset($_POST['something'])) {
   $errors['something'] = 'Please enter something';
}

if (empty($errors)) {
  $orm->save();
  return;
}

$tpl->error_fields($errors);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号