Kalhan.Toress
Kalhan.Toress

Reputation: 21901

laravel exists validation for more columns with OR

I want to check active_status of a user is A OR S in the validation, this is what i did and it check only active_status is sets to A, and now i want to extends it to check the condition A OR S

$checkUserActiveInMyPageRules = Array(
    'user' => 'required|exists:user,id,active_status,A'
);

is there any inbuilt option in laravel to make this work?

Upvotes: 0

Views: 1543

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

You cannot check for a set of values using built-in exists validation, so you'll need to extend the Validator with your custom validation.

Validator::extend('exists_with_attribute', function($attribute, $value, $parameters)
{
  $table = $parameters[0];
  $column = $parameters[1];
  $checkColumn = $parameters[2];
  $checkValues = array_slice($parameters, 3);

  return DB::table($table)->where($column, $value)->whereIn($checkColumn, $checkValues)->count() > 0;
});

and use it in your validation rules

$checkUserActiveInMyPageRules = Array(
  'user' => 'required|exists_with_attribute,id,active_status,A,S'
);

See the docs for more details about extending validator functionality: http://laravel.com/docs/4.2/validation#custom-validation-rules

Upvotes: 1

Related Questions