Laravel Course

- Displaying Validation Errors
- Custom Error Messages
- Practical Example - Validate upload image
- Available Validation Rules

Laravel's base controller class uses a ValidatesRequests trait which provides a validate() method to all controllers to validate incoming HTTP request with a variety of validation rules.
For example, you can validate form fields to match a specified minimum and maximum number of characters, or to match a given regular expression, or an uploaded file to have its dimensions between some specified values.

The validate() method accepts an incoming HTTP request and an array with a set of validation rules.
- Example, validate 'title', 'description', and 'content' fields before inserting in database:
public function store(Request $request){
  $this->validate($request, [
    'title'=>'required|unique:table_name|min:3|max:110',
    'description'=>'required|max:160',
    'content'=>'required',
  ]);

  //The post is valid, store in database...
}
Above it is specified that the fields are required, the title must be unique in a given database table (table_name) in a column with the same name (title), and to have between 3 and 110 characters. Description must have maximum 160 characters.
- If the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally.

Stopping on First Validation Failure

Rules will be validated in the order they are assigned.
To stop running validation rules on an attribute after the first validation failure, assign the bail rule to the attribute.
- Example, if the required rule on the title attribute fails, the unique rule will not be checked.
$this->validate($request, [
  'title'=>'bail|required|unique:table_name|min:3|max:110',
  'description'=>'required|max:160',
  'content'=>'required',
]);

Displaying Validation Errors

If the incoming request parameters do not pass the given validation rules, Laravel will automatically redirect the user back to their previous location. And all of the validation errors will automatically be flashed to the session, in $errors variable.
For AJAX requests, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.
The $errors variable will always be available in all of your views on every request.
Error message can be displayed in teplate by adding a code as shown below.
@if($errors->any())
<ul>
  @foreach($errors->all() as $error)
    <li>{{ $error }}</li>
  @endforeach
</ul>
@endif

Custom Error Messages

To customize the error messages, you may pass an array of attribute /rule pairs and their corresponding error messages as the third argument to the $this->validate() method.
You may utilize place-holders in validation messages: :attribute (will be replaced by the actual name of the field under validation), :max (The value of max property of the field that has this rule), :min, etc.
$rules =[
  'title'=>'bail|required|unique:table_name|min:3|max:110',
  'description'=>'required|max:160'
];

$messages =[
  'required'=> 'Please add a :attribute.',
  'title.min'=> 'The title must have at least :min characters.',
  'max'=> 'The :attribute must have maximum :max characters.'
];

$this->validate($request, $rules, $messages);
- Another way to create custom error messages is by adding your messages to custom array in the resources/lang/xx/validation.php language file.
'custom'=> [
  'title'=> [
    'required=> 'Please add a title',
    'min'=> 'The title must have at least :min characters',
  ],
],

Retrieving Error Message

- To retrieve the first error message for a given field, use the first('field_name') method:
$errors = $validator->errors();

echo $errors->first('email');
- To retrieve an array of all the messages for a given field, use the get('field_name') method:
foreach($errors->get('email') as $msg){
  //
}
To retrieve an array of all messages for all fields, use the all() method:
foreach($errors->all() as $msg){
  //
}
To determine if error message exist for a given field, use the has('field_name') method:
if($errors->has('email')){
  //
}

Practical Example - Validate upload image

In this example we create a validation upload image form with title and description fields.

