codestar-framework icon indicating copy to clipboard operation
codestar-framework copied to clipboard

Make field required only if a dependency is true

Open UVLabs opened this issue 2 years ago • 1 comments

I have a switchers that when set to the on position shows other fields, but those other fields have validation set on them such as being required.

If the settings are saved, while the switcher is still in it's off position, then the validation is still fired.

An example is:

array(
'id'       =>  'test_setting_id',
'type'     => 'switcher',
'title'    => 'Enable',
'text_on'  => __( 'Yes' ),
'text_off' => __( 'No' ),
'default'  => true,
),
array(
'id'         => 'test_input_field_id'
'type'       => 'text',
'title'      => 'Enter some text',
'dependency' => array( "test_setting_id", '==', 'true' ),
'validate'   => 'csf_validate_required',
),

In the example above, if the switcher is left in it's off position and the settings are saved, there will be a red exclamation mark shown on the setting tab:

image

UVLabs avatar Jan 12 '23 19:01 UVLabs