I got a problem with validation rules with nested conditions.
class StoreRequest extends Request
{
public function authorize(){
return true;
Your rule performs two checks that are independent of one another; just because the external_id
field is not required when the type_id
!= 3, does not mean the integer check is ignored.
What you are looking for is a conditional rule, which gives you finer control of when to perform a check, e.g. :
$validator = Validator::make($data, [
'type_id' => 'required|integer'
]);
$validator->sometimes('external_id', 'required|integer', function($input) {
return $input->type_id == 3;
});
When using form validation, you can access the underlying validator instance by overriding the getValidatorInstance()
method:
class StoreRequest extends Request
{
public function authorize(){
return true;
}
public function rules(){
return [
'type_id' => 'required|integer'
];
}
protected function getValidatorInstance() {
$validator = parent::getValidatorInstance();
$validator->sometimes('external_id', 'required|integer', function($input) {
return $input->type_id == 3;
});
return $validator;
}
}