1

I made custom request as following.

class CustomRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
      $rule['name']='required';
      $rule['email'] = 'required|email';
      return $rule;
    }
}

How can I return validation errors in ajax? When I didn't use custom request, I returned errors like this.

public function store(Request $request)
{
   $validation = Validator::make($request->all(), [
      'name'=>'required',
      'email'=>'required|email'
   ]
   if($validation->fails())
   {
      return response()->json([$errors=>$validation->errors()]);
   }
   return response()->json(['status'=>'success']);
}

So here instead of Request, if I use CustomRequest then how can we catch errors?


Another thing. In custom request rule, how can we get request input values?

   public function rules()
    {
      $rule['name']='required';
      if($this->input('phone')) {
        $rule['phone'] = 'integer';
      }
      $rule['email'] = 'required|email';
      return $rule;
    }

$this->input('phone') Is this right? Hope to give me answer to my 2 questions.

LoveCoding
  • 1,121
  • 2
  • 12
  • 33

1 Answers1

1

the error will be either in your error call back function or catch callback depends on how you make ajax call.

eg.

$.ajax({
        url: "/test",
        type: "post",
        data: {foo:'test'} ,
        success: function (response) {
            console.log(response);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            var data = jqXHR.responseJSON;
            console.log(data.errors);// this will be the error bag.
        }

for FormRequest I think your code is not correct

public function rules()
{
    return [
        'title' => 'required',
        'body' => 'required',
    ];
}

This will do the validation. You do not really need to access the input value as Laravel does it for you. But for some reason, if you really want it you can always use the global helper function request()->input('title');

Andy Song
  • 4,404
  • 1
  • 11
  • 29
  • 1
    well, it returns 422 error (Unprocessable Entity) And I think Form validation rule is correct. Because it's same return values.($rule is array), With your simple return array, we can't validate according to input value. – LoveCoding Apr 09 '20 at 08:33
  • 1
    it's 422, because something is wrong, there is no problem with it. you can still access the error message from it. @lovecoding – Andy Song Apr 09 '20 at 08:35
  • 1
    @lovecoding we can validate input, just `$validated = $request->validated();` this will give the validated input data. – Andy Song Apr 09 '20 at 08:37
  • 1
    @lovecoding the only thing is before it was `store(Request $request)` now it should be `store(CustomRequest $request)` – Andy Song Apr 09 '20 at 08:38
  • 1
    status [422](https://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422) is from rfc4918 – Andy Song Apr 09 '20 at 08:43
  • Yes, You are right. I was catching errors in ajax success, but in this case, I think it will return message in ajax error. – LoveCoding Apr 09 '20 at 08:47
  • Thanks, It validated input values successfully but. how can we remove this error showing in browser? https://snipboard.io/LpGTSk.jpg – LoveCoding Apr 09 '20 at 08:50
  • @lovecoding if it's in the `error` or `catch` it's treated as something is wrong, usually, code starts with 4**. So you cannot remove it. just 404, 403 402. That's just how it is. – Andy Song Apr 09 '20 at 08:53
  • Well, but I think it's not good way, For validation, it shouldn't throw error in browser. – LoveCoding Apr 09 '20 at 08:55
  • @lovecoding then fire an issue on `laravel` github ask to change the status code lol. – Andy Song Apr 09 '20 at 08:57
  • And also request()->input('title') this didn't work in rules() function. Instead of it, $this->title worked for me. – LoveCoding Apr 09 '20 at 08:58
  • @lovecoding ah i did not know that, good point. but you do not need to get it manually anyways. – Andy Song Apr 09 '20 at 08:59