Recently I have been learning Laravel and came accross validator problem that would be solved by using validator rule required_unless
from Laravel 5.2:
$validator = Validator::make(
array(
'social_id' => $social_id,
'login_by' => $login_by
), array(
'social_id' => 'required_unless:login_by,manual',
'login_by' => "in:manual,google,facebook, stack_exchange, myspace"
)
);
Problem is that I use Laravel 4.2 and this validation rule is not implemented jet.
Is there any other validating rule I could use or any other way?
If not, how would I write a custom validation rule and where would I put it?
Edit: I could do:
$validator = Validator::make(
array(
'social_id' => $social_id,
'login_by' => $login_by
), array(
'social_id' => 'required_if:login_by,google,facebook, stack_exchange, myspace',
'login_by' => "in:manual,google,facebook, stack_exchange, myspace"
)
);
...but this is just a workaround not an elegant permanent solution.
You can simply extend the Validator with the extend
method.
Something like this
Validator::extend('required_unless', function ($attribute, $value, $parameters) {
// Implement your version of required_unless here
});
And even steal a bit of logic from L5.2 here
You can see the doc on extend here