• Resolved julio75

    (@kingkurt)


    Is it possible to create a function for the submission form to only allow to submit events in locations that start with certain first two digits of a postcode ?
    For example, postcodes starting with 75, 91, 92, 93, 95.
    I have tried to add a validate function, but it does not work.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Can you share your validate function?

    Thread Starter julio75

    (@kingkurt)

    For the moment I have this function which works fine.

    add_filter( 'em_event_validate', 'my_em_event_validate', 10, 2 );
    function my_em_event_validate( $is_valid, $EM_Event ) {
    if ( $is_valid ) {
    if( empty($EM_Event->post_content ) ){
    $is_valid = false;
    $EM_Event->add_error( sprintf(("%s are mandatory.", 'events-manager'), ('details (descriptions) of the event','events-manager') ) );
    }
    if( strlen($EM_Event->post_content) >=1 && str_word_count($EM_Event->post_content) <= 30 ){ $is_valid = false; $EM_Event->add_error( sprintf(("%s are too short (minimum 30 words).", 'events-manager'), ('etails (descriptions) of the event','events-manager') ) );
    }
    }
    return $is_valid;
    }

    Already when I add something like:

    if( empty($EM_Event->location_postcode ) ){
    $is_valid = false;
    $EM_Event->add_error( sprintf(("%s are mandatory.", 'events-manager'), ('post code','events-manager') ) );

    it shows all the time “post code are mandatory”
    what ever is filled in the field

    • This reply was modified 1 year, 10 months ago by julio75.
    • This reply was modified 1 year, 10 months ago by julio75.
    Thread Starter julio75

    (@kingkurt)

    For the moment I have this function which works fine.

    add_filter( 'em_event_validate', 'my_em_event_validate', 10, 2 );
    function my_em_event_validate( $is_valid, $EM_Event ) {
            if ( $is_valid ) {
                    if( empty($EM_Event->post_content ) ){
                            $is_valid = false;
                            $EM_Event->add_error( sprintf(__("%s are mandatory.", 'events-manager'), __('details (descriptions) of the event','events-manager') ) );
                    }
                    if( strlen($EM_Event->post_content) >=1 && str_word_count($EM_Event->post_content) <= 30 ){
                            $is_valid = false;
                            $EM_Event->add_error( sprintf(__("%s are too short (minimum 30 words).", 'events-manager'), __('etails (descriptions) of the event','events-manager') ) );
                    }
            }
            return $is_valid;
    }
    

    As the post code field is not mandatory in the original version. When I add something like

    if( empty($EM_Event->location_postcodet ) ){
                            $is_valid = false;
                            $EM_Event->add_error( sprintf(__("%s are mandatory.", 'events-manager'), __('post code','events-manager') ) );

    it shows all the time “post code are mandatory”
    what ever is filled out or empty in the field

    The postcode is in the location, not in the event. So you would need a new validation function for the location. The following should work:

    add_filter( 'em_location_validate_meta', 'my_em_location_validate_meta', 10, 2 );
    function my_em_location_validate_meta( $is_valid, $EM_Location ) {
         if ( empty( $EM_Location->location_postcode ) ) {
              $EM_Location->add_error( __('Postcode field','events-manager').__(" is required.", 'events-manager') );
             $is_valid = false;
        }
        else {
            $value = substr( $EM_Location->location_postcode, 0, 2);
            $valid_codes = [ 75, 91, 92, 93, 95 ];
            if ( ! in_array( $val, $valid_codes ) ) {
                $EM_Location->add_error( __('Postcode field is not valid.','events-manager').__(" It must start with one of the following values: ".implode(', ', $valid_codes).'.', 'events-manager') );
                $is_valid = false;
            }
        }
        return is_valid;
    }
    Thread Starter julio75

    (@kingkurt)

    Thak you joneiseman for the fast answer.

    Unfortunately, when I add the function, I can now submit events even when address and city fields are not filled out.

    There might be a little mistake which disables the original validate function.

    Can’t find out where the problem is

    Here’s a corrected version:

    add_filter( 'em_location_validate_meta', 'my_em_location_validate_meta', 10, 2 );
    function my_em_location_validate_meta( $is_valid, $EM_Location ) {
         if ( empty( $EM_Location->location_postcode ) ) {
              $EM_Location->add_error( __('Postcode field','events-manager').__(" is required.", 'events-manager') );
             $is_valid = false;
        }
        else {
            $value = substr( $EM_Location->location_postcode, 0, 2);
            $valid_codes = [ "75", "91", "92", "93", "95" ];
            if ( ! in_array( $value, $valid_codes ) ) {
                $EM_Location->add_error( __('Postcode field ','events-manager').$EM_Location->location_postcode. __(' is not valid.','events-manager').__(" Postcode must start with one of the following values ",'events-manager').implode(', ', $valid_codes).'.' );
                $is_valid = false;
            }
        }
        return is_valid;
    }

    I saw one other error in my code snippet. Here’s the corrected version:

    add_filter( 'em_location_validate_meta', 'my_em_location_validate_meta', 10, 2 );
    function my_em_location_validate_meta( $is_valid, $EM_Location ) {
         if ( empty( $EM_Location->location_postcode ) ) {
              $EM_Location->add_error( __('Postcode field','events-manager').__(" is required.", 'events-manager') );
             $is_valid = false;
        }
        else {
            $value = substr( $EM_Location->location_postcode, 0, 2);
            $valid_codes = [ "75", "91", "92", "93", "95" ];
            if ( ! in_array( $value, $valid_codes ) ) {
                $EM_Location->add_error( __('Postcode field ','events-manager').$EM_Location->location_postcode. __(' is not valid.','events-manager').__(" Postcode must start with one of the following values ",'events-manager').implode(', ', $valid_codes).'.' );
                $is_valid = false;
            }
        }
        return $is_valid;
    }
    Thread Starter julio75

    (@kingkurt)

    GREAT ! Works fine now. Thank you very much.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Limit the submission over form via postcode to certain regions ?’ is closed to new replies.