Laravel Validation: Exists with Additional Column Condition - Custom Validation Rule

Is there a way to link to another field when specifying a validation rule exists in Laravel? I want to say that input a must exist in table a, input b must exist in table b And the value for column x in table b must be equal to input a.

The best example:

public $rules = array( 'game_id' => 'required|exists:games,id', 'team1_id' => 'required|exists:teams,id,game_id,<game_id input value here>', 'team2_id' => 'required|exists:teams,id,game_id,<game_id input value here>' ); 

Thus, with my validation rules, I want to make sure that:

  • game_id exists in the games ( id ) table
  • team1_id exists in the teams table (field id ), and the column game_id (in the teams table) must equal the input value game_id .
  • As stated above for team2_id

So, if I entered 1 for game_id in my form, I want to be able to make sure that the entry in the command table for team1_id and team2_id is set to 1 for game_id .

Hope this makes sense.

thanks

+5
source share
4 answers

You want a custom validation rule , and I would create a separate class for this. But for brevity, it's pretty much the same here using inline closure:

 // give it meaningful name, I'll go with game_fixture as an example Validator::extend('game_fixture', function ($attribute, $value, $parameters, $validator) { if (count($parameters) < 4) { throw new \InvalidArgumentException("Validation rule game_fixture requires 4 parameters."); } $input = $validator->getData(); $verifier = $validator->getPresenceVerifier(); $collection = $parameters[0]; $column = $parameters[1]; $extra = [$parameters[2] => array_get($input, $parameters[3])]; $count = $verifier->getMultiCount($collection, $column, (array) $value, $extra); return $count >= 1; }); 

Then use just the following:

 $rules = array( 'game_id' => 'required|exists:games,id', // last parameter here refers to the 'game_id' value passed to the validator 'team1_id' => 'required|game_fixture:teams,id,game_id,game_id', 'team2_id' => 'required|game_fixture:teams,id,game_id,game_id' ); 
+6
source

EDIT: Presumably this does not work in Laravel 5.5. @ user3151197 the answer may do the trick.

I use Laravel 5.4 and it has the ability to add custom Rule to existing and unique rules. I think it appeared some time in 5.3

Here is my scenario: I have an email verification table, and I want to make sure that the transmitted machine code and activation code exist on the same line.

Be sure to enable use Illuminate\Validation\Rule;

 $activationCode = $request->activation_code; $rules = [ 'mc' => [ 'required', Rule::exists('email_verifications', 'machineCode') ->where(function ($query) use ($activationCode) { $query->where('activationCode', $activationCode); }), ], 'activation_code' => 'required|integer|min:5', 'operating_system' => 'required|alpha_num|max:45' ]; 

The first argument to the existence method is the table, and the second is the name of the user column that I use for the "mc" field. I pass in the second column that I want to check using the "use" keyword, and then use this field in the a where clause.

This is very convenient, because now I no longer need to configure a validation rule.

+5
source

Since your rules are model properties, you need to make some changes for them before starting the validator.

You can change your rules to:

 public $rules = array( 'game_id' => 'required|exists:games,id', 'team1_id' => 'required|exists:teams,id,game_id,{$game_id}', 'team2_id' => 'required|exists:teams,id,game_id,{$game_id}' ); 

and now you will need to use a loop to insert the correct value instead of the line {$game_id} .

I can show you how I did this in my case for the editing rule:

 public function validate($data, $translation, $editId = null) { $rules = $this->rules; $rules = array_intersect_key($rules, $data); foreach ($rules as $k => $v) { $rules[$k] = str_replace('{,id}',is_null($editId) ? '' : ','.$editId , $v); } $v = Validator::make($data, $rules, $translation); if ($v->fails()) { $this->errors = $v->errors(); return false; } return true; } 

You can do the same in your case by changing {$game_id} to $data['game_id'] (in my case I changed {,id} to ,$editId

EDIT

Of course, if you did not have $rules set as a property, which you could just do:

 $rules = array( 'game_id' => 'required|exists:games,id', 'team1_id' => 'required|exists:teams,id,game_id,'.$data['game_id'], 'team2_id' => 'required|exists:teams,id,game_id,'.$data['game_id'] ); 

where do you have the data set.

+1
source

try this for me.

 'email'=>'required|unique:admintable,Email,'.$adminid.',admin_id', 
+1
source

Source: https://habr.com/ru/post/1203715/


All Articles