dou72260 2010-02-24 01:45
浏览 42
已采纳

向Kohana 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?

  • 写回答

3条回答 默认 最新

  • douqi3064 2010-02-24 09:36
    关注

    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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