1. Create an "uploads" folder in the "public/" directory.
2. Add the following code for controller in a "ValidateUpload.php" file (in app/Http/Controllers/ directory):
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ValidateUpload extends Controller {
  protected $dir ='uploads'; //folder for uploaded files (in public/ directory)

  //responds to GET /upload
  //returns view with upload-form
  public function showForm(){
    return view('upload_form');
  }

  //validate the form fields
  public function validateForm(Request $request){
    //define Rules for validation
    $rules =[
      'photo'=>'required|mimes:bmp,gif,jpe,jpeg,jpg,png|max:500|dimensions:min_width=80,min_height=80,max_width=800,max_height=800',
      'title'=>'required|min:3|max:110',
      'description'=>'max:250'
    ];

    //set custom error messages
    $msgs =[
      'required'=> 'Please add a :attribute',
      'max'=> 'The :attribute must have maximum :max characters',
      'photo.max'=> 'The image file must have maximum :max KB',

      //HERE you can define more custom messages
    ];

    //validate the request
    $this->validate($request, $rules, $msgs);
  }

  //Responds to POST /upload/photo
  //receives the $request instance
  //return redirect with 'status' string response
  public function uploadPhoto(Request $request){
    //validate the request
    $this->validateForm($request);

    $file = $request->file('photo');

    //set name and folder on server for uploaded file
    $fname = $file->getClientOriginalName();
    $fmoved = $file->move(base_path() .'/public/'. $this->dir, $fname);

    //if successfully moved in uploads directory
    if($fmoved){
      $resp ='<h4>File successfully uploaded:</h4>'. $this->dir .'/'. $fname .'<br><img src="/'. $this->dir .'/'. $fname .'" alt="Photo" /><br>- Title: '. $request->title .'<br>-Description: '. $request->description;
    }
    else $resp ='Unable to move the file to: '. $this->dir .'/'. $fname;

    return redirect()->route('upload')->with('status', $resp);
  }
}
- The 'photo' and 'title' fields are required. The upload file must be an image with maximum size 500 KB, and dimensions height and width between 80 and 800 pixels.
- The title must have minimum 3 and maximum 110 characters.

3. Add the following code in a view file "upload_form.blade.php" (in the "resources/views/" folder):
@if($errors->any())
<ul>
 @foreach($errors->all() as $error)
  <li>{{ $error }}</li>
 @endforeach
</ul>
@endif

@if(session('status'))
<blockquote>
 {!! session('status') !!}
</blockquote>
@endif

<h3>Upload Photo</h3>

<form method='post' action='/upload/photo' enctype='multipart/form-data'>
{{ csrf_field() }}
<p>File: <input type='file' name='photo'/></p>
<p>Title: <input type='text' name='title'/></p>
<p>Description: <input type='text' name='description'/></p>
<input type='submit' value='Upload'/>
</form>
4. Now, set up the routes; add this code in the "routes/web.php" file:
//shows the upload form
Route::name('upload')->get('/upload', 'ValidateUpload@showForm');

//when the upload form is submited
Route::post('/upload/photo', 'ValidateUpload@uploadPhoto');
5. Visit the following URL:
//localhost:8000/upload
- It will display a page like in this image:
laravel upload form

6. Click Upload button, without selecting a file, nor entering anything in the text fields. The output will be as shown in the following image:
laravel validate upload

Available Validation Rules

Below is a list of available validation rules and their function.
Accepted Active URL After (Date) After Or Equal (Date) Alpha Alpha Dash Alpha Numeric Array Before (Date) Before Or Equal (Date) Between Boolean Confirmed Date Date Format Different Digits Digits Between Dimensions (Image Files) Distinct E-Mail Exists (Database) File Filled Image (File) In In Array Integer IP Address JSON Max MIME Types MIME Type By File Extension Min Nullable Not In Numeric Present Regular Expression Required Required If Required Unless Required With Required With All Required Without Required Without All Same Size String Timezone Unique (Database) URL

accepted

The field under validation must be yes, on, 1, or true.
'agree'=> 'accepted'

active_url

The field under validation must have a valid DNS hostname record according to the dns_get_record() PHP function.
'url'=> 'active_url'

after:date

The field under validation must be a value after a given date. The dates will be passed into the strtotime() PHP function:
'start_date'=> 'required|date|after:tomorrow'
Instead of passing a date string to be evaluated by strtotime(), you may specify another field to compare against the date:
'finish_date'=> 'required|date|after:start_date'

after_or_equal:date

The field under validation must be a value after or equal to the given date.
'start_date'=> 'required|date|after_or_equal:yesterday'

alpha

The field under validation must be entirely alphabetic characters.
'field_name'=> 'alpha'

alpha_dash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.
'field_name'=> 'alpha_dash'

alpha_num

The field under validation must be entirely alpha-numeric characters.
'field_name'=> 'alpha_num'

array

The field under validation must be a PHP array.
'field_name'=> 'array'

before:date

The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime() function.
'start_date'=> 'required|date|before:yesterday'

