Welcome Guest, Not a member yet? Register   Sign In
CI4 validation greater_than on date field
#1

HI ,

can i validate date with grater than ? 
this is my validation :
Code:
                'data_tour' => [
                    'label' => 'Data Partenza',
                    'rules' => 'required|valid_date[Y-m-d]|greater_than[' . date('Y-m-d') . ']',
                    'errors' => [
                        'required' => '{field} obbligatorio',
                        'valid_date' => ' {field} deve essere una data valida',
                        'greater_than' => ' {field} deve essere maggiore di oggi'.$post['data_tour'].'|'. date('Y-m-d'),                   
                    ]

                ],

and this is the validation (not passed) :
                Data Partenza deve essere maggiore di oggi2025-04-21|2025-04-20
2025-04-21 is bigger than 2025-04-20...
Reply
#2

(This post was last modified: 9 hours ago by grimpirate.)

Convert your date into a unix timestamp after validating it once as a valid date and then perform the greater_than validation. Alternatively, you could pass your dates in a specific format 'Ymd' and the comparison would work.
Reply
#3

(This post was last modified: 9 hours ago by pippuccio76.)

I create a custom rules:

Code:
<?php

namespace App\Validation;

class DateRules
{

    /**
    * Check if the first date is bigger than the second date.
    *
    * @param string $today oggi (Y-m-d format).
    * @param string $date_to_control data da controllare(Y-m-d format).
    * @return bool
    *
    */

    public function date_bigger_than_today(string $date_to_control)
    {

        $today = strtotime("now");
        $date_to_control = strtotime($date_to_control);

        if ($date_to_control > $today) {

            return true;

        } else {
           
            return false;
        }

    }

solved
Reply




Theme © iAndrew 2016 - Forum software by © MyBB