Can you show your code, there are many way to do it.
How to validate multiple fields together?
I am looking at validating multiple fields together. I am sending through the expiration month and expiration year of a credit card. I want to validate that they are the equal to or later then the current date.
I know how to handle the validation logic, but how do I have my validator check multiple fields at once?
client side or server side?
Server side. They both come through as seperate values, cc_expire_month and cc_expire_year.
Effectively I want to do something like this:
$cc_expiration = Carbon::parse($cc_expire_year . '-' . $cc_expire_month)->endOfMonth();
if (Carbon::now() > $cc_expiration) {
return false;
}
return true;
The problem is, I don't know how to get the validator to pass both values through;
Ok, then just concatenate the date as you describe and store it on the $request before you pass it into the validation
https://laravel.com/docs/5.4/validation#quick-writing-the-validation-logic
Alternatively just concatenate it as an integer e.g. 201708 and check that this is smaller or bigger as another integer build in the exact same way (e.g. 201705).
You can use $varibale in the validation rules.
@EventFellows That's a good idea. Do you know anyway to do this using FormRequests?
@chrisblackwell you could create a custom validator (much easier than it sounds).
When I am home I can post an example.
@mstnorris I actually have a custom validator going, trying to figure out how to compare to different fields together.
In my AppServiceProvider this is what I have to compare an integer value that is provided against the size of an array (also provided).
In short, you need to get the data from the $validator using $validator->getData().
There is a little complexity in the code below as I am working with nested arrays and I don't have time right now to strip it back to its most basic form, but I hope this gets you started.
Validator::extend('quantity', function ($attribute, $value, $parameters, $validator) {
return collect(array_get(array_get($validator->getData(), 'stock'), 'content'))
->filter(function ($item) {
return $item['quantity'] != count($item['codes']);
})->isEmpty();
});
I don't know of a way to concatenate before it goes to the form request (maybe it could work in the constructor of the form request)
But in FormRequest you could create dynamic values for your rules, that should do.
Here is how:
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$minYear = $this->getDynamicYear();
$minMonth = $this->getDynamicMonth();
return [
'year_from_form' => "required|min:$minYear",
'month_from_form' => "required|min:$minMonth",
];
}
public function getDynamicYear()
{
// return your current year, e.g. 2017 as an integer;
}
public function getDynamicMonth()
{
// if('year_from_form' == $this->getDynamicYear()) { // this is sudo code jsut to show logic
// return 'current_month_as_an_integer'; Only if the year given is hte current year the month input must be bigger or eqaul to current month. if year is in future month can be anyhting.
} else {
return 1;
}
}
Please or to participate in this conversation.