mardi 15 mai 2018

How can we show Laravel Validation response in ReactJS render?

I want to display laravel validation list in ReactJS rendor function. I got below response.

{"message":"The given data was invalid.","errors":{"phone_number":["The phone number field is required."],"group_id":["The group id field is required."]}}

I don't know how to show errors like below in reactJS rendor.

@if($errors->has())
  <ul>
   @foreach ($errors->all() as $error)
      <li></li>
  @endforeach
 </ul>
@endif

This is my axios api form handle function.

 handleSubmit(e){
    e.preventDefault();
    console.log(this.state);

    if(this.state.action === "add"){
        axios.post('/api/add', this.state).then(response => {

                alert("Data Success inserted")

        }).catch(error => {
            if(error.response.status == 422){
                // alert("Aree Bapre.. Error ila mare...");
                this.setState({
                    errors: error.response.data.errors
                })
            }
        })
    }
  }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire