dtqf81594 2012-04-10 11:28
浏览 26
已采纳

验证模型值不会引发异常

I got a problem with validating models in Kohana ORM, it should throw an ORM_Validation_Exception, however, it does not. I am trying to validate an emailaddress.

Model_Emailaddress

public function rules()
{
    return array(
        'emailaddress' => array(
            array(
                'Valid::email'
            ),
        ),
    );
}

Controller_Test

public function action_valid()
    {
        $email = ORM::factory('emailaddress');

        $email->emailaddress = 'test';

        try
        {
            $email->create();

        }
        catch(ORM_Validation_Exception $e)
        {
            echo Debug::dump($e->errors());
        }
    }

This example should dump an array with errors to the screen but instead it just saves the emailaddress without further notices.

When I change 'Valid::email' to 'not_empty', it does throw an exception. After some debugging I discovered that Valid::email returns false, as it should.

EDIT: As some people stated, changing 'Valid::email' to 'email' does not help. Also, when I place an exit inside the email method, the script execution will be stopped.

  • 写回答

2条回答 默认 最新

  • duanhuang2150 2012-04-11 09:04
    关注

    After some heavy, heavy debugging I discovered that the Kohana validation class contains a bug in combination with php 5.3.3. It should work, but it doesn't.

    In Kohana_Validation::check()

    Change

    $data[$field] = Arr::get($this, $field);
    

    To

    $data[$field] = Arr::get($this->_data, $field);
    

    In Kohana_Validation::errors()

    Change

    ':value' => Arr::get($this, $field),
    

    To

    ':value' => Arr::get($this->_data, $field),
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改