29.07.2016 Views

laravel-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Validation 387<br />

.<br />

1 'email' => 'unique:users,email_address,'.$user->id<br />

Adding Additional Where Clauses:<br />

You may also specify more conditions that will be added as “where” clauses to the query:<br />

.<br />

1 'email' => 'unique:users,email_address,NULL,id,account_id,1'<br />

In the rule above, only rows with an account_id of 1 would be included in the unique check.<br />

url<br />

The field under validation must be a valid URL according to PHP’s filter_var function.<br />

Conditionally Adding Rules<br />

In some situations, you may wish to run validation checks against a field only if that field is present<br />

in the input array. To quickly accomplish this, add the sometimes rule to your rule list:<br />

.<br />

1 $v = Validator::make($data, [<br />

2 'email' => 'sometimes|required|email',<br />

3 ]);<br />

In the example above, the email field will only be validated if it is present in the $data array.<br />

Complex Conditional Validation<br />

Sometimes you may wish to add validation rules based on more complex conditional logic. For<br />

example, you may wish to require a given field only if another field has a greater value than 100.<br />

Or, you may need two fields to have a given value only when another field is present. Adding these<br />

validation rules doesn’t have to be a pain. First, create a Validator instance with your static rules<br />

that never change:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!