Sometimes Validation Rule In Laravel

Web Developer Kaustubh
2 min readOct 28, 2019

--

When to use sometimes validation rule?

In some scenarios, you want to apply certain validation if a particular field is present then this Laravel validation rule plays important role.

Example:-

Validation code in the controller
Without age input

In the above input we are not passing age input therefore in the controller it doesn’t validate age input but in bellow scenario, we are passing name as well as age so it is validating both inputs

With name and age input

Here we are passing both name and age, therefore, it validates both name and age.

Complex Conditional Validation

Sometimes you may wish to add validation rules based on more complex conditional logic,let see how to achieve this with example.

Controller

Above code shows how to apply complex validation in Laravel

Blade

if age is bellow 60 then the salary field is compulsory.

Form

Here user inserted age 45, therefore, the salary field is compulsory.

--

--

Web Developer Kaustubh

Sr.Web developer. love to code. PHP , Javascript, Mysql, Laravel, Jquey, git, project planing,Linux