penguinfeet
4 years ago

Sending Laravel 5 errors to the front end with Javascript (React.js)

Posted 4 years ago by penguinfeet

Hi guys,

I am rendering my views with React.js code and have a simple input form. I would like to validate the input, and send back errors to the front end in the event that there are errors. The input form is sending the data back with Ajax.

I am unsure of how to do this?

View:

Relevant React.js code:

<div className="overall-input">
                                  <ReactCSSTransitionGroup transitionName="example" transitionAppear={true}>
                                      <input type="email" className="input_field" onChange={this._updateInputValue} ref="email" value={this.state.email} />

                                      <div className="button-row">
                                          <a href="#" className="button" onClick={this.saveAndContinue}>Request Invite</a>
                                      </div> 
                                  </ReactCSSTransitionGroup>
                              </div>   

request = $.ajax({ 
              url: "/user", 
              type: "post", 
              data: 'email=' + email + '&_token={{ csrf_token() }}',
              data: {'email': email, '_token': $('meta[name=_token]').attr('content')},
              beforeSend: function(data){console.log(data);},
              success:function(data){},  
        });

Controller:

public function store(Request $request) {

        $this->validate($request, [
            'email' => 'Required|Email|Min:2|Max:80'
        ]);

        //$v = User::validate(Input::all());

        $email = $request->input('email');;

        //if ( $v->passes() ) {  

            $user = new User;
            $user->email = $email;
            $user->save();

        //}

        return $email;

    }

Please sign in or create an account to participate in this conversation.