Laravel - Validation

laravel

How can we display errors at the top of the form only if there are some validation errors?

@if(count($errors) > 0)
  <div class="row">
    <div class="col-md-6">
      <ul>
        @foreach($errors->all() as $error)
          <li>{{$error}}</li>
        @endforeach
      </ul>
    </div>
  </div>
@endif

How can we validate user input on the server side?

Inside the controller method, invokes the validate method:

$this->validate($request, [
  'email' => 'required|email|unique:users',
  'first_name' => 'required|max:120',
  'password' => 'required|min:4'
]);

How can we prevent Laravel from clearing out the form values when there are validation errors?

Update the form fields to include the value attribute:

<input class="form-control" type="text" name="email" id="email" value="{{ Request::old('email') }}"/>

How can we highlight fields that has validation error?

Update the form control:

<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License