Reputation: 5859
I have a class which checks two dates and sees if the value is greater than or equal to the second parameter. My question is - how do you properly get the value from the second field I've used Input::get($value_to_compare) which I don't think is actually the correct way to go about it.
class CoreValidator extends Illuminate\Validation\Validator {
protected function validateDateLessThanOrEqualTo($attribute, $value, $parameters)
{
/*
* If a input with the name equal to the value we compare with, we
* use it, otherwise we proceed as usual
*/
if( isset( $this->attributes[ $parameters[0] ] ) )
{
$value_to_compare = $this->attributes[ $parameters[0] ];
}//if we have an input with this name
else
{
$value_to_compare = $parameters[0];
}//we compare with the provided value
return ( date_parse( $value ) <= date_parse( Input::get($value_to_compare) ) );
}
protected function replaceDateLessThanOrEqualTo($message, $attribute, $rule, $parameters)
{
return str_replace(':other', ucwords(str_replace('_', ' ', $parameters[0])), $message);
}
}
Upvotes: 0
Views: 220
Reputation: 152860
You can use the member variable $data
on the validator object to get the other attributes's value:
return ( date_parse( $value ) <= date_parse( $this->data[$value_to_compare] ) );
Upvotes: 1