Date element validation rule

jo-ka

Member
Hello,

I have a form with 2 date elements. One for starting date and other for end date.

Can you please point me the best way to have some validation rules like:

- Start date can't be less that today plus 10 days, this is, I can only select a start date 10 days ahead.
- End date must be greater or equal to start date.

I think it's possible to run this rules on form post, but it would be great tho have this rules running before the form post, maybe with AJAX or even JavaScript.

Any clue?

Thanks in advance.
 
You should be able to do a PHP validation, and enable AJAX validations on the form.

In the validation you can access the dates through $formModel->formData[] array. One things you have to watch for is that sometimes they'll be an array, sometimes they'll be a string.

Code:
        $startDate = $formModel->formData['youtable___startdate'];

        if (is_array($startDate))
        {
            $sDate = explode(' ', $startDate['date']);
            $sDate = $sDate[0] . ' ' . ltrim($startDate['time']);
        }
        else
        {
            $sDate = $startDate;
        }
[code]

Once you have a date, you can then do normal PHP date comparisons, like to test for < today + 10 days ...

[code]
$eDate = new DateTime();
$eDate->modify("+10 days");

return $sDate < $eDate;

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top