Wednesday, April 12, 2017

Displaying laravel error messages on redirect

I have a form which i am hoping to use to insert some data to mysql. I have setup the validator like this

public function insert_post(){
        $rules = array(
            'model' => 'required',
            'country' => 'required',
            'engine' => 'required'
                 );

        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {

            // get the error messages from the validator
            $messages = $validator->messages();
            echo '<pre>';
            print_r($messages);
            echo '</pre>';
            return Redirect::to('create-posts')
                ->withErrors($validator);
        }
        else {
            $i = new Text;
            $i->model = request('model');
            $i->country = request('country');
            $i->engine = request('engine');
            $i->save();
            return redirect('/');
        }
    }

My create-posts route looks like this.

 public function create_post(){
        return view('create-posts');
    }

However, its not displaying the error since i think i am loading a fresh create-posts and the validator messages are lost.

view code

 <div class="form-group">
                    <label for="inputEmail" class="control-label col-xs-2">Model</label>
                    <div class="col-xs-10">
                        <input type="text" class="form-control" id="inputEmail" name="model" placeholder="Model">
                        @if ($errors->has('model')) <p class="help-block"></p> @endif
                    </div>
                </div>

Is that what's the cause?.



via Geoffrey Isle

Advertisement