Organizer can’t be set as required field

Home Forums Calendar Products Community Events Organizer can’t be set as required field

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #1137543
    Sean
    Participant

    Hi,

    I wanted to check in on the status of this issue that prohibits us from setting any of the Organizer fields as required (as noted in this ticket). Is there a fix/snippet available that will allow events to be submitted when the Organizer is set as required? If not, when can we expect this problem to be addressed?

    Thank you,
    Karly

    #1138227
    Nico
    Member

    Hi Karly,

    Thanks for reaching out! I took a look to the related thread and George’s reply as well. He still doesn’t have much to add, but I can try to see if there’s a workaround for this situation.

    Can you please describe what you are looking for? Do you want the event to have at least 1 organizer? or to validate the fields in the organizer?

    Please let me know and I’ll give this a shot,
    Best,
    Nico

    #1138433
    Jesse Simmers
    Participant

    I would like to require at least 1 organizer with a name and email address. Both fields should be required and if left empty a message will be displayed upon submission.

    #1138500
    Sean
    Participant

    Hi Nico,

    Thanks for seeing if there is a solution to this problem. It looks like Jesse is looking for the same requirements as I am: at least one Organizer is required (Organizer Name and Organizer Email Address are both required). If either field is left blank, an error message is returned (we’re already customizing the required fields and error messages as outlined in this TEC snippet).

    Thank you,
    Karly

    #1139700
    Nico
    Member

    Hey Jesse and Karly,

    Thanks for the patience while I looked into this! As George stated in his reply it’s not a simple issue for sure, but hopefully I could find a workaround.

    First of all please remove ‘organizer’ from the snippet here if you are using it. Once that’s removed or commented out paste this new snippet in your theme (or child theme) functions.php file:


    /* Check for a valid Organizer (a saved organizer or a new one with name + email) */
    add_filter( 'tribe_community_events_form_errors', 'ce_custom_error_msg' );

    function ce_custom_error_msg( $errors ) {

    // bail if no post
    if ( empty( $_POST ) ) return $errors;

    // check for at least 1 valid organizer
    $valid_organizer = false;

    $cant_organizers = count ( $_POST['organizer']['OrganizerID'] );

    for ( $i = 0; $i < $cant_organizers; $i++ ) {

    // saved organizer
    if ( !empty( $_POST['organizer']['OrganizerID'][$i] ) ) {
    $valid_organizer = true;
    break;
    }

    // organizer has name and email
    if ( !empty( $_POST['organizer']['Organizer'][$i] ) && !empty( $_POST['organizer']['Email'][$i] ) ) {
    $valid_organizer = true;
    break;
    }

    }

    if ( $valid_organizer ) return $errors;

    // add organizer error message
    $existing_errors = '';
    $type = 'error';

    if ( is_array( $errors ) ) {
    $existing_errors = $errors[0]['message'];
    $type = $errors[0]['type'];
    }

    $errors[0] = array(
    'type' => $type,
    'message' => $existing_errors . '<p>Event Organizer is required</p>'
    );

    return $errors;
    }

    I gave this a go locally and it works as expected but be sure to test this extensively before using it on a live site. I’ll also make sure this is logged as a bug and assosiate the internal ticket with this thread to give you a heads up when it’s fixed.

    Please let me know if this works as expected,
    Best,
    Nico

    #1139830
    Jesse Simmers
    Participant

    Thank you, Nico!

    We are getting closer… For testing I removed all of my custom functions except what you provided above and disabled all of my custom templates. Running latest of all plugins.

    If I leave name and email blank I get the message ‘Event Organizer is required’, perfect! But if I fill in name OR email (not both) then the form submits and proceeds to the confirmation page. Then on the confirmation page it says ‘Event Organizer is required’ below ‘Event submitted’. If I fill in both name and email and submit and event then it will proceed to the confirmation page and the message ‘Event Organizer is required’ is NOT shown.

    So it looks like the confirmation page knows both fields should be filled out but the form is only requiring one field to proceed.

    #1140070
    Sean
    Participant

    Thanks for putting this together Nico!

    I gave this a try on my staging site. Twentysixteen theme with your snippet added to the other related “required fields” snippets offered by Modern Tribe on this site (see below for the full code)

    In my testing, I found that while the message “Event Organizer is required” is displayed on the confirmation page beneath “Submit another event”, the form does not return an error message (and block the event from being submitted successfully) unless one of the other required fields is left blank (event name, website url, etc).

    Here’s the full code I have added to the default Twentysixteen theme, with all TEC plugins updated to the latest versions:

    *
     * -----------------------------------------------------
     *     TEC: Customize the List of Required Fields on
     *     the Community Events Submit an Event Form
     * -----------------------------------------------------
     *
     */
    add_filter( 'tribe_events_community_required_fields', 'my_community_required_fields', 10, 1 );
    
    function my_community_required_fields( $fields ) {
        $fields = array(
            'post_title',
    		'EventStartDate',
    		'EventStartHour',
    		'EventStartMinute',
    		'EventStartMeridian',
    		'EventURL',
    		'venue',
        );
    
        return $fields;
    
    }
    
    /*
     * -----------------------------------------------------
        Requiring the organizer email (or other "square bracketed"
        fields) can't be done using the above approach, but we can
        simply test within the $_POST superglobal directly.
    
        If a field hasn't been populated then we simply add a requirement
        using a field name we know doesn't exist, but can be used to form
        a meaningful error message (if the org email is missing the user
        will see "Organizer Email is required").
    
        You could take this a step further and additionally validate the
        email field to ensure it looks like a valid email address and
        provide an alternative error using the same approach.
     * -----------------------------------------------------
     *
     */
    
    add_filter( 'tribe_events_community_required_fields', 'tribe_community_required_fields_venue_city', 10, 1 );
    
    function tribe_community_required_fields_venue_city( $fields ) {
        if ( !empty($_POST['venue']["VenueID"]) )
            return $fields;
    
        if ( empty( $_POST['venue']['Address'] ) )
            $fields[] = 'Venue Address';
    
        if ( empty( $_POST['venue']['City'] ) )
            $fields[] = 'Venue City';
    
        if ( empty( $_POST['venue']['State'] ) )
            $fields[] = 'Venue State';
    
        if ( empty( $_POST['venue']['Zip'] ) )
            $fields[] = 'Venue Zip Code';
    
        return $fields;
    }
    
    // Customize Required Fields Error Message
    function ce_custom_error_msg( $errors ) {
    // Don't filter if it is an 'update' or other type of message
    if ( $errors[0]['type'] != 'error' ) return $errors;
    
    $existing_errors = '';
    $type = 'error';
    
    if ( is_array( $errors ) ) {
    $existing_errors = $errors[0]['message'];
    $type = $errors[0]['type'];
    }
    
    // Set overall message by appending a heading to the front
    if ( strpos($_SERVER['REQUEST_URI'], '/events/community/edit/') !== 0 ) {
    // You are not editing an event
    $errors[0] = array(
    'type' => $type,
    'message' => '<strong>The following fields are required:</strong>' . $existing_errors
    );
    }
    
    if ( strpos($_SERVER['REQUEST_URI'], '/events/community/edit/') === 0 ) {
    // You are editing an event
    $errors[0] = array(
    'type' => $type,
    'message' => '' . $existing_errors
    );
    }
    
    // User str_replace to choose a specific message to change
    $errors[0]['message'] = str_replace( 'Tax Input is required', 'Event Category is required', $errors[0]['message'] );
    $errors[0]['message'] = str_replace( 'Venue is required', 'Venue Name is required', $errors[0]['message'] );
    
    return $errors;
    }
    
    // Return custom error message
    add_filter( 'tribe_community_events_form_errors', 'ce_redirect_after_submit', 10, 1 );
    function ce_redirect_after_submit( $messages ) {
    if ( is_array( $messages ) && !empty( $messages ) ) {
    $messages = ce_custom_error_msg( $messages );
    $first_message = reset( $messages );
    if ( $first_message['type'] == 'update' ) {
    add_action( 'parse_request', 'tribe_redirect_after_community_submission', 11, 1 );
    }
    }
    return $messages;
    }
    
    /* Check for a valid Organizer (a saved organizer or a new one with name + email) */
    add_filter( 'tribe_community_events_form_errors', 'ce_custom_error_msgtwo' );
      
    function ce_custom_error_msgtwo( $errors ) {
     
        // bail if no post
        if ( empty( $_POST ) ) return $errors;
     
        // check for at least 1 valid organizer
        $valid_organizer = false;
     
        $cant_organizers = count ( $_POST['organizer']['OrganizerID'] );                
     
        for ( $i = 0; $i < $cant_organizers; $i++ ) {
     
            // saved organizer
            if ( !empty( $_POST['organizer']['OrganizerID'][$i] ) ) {
                $valid_organizer = true;
                break;
            }
     
            // organizer has name and email
            if ( !empty( $_POST['organizer']['Organizer'][$i] ) && !empty( $_POST['organizer']['Email'][$i] ) ) {
                $valid_organizer = true;
                break;
            }
     
        }
     
        if ( $valid_organizer ) return $errors;
          
         // add organizer error message
        $existing_errors = '';
        $type = 'error';
      
        if ( is_array( $errors ) ) {
            $existing_errors = $errors[0]['message'];
            $type = $errors[0]['type'];
        }
      
        $errors[0] = array(
            'type' => $type,
            'message' => $existing_errors . '<p>Event Organizer is required</p>' 
        );
      
        return $errors;
    }
    #1141275
    Nico
    Member

    Thanks so much for the detailed feedback! I’m a bit out of time today but I’ll give this a spin tomorrow 🙂

    Best,
    Nico

    #1143234
    Nico
    Member

    Hey Jesse and Karly,

    Thanks so much for the patience and sorry for the delay on my reply! I think I could correct the snippet and as far as I could test it works 🙂

    I changed a bit how it works, just delete the previous code I sent and try this one instead:

    /* helper function to see if there's at least one valid organizer */
    function tribe_valid_organizers ( ) {

    // check for at least 1 valid organizer
    $valid_organizer = false;

    // bail if post not set
    if ( !isset($_POST) || !isset($_POST['organizer']) ) return $valid_organizer;

    $cant_organizers = count ( $_POST['organizer']['OrganizerID'] );

    for ( $i = 0; $i < $cant_organizers; $i++ ) {

    // saved organizer
    if ( !empty( $_POST['organizer']['OrganizerID'][$i] ) ) {
    $valid_organizer = true;
    break;
    }

    // organizer has name and email
    if ( !empty( $_POST['organizer']['Organizer'][$i] ) && !empty( $_POST['organizer']['Email'][$i] ) ) {
    $valid_organizer = true;
    break;
    }

    }

    return $valid_organizer;
    }

    /* define organizer as required */
    add_filter( 'tribe_events_community_required_fields', 'my_community_required_fields', 10, 1 );

    function my_community_required_fields( $fields ) {

    $fields = array(
    'post_title',
    'post_content'
    );

    /* make organizer required */
    if ( !tribe_valid_organizers() ) {
    $fields[] = 'organizer';
    }

    return $fields;
    }

    Be sure to change the fields array to whatever fields are required in your site!

    Please give it a go and let me know about it,
    Have a great weekend,
    Nico

    #1143565
    Sean
    Participant

    Hi Nico,

    Hope you had a great weekend and thanks for your continued help. I just tested this and found that while the Organizer Name field was required and correctly returns the error message, the Organizer email address field is not required.

    Best regards,
    Karly

    #1143772
    Nico
    Member

    Thanks for following up Karly!

    I just did a quick test on my local install and the form is validating that the organizer name and email are mandatory. if you select a saved organizer then no further validation is required.

    This is the test I’m running, maybe you notice something different from what you are testing: https://cloudup.com/c_GjJ2RUUgj

    Please let me know about it,
    Best,
    Nico

    #1143964
    Sean
    Participant

    Hi Nico,

    Okay, this is working nicely now! I had forgotten to update to the latest version of the plugins after copying my production site over to a test site. With all of TEC plugins up to last week’s release, I am seeing the correct error message returned if the Organizer Name and/or Organizer Email are blank. Thank you so much for putting this snippet together for us!

    On a related note, I noticed on the latest versions, if the Venue is set to be required (either by adding ‘venue’ to the array or by setting each individual venue field to be required via a filter), no error message is returned if the fields are left blank (even though it says “(REQUIRED)” next to the Venue Details heading). Are you able to reproduce this issue on your end? If so, I’m happy to start another topic for it.

    Thanks and have a great day!
    Karly

    #1144876
    Nico
    Member

    Thanks for following Karly! Glad to see the workaround works for you 🙂

    I’ll check on the venue validation and will give you a heads-up about it tomorrow!

    Thanks,
    Nico

    #1145633
    Nico
    Member

    Hey Karly,

    I’m following up to inform you I was out yesterday and will also be out today, so I’ll review this on Monday. Sorry for the delay!

    Have a great weekend,
    Nico

    #1145655
    Sean
    Participant

    No worries Nico. Enjoy your weekend as well!

    Karly

Viewing 15 posts - 1 through 15 (of 18 total)
  • The topic ‘Organizer can’t be set as required field’ is closed to new replies.