• Hi, I would like to make the image upload a required field in the event submission form. I.e. when a user trying to submit an event without uploading image, a validation message prompted: “You must upload an image for your event.”

    There seems not a straight forward way to achieve this.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You can use the em_event_validate filter.

    add_filter( 'em_event_validate', 'my_em_event_validate', 10, 2);
    function my_em_event_validate( $is_valid, $EM_Event ) {
    if ( $EM_Event->get_image_url() == '' ) {
    $is_valid = false;
    $EM_Event->add_error(__('You must provide a featured image.', 'events-manager'));
    }
    return $is_valid;
    }
    Thread Starter enkoes

    (@enkoes)

    Thanks for your help.

    I tested but the filter doesn’t work. Even though image is uploaded, the form submission is blocked by displaying the error message “You must provide a featured image.” I guess the image hasn’t been saved or attached as a featured image by the time validation runs.

    joneiseman

    (@joneiseman)

    Use the following code snippet instead to make the image upload a required field:

    add_action('em_uploads_uploader_init', function() {
    EM\Uploads\Uploader::$default_options['required'] = true;
    } );
    Thread Starter enkoes

    (@enkoes)

    Hi did a brief test after activated your code snippet. When no image is uploaded, the form can still be submitted without validation.

    joneiseman

    (@joneiseman)

    Interesting, it worked for me. Did you enable the visual uploader?

    • This reply was modified 1 week ago by joneiseman.
    Thread Starter enkoes

    (@enkoes)

    Yes. I tested both (with visual uploader enable & disable), but didn’t help. User still can submit event without the image.

    joneiseman

    (@joneiseman)

    Well, like I said, it worked for me. Maybe someone else will be able to help you with this.

    Thread Starter enkoes

    (@enkoes)

    After spending some time into it, still can’t figure out why your code doesn’t work for me.

    As an alternative, I use the following JS to make the image upload field required successfully. In case someone needs this:

    document.addEventListener('DOMContentLoaded', function() {
    var fileInput = document.querySelector('input[type="file"][name="event_image"]');
    if (fileInput) {
    fileInput.required = true;
    }
    });

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.