2014-05-04 52 views
0

我正在使用Kohana的验证方法来确定表单中存在某些强制值。在验证confirm_password ORM_Validation_Exception的“错误”的方法在下面的格式返回数组kohana - 如何从文件中获取_external错误消息

array(1) (
    "_external" => array(1) (
     "password_confirm" => string(45) "password confirm must be the same as password" 
    ) 
) 

我怎样才能使它遵循相同的约定在错误的休息,这样我可以做以下的,只是通过遍历视图文件中的错误。

$Errors = $e->errors('user'); // inside the controller 

<?php if ($Errors): ?> 
<p class="message">Some errors were encountered, please check the details you entered.</p> 
<ul class="errors"> 
<?php 
echo Debug::vars($Errors); 
foreach ($Errors as $message): ?> 
    <li><?php echo $message ?></li> 
<?php endforeach ?> 
<?php endif; 

我曾尝试加入下消息的_external文件(也试过把它放在/消息/模型)文件夹,但它似乎没有工作。我应该呼叫$Errors = $e->errors('_external')加载错误消息,在这种情况下,我怎么能加载包含其余错误消息的'用户'文件中的消息?

回答

0

即使您使用翻译的消息文件(在你的情况应该在messages/user/<model>/_external.php去)的错误,你的$Errors阵列仍然具有相同的结构,即外部的错误信息会在自己的子阵列,$Errors['_external']

如果你需要它“扁平化”,我认为你必须是手工做的,如:

// The next line is from your question 
$Errors = $e->errors('user'); // inside the controller 

// If there are any '_external' errors, we place them directly into $Errors 
if (isset($Errors['_external'])) 
{ 
    // Keeps track of a possible edge case in which the _external 
    // array has a key '_external' 
    $double_external = isset($Errors['_external']['_external']); 

    // Move the elements of the sub-array of external errors into the main array 
    $Errors = array_merge_recursive($Errors, $Errors['_external']); 

    // Remove the '_external' subarray, except in the edge case 
    if (!$double_external) 
    { 
     unset($Errors['_external']); 
    } 
}    
0

你应该将它们合并,据AR我知道有没有功能或任何在框架这是为你做的。不幸。

$errors = $e->errors('user'); 
$errors = Arr::merge($errors, Arr::get($errors, '_external')); 
unset($errors['_external']);