简体   繁体   English

Kohana 3.2验证多个模型

[英]Kohana 3.2 Validate More Than One Model

I've been searching Google, but I have not found any examples on how to validate more than one model with Kohana 3.2. 我一直在搜索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'); 
}   

If "one" has any errors, "two" is never checked. 如果“一个”有任何错误,则从不检查“两个”。

This is the regular behaviour using try catch . 这是使用try catch的常规行为。 Rewriting the code like the following should do it 像下面这样重写代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM