Php kohana-如何从文件中获取外部错误消息

Php kohana-如何从文件中获取外部错误消息,php,kohana,Php,Kohana,我使用Kohana的验证方法来确定表单中存在某些强制值。验证确认\u密码时,ORM\u验证\u异常的“errors”方法返回以下格式的数组 array(1) ( "_external" => array(1) ( "password_confirm" => string(45) "password confirm must be the same as password" ) ) 如何使它与其他错误遵循相同的约定,以便我可以执行以下操作,并在视图文件

我使用Kohana的验证方法来确定表单中存在某些强制值。验证确认\u密码时,ORM\u验证\u异常的“errors”方法返回以下格式的数组

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; 

我曾尝试在messages下添加一个_外部文件,也尝试将其放置在/messages/model文件夹中,但似乎不起作用。我应该调用$Errors=$e->Errors“u external”来加载错误消息吗?在这种情况下,我如何从包含其余错误消息的“User”文件加载消息?

即使您使用消息文件翻译错误,该文件在您的情况下应该放在messages/User/\u external.php中,$Errors数组仍将具有相同的结构,即外部错误消息将位于其自己的子数组中,$Errors[''u 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']);
    }
}            

您应该合并它们,据我所知,框架中没有任何函数或任何东西可以为您这样做。不幸的是

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