代码之家  ›  专栏  ›  技术社区  ›  alex

在Kohana 3的验证库中添加错误时出现问题

  •  2
  • alex  · 技术社区  · 15 年前

    我需要向Kohana3的验证助手添加一些错误。

    以下是我的出发点:

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

    现在,我也检查了一些东西 添加 遇到问题时出错

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

    我已经做了一些 var_dump() 在这些和他们 返回应添加错误的值!

    但是,当我打电话的时候 $post->check() ,它似乎只验证上面我在第一个代码块中添加的规则。

    我的/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.',
    );
    

    我在这里做错什么了吗?谢谢

    更新

    我刚刚在验证库中做了一些快速调试,即转储 _errors 每次调用后的属性 error 方法。

    我能看到的是,我的错误正在被添加,但随后被覆盖(可能与我上面添加的规则相冲突)。这正常吗?

    3 回复  |  直到 12 年前
        1
  •  4
  •   meze    15 年前

    作为一种替代方法(如果您不想黑客核心),您可以使用回调验证器。然后,您的代码将如下所示:

        $post->callback('state', array($this, 'doesStateExist'));
        $post->callback('email', array($this->userModel, 'doesEmailExist'));
    
        2
  •  0
  •   shadowhand    15 年前

    你应该一直跑 $validate->check() 在进行自己的检查和添加错误之前。梅兹的回答会更好。

        3
  •  0
  •   michaelb958--GoFundMonica Mong Tararak Ruchirabha    12 年前

    我找到了另一种附加错误消息的方法:

    $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);
    
    推荐文章