使用Laravel Validator检索失败的字段名和消息


Retrieve Failed Fieldnames and Messages with Laravel Validator

我想使用Laravel的内置验证器facade来验证一些表单字段,然后使用with运算符(例如:)将失败的字段名传回

foreach(Validator::make(Input::all(), $rules)->getErrors() as $field => $message)
{
  $failed_fields[$field] = $message;
}

return Redirect::to('user/login')->with('errors', array($failed_fields);

这样我就可以将数组传递给js数组,并消耗错误,然后找到字段并更改其背景颜色等。

知道如何以这种或类似的方式访问这些值吗?

好的,我在查看了负责的类后发现了它,它对我来说很简单。

 if($validator->fails())
        {
            return Redirect::route('cart')
                ->withErrors($validator)
                ->with('json_field_errors',json_encode($validator->errors()->getMessages()))
                ->withInput(Input::except('card_number'));
        }else{
            //Clear the cart and add the one product
            Cart::destroy();
            Cart::add('product', 'Description', 1, 69.95);
            //Pass to the processor for order processing
            return $this->process();
        }