Laravel在表单请求错误时没有将错误传递给Ajax

时间:2017-06-19 17:43:00

标签: ajax laravel laravel-5.3

使用自定义表单请求向laravel控制器发布内容时,错误不会返回给ajax调用。

'错误中的代码' part永远不会被执行,错误只显示在控制台中,没有我在请求验证中指定的任何错误消息。

有关如何解决此问题的任何想法?

这是我的ajax请求

  var data = {
            'description' : $("#shiftdescription").val()
        };

 $.ajax({
                type: "POST",
                url: "{{ env('APP_SITE', 'http://localhost:8000') }}/shifts/store",
                content: "application/json; charset=utf-8",
                dataType: "json",
                data: data,
                success: function (response) {
                    $("#shiftdetails").html(response);
                },
                error: function(response){
                    var errors = $.parseJSON(response.responseText);
                    console.log(errors);
                    // Render the errors with js ...
                }
            });

这是表单请求

 public function rules()
{
    return [
        'description'   => 'required|string|min:5'
    ];
}

public function messages()
{
    return [
        'description.required'    => trans('crud.description').' '.trans('error.required'),
    ];
}

我收到此错误 enter image description here

编辑1 - 查看代码

<div class="modal-body">
<form>
    <div>
        <div class="form-group input-field col m2 l2 shiftform">
            {{ Form::text('description', null, array('id'=> 'shiftdescription', 'class'=>'validate', 'placeholder' => trans('crud.description'))) }}
            {{$errors->first('description', '<span class=error>:message</span>')}}
        </div>
        <a href="#!" onclick="nextshift_1()">Volgende stap</a>
    </div>
</form>

控制器

 public function store(Requests\StoreShiftRequest $request)
{
        $shift = new Shift();
        $shift->description = $request['description'];
...

0 个答案:

没有答案