0

I have a form built using the wp-forms plugin and I'm using the wpforms_process hook to trigger an add_action that validates the data in one of my form fields.

add_action( 'wpforms_process', 'check_isbns', 0, 4 );

The code in the check_isbns function uses code similar to the following to check my form field and if the check fails it displays an error under the field.

$value_to_check = true;

    if ( ! $value_to_check ) {
        // Add to global errors. This will stop form entry from being saved to the database.
        // Error id displayed above the form.
        wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );

        // Uncomment the line below if you need to display the error for a specific field. 
        // wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = esc_html__( 'Some error occurred.', 'plugin-domain' );

        // Return early, so the code below won't fire.
        return;
    } 

This code is working and I see the error message when my data validation fails, but the field is a text field that must contain a list of numbers where each number must be on a seperate line. After the validation has run and the form is redisplayed with the validation message, all of the \r\n line breaks have been stripped out of the data in the field, so it cannot easily be corrected and resubmitted. All the line breaks have to be manually re entered.

What I have before validation is like this before validation

And after my validation has run it looks like this enter image description here

How can I stop this from happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.