validation forms with axois using laravel

  axios, laravel

i am trying to submit empty form but validation is not working and i am getting error on response please help me how can i resolve this issue using axois Does anyone have an idea please help me thanks.

getting error

{"message":"The given data was invalid.","errors":{"name":["Name is required"],"email":["Email is required"]}}

controller

public function store(Request $request)
    {
        $this->validate(
            $request,
            [
                'name' => 'required',
                'email' => 'required|email|unique:subscribes,email',

            ],
            [
                'name.required' => 'Name is required',
                'email.required' => 'Email is required'
            ]
        );

        $subscribes = new Subscribe();
        $subscribes->name = $request->name;
        $subscribes->email = $request->email;
        $subscribes->save();

    }

axois script

<script>
  $(document).ready(function () {
      $("#prospects_form").submit(function (e) {
          e.preventDefault();
          let url = "{{route('home.store')}}";
          var name = $("#name").val();
          var email = $("#email").val();
          axios
              .post(url, {
                  name: name,
                  email: email,
              })
              .then((res) => {
                  console.log(res);
              })
              .catch(function (err) {
                  console.log(err);
              });
      });
  });
  </script>

Source: Laravel

Leave a Reply