before_or_equal:date

The field under validation must be a value preceding or equal to the given date.
'start_date'=> 'required|date|before_or_equal:yesterday'

'finish_date'=> 'required|date|before_or_equal:start_date'

between:min,max

The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same way as the size rule.
'title'=> 'required|between:3,110' //string between 3 and 110 characters

'number'=> 'between:10,40' //a number between 10 and 40

'file'=> 'between:1,500' //file between 1 and 500 KB

boolean

The field under validation must be: true, false, 1, 0, "1", or "0".
'field_name'=> 'boolean'

confirmed

The field under validation must have a matching field of '..._confirmation'. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.
'password'=> 'confirmed'

date

The field under validation must be a valid date according to the strtotime() PHP function.
'start_date'=> 'date'

date_format:format

The field under validation must match the given format. You should use either date or date_format when validating a field, not both.
'end_date'=> 'Y-n-j' //2017-8-15

different:field

The field under validation must have a different value than field.
'field_name'=> 'different:other_field'

digits:value

The field under validation must be numeric and must have an exact length of value.
'number'=> 'digits:3' //789

digits_between:min,max

The field under validation must have a length between the given min and max.
'number'=> 'digits_between:2,4' //23, 456, 7890

dimensions

The file under validation must be an image, with the dimension specified by the rule's parameters:
'avatar' => 'dimensions:min_width=100,min_height=200'
Available parameters are: min_width, max_width, min_height, max_height, width, height, ratio.
- A ratio should be represented as width divided by height. This can be specified either by a statement like 3/2 or a float like 1.5:
'avatar' => 'dimensions:ratio=3/2'
Since this rule requires several arguments, you may use the Rule::dimensions method to fluently construct the rule:
use Illuminate\Validation\Rule;

//$data is the $request
Validator::make($data, [
  'avatar' => [
    'required',
    Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2),
  ]
]);

distinct

When working with arrays, the field under validation must not have any duplicate values.
'foo.*.id' => 'distinct'

email

The field under validation must be formatted as an e-mail address.
'email'=> 'email'

exists:table,column

The field under validation must exist on a given database table.
'state'=> 'exists:states'
- Specifying a custom column name:
'state'=> 'exists:table_name,column_name'
To use a specific database connection for the exists query, prepend the connection name to the table name using "dot" syntax:
'email' => 'exists:conn_name.staff,email'

file

The field under validation must be a successfully uploaded file.
'photo'=> 'file'

filled

The field under validation must not be empty.
'title'=> 'filled'

image

The file under validation must be an image (jpeg, png, bmp, gif, or svg).
'photo'=> 'image'

in:foo,bar,...

The field under validation must be included in the given list of values.
'zones'=> 'required|in:first-zone,second-zone'
Since this rule often requires to implode an array, the Rule::in method may be used to fluently construct the rule:
use Illuminate\Validation\Rule;

//$data is the $request
Validator::make($data, [
  'zones' => [
    'required',
    Rule::in(['first-zone', 'second-zone']),
  ]
]);

in_array:anotherfield

The field under validation must exist in another_field's values.
'field_name'=> 'in_array:another_field'

integer

The field under validation must be an integer.
'day'=> 'integer'

ip

The field under validation must be an IP address.

ipv4

The field under validation must be an IPv4 address.

ipv6

The field under validation must be an IPv6 address.
'ip'=> 'ip'

'ip'=> 'ipv4'

'ip'=> 'ipv6'

json

The field under validation must be a valid JSON string.
'field_name'=> 'json'

max:value

The field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same way as the size rule.
'title'=> 'max:110' //string with maximum 110 characters

'number'=> 'max:40' //a maximum number of 40

'file'=> 'max:500' //maximum file size 500 KB

mimetypes:text/plain,...

The file under validation must match one of the given MIME types:
'video'=> 'mimetypes:video/avi,video/mpeg,video/quicktime'
To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client provided MIME type.

mimes:foo,bar,...

The file under validation must have a MIME type corresponding to one of the listed extensions.
'photo'=> 'mimes:jpeg,bmp,png'
Even though you only need to specify the extensions, this rule actually validates against the MIME type of the file by reading the file's contents and guessing its MIME type.
- A full listing of MIME types and their corresponding extensions may be found at the following location: //svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

