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

eugenefvdm's avatar

Validate a date based on another input request field

I have this beautiful date validation code below that works like a charm. Now the problem is the sold_at date must before before the transferred_at date.

So my question is how would I do additional validation using before based on something something (transferred_at) that is there already?

And as a bonus, how can I update the fancy feedback messages if .before has already been used in that context? Something like before.2?

public function rules()
    {                
        $before = Carbon::now(); // the dates cannot be in the future

        $after  = Carbon::now()->subDays(90); // a transfer cannot be older than 90 days

		// insert new rule here where the sold_at date must be before the transferred_at date

        return [
            'transferred_at' => 'required|date_format:"Y-m-d"|after:' . $after . '|before:' .  $before,
            'sold_at'        => 'required|date_format:"Y-m-d"|before:' . $before,            
            'photo_path'     => 'max:5000000', // 5 MB
        ];
    }

    public function messages()
    {
        return [
            'transferred_at.after'  => 'The date of transfer must not be more 90 days ago',
            'transferred_at.before' => 'The transfer date cannot be in the future',
            'sold_at.before'        => 'The date of sale cannot be in the future',
            'photo_path.max'        => 'The property photo cannot be larger than 5 Megabytes',
        ];
    }
0 likes
5 replies
bugsysha's avatar

Not sure I understand your question, but let me try.

You can use $this->input('transfered_at') to get the value of current transfered_at value to check if the sold_at is after transfered_at.

eugenefvdm's avatar

Hi @bugsysha ,

Having considered my options of using Laravel's built in automated rule resolution and doing it manually using $this->input I'm starting to lean towards just adding some code to do the checks. So if I use $this->input can I just do a return with a message if the value is wrong?

MostafaGamal's avatar
Level 10

simply do this:

'transferred_at' => 'after:sold_at'
eugenefvdm's avatar

Hi MostafaGamal,

I tried this:

return [
            'transferred_at' => 'required|date_format:"Y-m-d"|after:' . $after . '|before:' .  $before,
            'transferred_at' => 'after:sold_at',
            'sold_at'        => 'required|date_format:"Y-m-d"|before:' . $before,            
            'photo_path'     => 'max:5000000', // 5 MB
        ];

Now it seems the second transferred_at overrides the first one?

The transferred_at needs three actual checks:

  1. It must not be in the future
  2. It must be less than 90 days ago.
  3. If must be at least one day more than the sold_at date.

Unless I misunderstood you?

Also then tried adding a message for that below and that also doesn't seem to work because I don't think you can have transferred.after:sold_at and transferred_at.after. And sure two transferred_at.after doesn't work?

return [
            'transferred_at.after:sold_at' => 'The transfer date must be after the sale date',
            'transferred_at.after'         => 'The date of transfer must not be more 90 days ago',
            'transferred_at.before'        => 'The transfer date cannot be in the future',
            'sold_at.before'               => 'The date of sale cannot be in the future',
            'photo_path.max'               => 'The property photo cannot be larger than 5 Megabytes',
        ];
eugenefvdm's avatar

Ah! This is what you meant, add another section and update the message:

'transferred_at' => 'required|date_format:"Y-m-d"|after:sold_at|after:' . $after . '|before:' . $before,

message:

'transferred_at.after' => 'The date of transfer must be after the date of sale and less than 90 days ago',

EDIT:

Still not working. You can't have two after sections the first one overrides the second one.

Please or to participate in this conversation.