1

I'm working a on website project which includes a web form which asks for various personal details, one of the fields on the form is Date of Birth.

The form is being validated by a custom plugin which is using models for validation.

Currently the AttributeType for the Date Of Birth field is defined as AttributeType::DateTime, so bad values just get removed during POST, however it would be nice to try and catch values that fail a date format validation of dd/MM/yyyy so the user can correct it, otherwise the field value will simply be discarded and user will be unaware of this.

What is best way to check and catch that the Date Of Birth field value isn't in the required format? I want avoid any regex related parsing as I'm sure there are better ways to examine the format using other PHP date related functions.

1

Would it be worth doing some client-side validation first using javascript to check the date of birth field is entered correctly? You can use an onChange event on your field and then split the string on "/" and check each part is within a valid range 0-31, 1-12, 1900-2016.

For serverside checking, you can use strtotime() and check that the return isn't false.

| improve this answer | |
  • A note, for UK dates, you would have to convert the format to US first before using strtotime. – James White Jul 21 '16 at 8:28
  • Well spotted. You could also use DateTime::createFromFormat if your form has separate fields for the day / month / year. If it's a single input field then you can't really tell if it's UK/US for certain dates. – Alex Holsgrove Jul 21 '16 at 8:42
  • Yeah, in my case the field is a single input, so the only thing to go by would be the / character to convert to - or use explode() on and break down into separate values like $dd $mm $yyyy – James White Jul 21 '16 at 8:46
1

Here's what I ended up doing. Despite having the format of DD/MM/YYYY on the front-end the Craft model actually stores the date in YYYY-MM-DD. Given the main the reason for wanting to perform server side validation is to check certain parts of the date i.e. year, you can create an array out of the string. You'll want to do a check to make sure the date string is a valid date, otherwise you'll get PHP errors with undefined offsets if trying to access array parts that aren't there.

Example:

  • $value - original date string submitted on the form, stored as YYYY-MM-DD by Craft
  • isValidDate - Perform a check on the string value to ensure its a valid date value. DateTime::createFromFormat would return a date value if true, if it doesn't match the specified format, its false.
  • $dateOfBirth - Using explode() create an array of the date string, accessible in parts as [0], [1], [2]

    if($value != '')
    {
        $isValidDate = DateTime::createFromFormat('Y-m-d', $value);
    
        if($isValidDate)
        {
            $dateOfBirth = explode('-', $value);
            $dobYear = $dateOfBirth[0];
            $dobMonth = $dateOfBirth[1];
            $dobDay = $dateOfBirth[2];
    
            // Further validation on date parts can occur here.
        }
        else 
        {
            // Not a valid date
        }   
    
    }
    
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.