min:value

The field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same way as the size rule.
'title'=> 'min:110' //string with minimum 110 characters

'number'=> 'min:40' //a minimum number of 40

'file'=> 'min:20' //minimum file size 20 KB

nullable

The field under validation may be null. This is useful when validating strings and integers that can contain null values.
'field_name'=> 'nullable'

not_in:foo,bar,...

The field under validation must not be included in the given list of values.
'field_name'=> 'not_in:abc,xyz'

numeric

The field under validation must be numeric.
'number'=> 'numeric'

present

The field under validation must be present in the input data but can be empty.
'description'=> 'present'

regex:pattern

The field under validation must match the given regular expression.
'field_name'=> 'regex:(^[A-z]+-[0-9]+$)' //Abc-123

required

The field under validation must be present in the input data and not empty.
'title'=> 'required'
- A field is considered "empty" if one of the following conditions are true:

required_if:anotherfield,value,...

The field under validation must be present and not empty if the another_field is equal to any value.
'description'=> 'required_if:another_field,val_1,val_2'

required_unless:anotherfield,value,...

The field under validation must be present and not empty unless the another_field is equal to any value.
'description'=> 'required_unless:another_field,val_1,val_2'

required_with:foo,bar,...

The field under validation must be present and not empty only if any of the other specified fields are present.
'field_name'=> 'required_with:other_field,another_field'

required_with_all:foo,bar,...

The field under validation must be present and not empty only if all of the other specified fields are present.
'field_name'=> 'required_with_all:other_field,another_field'

required_without:foo,bar,...

The field under validation must be present and not empty only when any of the other specified fields are not present.
'field_name'=> 'required_without:other_field,another_field'

required_without_all:foo,bar,...

The field under validation must be present and not empty only when all of the other specified fields are not present.
'field_name'=> 'required_without_all:other_field,another_field'

same:field

The given field must match the field under validation.
'field_name'=> 'same:other_field'

size:value

The field under validation must have a size matching the given value.
- For string data, value corresponds to the number of characters.
- For numeric data, value corresponds to a given integer value.
- For an array, size corresponds to the count() of the array.
- For files, size corresponds to the file size in kilobytes.
'code'=> 'size:8' //string with 8 characters

'number'=> 'size:40' //the number 40

'file'=> 'size:20' //file size 20 KB

string

The field under validation must be a string.
- If you would like to allow the field to also be null, you should assign the nullable rule to the field.
'field_name'=> 'string|nullable'

timezone

The field under validation must be a valid timezone identifier according to the timezone_identifiers_list() PHP function.
'zone'=> 'timezone' //Europe/London

unique:table,column,except_id,id_column

The field under validation must be unique in a given database table. If the column option is not specified, the field name will be used.
'email' => 'unique:users,email_address'
- With custom Database Connection:
'email' => 'unique:conn_name.users,email_address'
To tell the unique rule to ignore a specified ID, you may pass the ID as the third parameter:
'email' => 'unique:users,email_address,'.$id
- If your table uses a primary key column name other than id, to ignore it, you may specify it as the fourth parameter:
'email' => 'unique:users,email_address,'.$id.',id_col'
• Or, with the Rule class:
use Illuminate\Validation\Rule;

//$data is the $request
Validator::make($data, [
  'email' => [
    'required',
    Rule::unique('users')->ignore($id, 'id_col')
  ]
]);
• You can also use the Rule class, to customize the query with additional where() parameters:
use Illuminate\Validation\Rule;

//$data is the $request
Validator::make($data, [
  'email'=>[
    'required',
    'email'=> Rule::unique('users')->where(function($query){
      $query->where('account_id', 1);
    })
  ]
]);
- In the rule above, only rows with an account_id of 1 would be included in the unique check.

url

The field under validation must be a valid URL.
'link'=> 'url'


- Documentation: Laravel - Validation

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Laravel - Validation

Last accessed pages

  1. Common PHP Errors and Solutions (9771)
  2. PHP getElementById and getElementsByTagName (49471)
  3. Mysql SELECT JOIN tables on two different Databases (4498)
  4. jQuery UI draggable - Drag elements (11448)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)