【问题标题】:Laravel Validator fails due to array to string conversionLaravel Validator 由于数组到字符串的转换而失败
【发布时间】:2014-09-05 18:02:07
【问题描述】:

我正在尝试验证此输入:

$values = [                                                
    'id'                => $input['id'][$i],   
    'template_id'       => $input['template_id'][$i],   
    'schedulable_id'    => $id,                               
    'schedulable_type'  => $type,       
    'order_by'          => $i                                 
];

违反我的 Schedule 课程中的这些规则:

public static $rules = [                                                                                         
    'template_id'           => 'required|integer|exists:templates,id',                                        
    'schedulable_id'        => 'required|integer',                                                                  
    'schedulable_type'      => 'required|in:Item,Order',
    'order_by'              => 'integer'                                                                            
];

当我执行以下操作时,我总是在第 905 行的“/laravel/vendor/laravel/framework/src/Illuminate/Validation/Validator.php”中得到一个数组到字符串的转换错误:

$validator = Validator::make($values, Schedule::$rules);

if ($validator->fails()) {
    $errors[$i] = $validator->messages();

    continue;
}

为什么会这样?

【问题讨论】:

    标签: php validation laravel-4


    【解决方案1】:

    刚刚发现我有 Ardent 的 $forceEntityHydrationFromInput = true,我的输入无法直接从 Input 提取以进行验证,因为它是作为部分引用值的数组提交的。

    要解决此问题,请更改为 $forceEntityHydrationFromInput = false 并使用标准输入验证程序,而不是依赖 Ardent 的魔法。

    有时聪明的包太聪明了。

    【讨论】:

    • @gradd 它设置在模型的属性中。如果您一开始没有设置它,那么这可能不是您的问题。
    • 我还不知道,因为我从未设置过实际上我使用的是 L5.1
    猜你喜欢
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2017-01-14
    • 2020-09-10
    相关资源
    最近更新 更多