kohana - 如何从文件中获取_external错误消息

时间:2014-05-04 04:19:11

标签: php kohana

我正在使用Kohana的验证方法来确定表单中存在某些强制值。在验证confirm_password时,错误' ORM_Validation_Exception的方法是以下列格式返回数组

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文件(也尝试将其放在/ messages / model中)文件夹,但它似乎不起作用。我是否应该致电$Errors = $e->errors('_external')加载错误消息,在这种情况下,我如何加载来自“用户”的消息?包含其余错误消息的文件?

2 个答案:

答案 0 :(得分: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']);
    }
}            

答案 1 :(得分:0)

你应该合并它们,我知道框架中没有任何功能或任何功能可以帮助你。不幸的是

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