Check if Date Entered is a Sunday

I have a form where I managed to change the date picker so that only sundays were selectable. That works OK, but people can still just enter any date manually, so what I was hoping to do was validate the entry somehow so that if the date is not a Sunday, they get an error message.

I assume the answer is using gform_field_validation but I can’t work out what the code would be, any suggestions welcomed

Something like the following should work (update form ID and field ID in the filter name)

add_filter('gform_field_validation_3156_1', function ( $result, $value, $form, $field, $context ) {
    
    if( DateTime::createFromFormat( 'm/d/Y', $value )->format('w') == 0 ) {

        $result['is_valid'] = true;

    } else {

        $result['is_valid'] = false;
        $result['message'] = 'Please enter a Sunday date';

    }
    
    return $result;

}, 10, 5 );

If you’d prefer users can’t enter other restricted dates, you can run the following to require they select from datepicker…

// require input via calendar on datepicker field
add_filter( 'gform_field_content', function ( $content, $field, $value, $lead_id, $form_id ) {

    if ( $field instanceof GF_Field_Date && $field->dateType == 'datepicker' ) {
        
        $content = str_replace( "<input", "onfocus='blur();' class='datepicker" , $content );
        
    }

    return $content;

}, 10, 5 );

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.