06.10.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 507<br />

You may also specify more conditions that will be added as “where” clauses to the query:<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.<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 />

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:<br />

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

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

3 'games' => 'required|numeric',<br />

4 ]);<br />

Let’s assume our web application is for game collectors. If a game collector registers with our<br />

application and they own more than 100 games, we want them to explain why they own so many

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

Saved successfully!

Ooh no, something went wrong!