Kohana 3.2验证多个模型

时间:2013-05-29 19:29:52

标签: php validation orm kohana

我一直在搜索Google,但我没有找到任何关于如何使用Kohana 3.2验证多个模型的示例。

try 
{
     $one = ORM::factory('one'); 
     $one->values($this->request->post());
     $one->check();
     $two = ORM::factory('two'); 
     $two->values($this->request->post());
     $two->check();
} 
catch(ORM_Validation_Exception $e)
{
     $errors = $e->errors('models'); 
}   

如果“one”有任​​何错误,则永远不会检查“two”。

1 个答案:

答案 0 :(得分:0)

这是使用try catch的常规行为。像下面那样重写代码应该这样做

$errors = array();
try
{
    $one = ORM::factory('one')->values($this->request->post());
    $one->check();
}
catch (ORM_Validation_Exception $e)
{
    $errors = array_merge($errors, $e->errors('models'));
}
try
{
    $two = ORM::factory('two')->values($this->request->post());
    $two->check();
}
catch (ORM_Validation_Exception $e)
{
    $errors = array_merge($errors, $e->errors('models'));
}