reduxframework / redux-framework-4

Redux v4
https://redux.io/redux-4/
Other
97 stars 32 forks source link

Custom validation error messages for 'ace_editor' fields are not removed once field passes validation #237

Closed sjregan closed 2 years ago

sjregan commented 2 years ago

This is a continuation of issue #196 .

Support Hash

https://support.redux.io/?id=yDWdaYTYRxDxPQglhZqGQ0HDgyezD78hUHJ0KylPK6nPQKD9GxbP%2FGGGkHSDxSCQ%2F8RAFZxGx5h8hWPZ2tr3ug%3D%3D

Steps to reproduce

  1. Add a field of type 'ace_editor' with custom validation using the validate_callback option.
  2. Write custom validation function.
  3. Open redux settings and enter a value into field that will cause validation to fail. Click 'Save Changes'
  4. Enter a value into field that will pass validation. Click 'Save Changes'

Expected Behavior

The validation error message to be removed.

Actual Behavior

The validation error message remains on screen even though the field has passed validation and been saved.

Any Error Details (PHP/JavaScript)

Modifications to sample config:

array(
    'id'       => 'opt-textarea-no-html',
    'type'     => 'ace_editor',
    'title'    => esc_html__( 'Textarea Option - No HTML Validated', 'your-textdomain-here' ),
    'subtitle' => esc_html__( 'All HTML will be stripped', 'your-textdomain-here' ),
    'desc'     => esc_html__( 'This is the description field, again good for additional info.', 'your-textdomain-here' ),
    'validate_callback' => 'validate_json',
    'default'  => '{"opt-slider-label":"1", "opt-slider-text":"10"}',
),

Validation function:

function validate_json( $field, $value, $existing_value ) {
    $json = json_decode( $value );

    if ( json_last_error() === JSON_ERROR_NONE ) {
        return array( 'value' => trim( $value ) );
    }

    $field['msg'] = esc_html__( 'You must enter valid JSON.', 'text-domain' );

    return array(
        'error' => $field,
        'value' => trim( $existing_value ),
    );
}
kprovance commented 2 years ago

In the future, please use the issue tracker here: (https://github.com/reduxframework/redux-framework). This beta repo is closed. I'll ask to have it taken down.

Change your validation function to this:

function validate_json( $field, $value, $existing_value ) {
    $json = json_decode( $value );

    if ( json_last_error() === JSON_ERROR_NONE ) {
        $field['msg'] = '';
        return array( 
            'value' => trim( $value ), 
            'error' => $field 
        );
    }

    $field['msg'] = esc_html__( 'You must enter valid JSON.', 'text-domain' );

    return array(
        'error' => $field,
        'value' => trim( $existing_value ),
    );
}

For custom validations, you'll have to pass a null or empty value to the field error and return it to make the message disappear on success.

sjregan commented 2 years ago

Thanks for your response, I've opened a new issue in the other repo as the issue is JavaScript related, not PHP. So, while the validate_json() function was incorrect, it is unrelated to the bug.