Validation errors in AJAX mode

前端 未结 8 854
盖世英雄少女心
盖世英雄少女心 2021-01-30 00:14

Currently I use this to display validation errors via ajax:

            if (data.validation_failed == 1)
            {
                var arr = data.errors;
            


        
8条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 00:57

    I handled it using this way for laravel 5.5

    Html code

    " required="required"> {!! $errors->first('mandapName', ':message') !!}

    Controller validation code

     // Validate form data
        $validatedData = request()->validate([
          'mandapName' => 'required',
          'location' => 'required',
          'landmark' => 'required',
          'description' => 'required',
          'contactNo' => 'required',
          'slug' => 'required',
          'functional' => 'required'
        ]);
    

    And in javascript

         $.ajax({
            //.....Your ajax configuration
            success: function (data) {
                // Success code
    
            },
            error: function (request, status, error) {
                $('[name="mandapName"]').next('span').html(request.responseJSON.errors.mandapName);
                //.......
            }
        });
    

提交回复
热议问题