• Resolved dhaigh

    (@dhaigh)


    I’m trying to add some additional validation to the urls submitted on my registration form, and while the code below does properly prevent form submission if the urls aren’t set properly, the error messages aren’t appearing beside the appropriate fields. I’m not sure if I’ve just got the wrong hooks, if it’s a problem with the multi-step/paginated setup, or some other issue. Some tips on how to revise this to properly filter my urls would be appreciated

    add_filter( 'forminator_custom_form_submit_errors', 'custom_url_validation', 10, 3 );

    function custom_url_validation( $submit_errors, $form_id, $field_data_array ) {
    // Target specific form by ID
    $target_form_id = 140100;
    if ( $form_id != $target_form_id ) {
    return $submit_errors;
    }

    // Define the fields to validate
    $platform_url_fields = array( 'url-2', 'url-3', 'url-4', 'url-5' );

    foreach ( $platform_url_fields as $field_name ) {
    $url = '';
    foreach ( $field_data_array as $field_data ) {
    if ( $field_data['name'] === $field_name ) {
    $url = $field_data['value'];
    break; // Exit inner loop after finding the URL
    }
    }

    if ( empty( $url ) ) {
    continue; // Skip validation for empty URL fields
    }

    // Generic URL validation
    if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
    $submit_errors[ $field_name ] = __( 'Please enter a valid URL.', 'your-text-domain' );
    continue; // Skip to the next field after validation failure
    }

    // Platform-specific validation
    switch ( $field_name ) {
    case 'url-2':
    if ( strpos( $url, 'instagram.com' ) === false ) {
    $submit_errors[ $field_name ] = __( 'Please enter a valid Instagram URL.', 'your-text-domain' );
    }
    break;

    case 'url-3':
    if ( strpos( $url, 'facebook.com' ) === false ) {
    $submit_errors[ $field_name ] = __( 'Please enter a valid Facebook URL.', 'your-text-domain' );
    }
    break;

    case 'url-4':
    if ( strpos( $url, 'linkedin.com' ) === false ) {
    $submit_errors[ $field_name ] = __( 'Please enter a valid LinkedIn URL.', 'your-text-domain' );
    }
    break;

    case 'url-5':
    // Add your platform-specific logic for 'url-5'
    if ( strpos( $url, 'x.com' ) === false ) {
    $submit_errors[ $field_name ] = __( 'Please enter a valid X URL.', 'your-text-domain' );
    }
    break;

    default:
    // Optional: Handle unexpected cases
    $submit_errors[ $field_name ] = __( 'Unexpected field encountered.', 'your-text-domain' );
    break;
    }
    }

    return $submit_errors;
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Imran – WPMU DEV Support

    (@wpmudev-support9)

    Hello @dhaigh,

    I hope things are going well for you.

    If you wish to achieve the validation, an error should appear below the fields. You’ll have to replace this $submit_errors[ $field_name ] with $submit_errors[][ $field_name ] so it should work fine.

    Kindly note that further script customisation would be out of our General Support Scope; we can still try to help with hooks and filters.

    Let’s try the above suggested thing and share the outcome with us.


    Thanks & Kind Regards,
    Imran Khan

    Thread Starter dhaigh

    (@dhaigh)

    Your suggested fix worked nearly perfectly. It would have been ideal if it worked prior to submission by leveraging AJAX, or backscrolling to the page with the error through the pagination, but it works well enough for our current purposes and I count that as a win. I’ve included a simplified version of the code I used below, without some of the bells and whistles we added for logging/recording purposes on our site.

    add_filter('forminator_custom_form_submit_errors', 'custom_url_validation', 10, 3);
    function custom_url_validation($submit_errors, $form_id, $field_data_array) {

    // Target specific form by ID
    $target_form_id = 140100;

    if ($form_id != $target_form_id) {
    return $submit_errors; // Exit if it's not the targeted form
    }

    // Define the fields to validate
    $platform_url_fields = array('url-2', 'url-3', 'url-4', 'url-5');

    foreach ($platform_url_fields as $field_name) {
    $url = '';

    // Find the value of the current field in the submitted data
    foreach ($field_data_array as $field_data) {
    if (isset($field_data['name']) && $field_data['name'] === $field_name) {
    $url = $field_data['value'];
    break; // Exit inner loop after finding the URL
    }
    }

    if (empty($url)) {
    continue; // Skip validation for empty URL fields
    }

    // Generic URL validation
    if (!filter_var($url, FILTER_VALIDATE_URL)) {
    $submit_errors[$field_name] = __('Please enter a valid URL.', 'your-text-domain');
    continue; // Skip to the next field after validation failure
    }

    // Platform-specific validation
    switch ($field_name) {
    case 'url-2':
    if (strpos($url, 'instagram.com') === false) {
    $submit_errors[][$field_name] = __('Please enter a valid Instagram URL.', 'your-text-domain');
    }
    break;

    case 'url-3':
    if (strpos($url, 'facebook.com') === false) {
    $submit_errors[][$field_name] = __('Please enter a valid Facebook URL.', 'your-text-domain');
    }
    break;

    case 'url-4':
    if (strpos($url, 'linkedin.com') === false) {
    $submit_errors[][$field_name] = __('Please enter a valid LinkedIn URL.', 'your-text-domain');
    }
    break;

    case 'url-5':
    if (strpos($url, 'x.com') === false) {
    $submit_errors[][$field_name] = __('Please enter a valid X URL.', 'your-text-domain');
    }
    break;

    default:
    // Optional: Handle unexpected cases (if needed)
    break;
    }
    }

    return $submit_errors; // Return all validation errors
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Multi-step form not properly displaying errors’ is closed to new replies.