reduxframework / redux-framework-4

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

Custom validation error messages are not removed once field passes validation #196

Closed sjregan closed 3 years ago

sjregan commented 3 years ago

Support Hash

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

BTW Generate support hash URLs are 404s :)

Steps to reproduce

  1. Add a field 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)

Example validation callback function:

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

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

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

    return [
        'error' => $field,
        'value' => trim( $existing_value ),
    ];
}
kprovance commented 3 years ago

I changed one of the textarea fields of the sample config to this:

array(
    'id'       => 'opt-textarea-no-html',
    'type'     => 'textarea',
    '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"}',
),

And used this function (which I changed to use actual arrays instead of the short array syntax, which is legacy and not part of the WP Coding Standards).

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 ),
    );
}

All worked as intended.

Save with bad JSON image

Save with corrected JSON image

The problem may lay within your execution. It does not appear to be Redux. Good luck!