Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisblackwell's avatar

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?

0 likes
9 replies
chrisblackwell's avatar

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;

mstnorris's avatar

@chrisblackwell you could create a custom validator (much easier than it sounds).

When I am home I can post an example.

chrisblackwell's avatar

@mstnorris I actually have a custom validator going, trying to figure out how to compare to different fields together.

mstnorris's avatar

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();
});
1 like
EventFellows's avatar

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.