alleyinteractive / alleyinteractive/wordpress-fieldmanager

Conditional visibility but unconditional validation

Ouverte
#846 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
PHP
Étoiles
563
Forks
99
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

Consider the following code, in which the `manual` option of `ip_option` is used to conditionally display `manual_ip` which is a text box to allow the user to enter an IP address:
```
'children' => array(
'ip_presets' => new \Fieldmanager_Group(
array(
'label' => 'Use Presets',
'description' => '

These allow you to force IP addresses using presets for in- and out-of-market.

',
'children' => array(
'ip_option' => new \Fieldmanager_Radios(
array(
'options' => array(
'default' => 'Use the IP address calculated by the system',
'in_market' => 'Force In-Market (Portland, ME: 154.6.12.147)',
'out_of_market' => 'Force Out-of-Market (Chicago, Il: 181.214.165.13)',
'manual' => 'Enter an IP address to use',
),
)
),
'manual_ip' => new \Fieldmanager_TextField(
'Enter IP address',
array(
'display_if' => array(
'src' => 'ip_option',
'value' => 'manual',
),
'attributes' => array(
'size' => 25,
),
// Validation functions must return true or false.
'validate' => array(
function( $value ) {
return filter_var( sanitize_text_field( $value ), FILTER_VALIDATE_IP );
},
),
// Sanitization functions must return true or false.
'sanitize' => fn( $value ) => filter_var( sanitize_text_field( $value ), FILTER_VALIDATE_IP ),
)
),
),
'escape' => array(
'description' => 'wp_kses_post',
),
'description_after_element' => false,
),
),

```

If the user selects the manual option and enters a invalid IP address, you get the warning as expected:

[![Screen-Shot-2022-10-02-at-11-58-58-AM.jpg](https://i.postimg.cc/zBFxyBJn/Screen-Shot-2022-10-02-at-11-58-58-AM.jpg)](https://postimg.cc/Q9VQPjKt)

Now the user hits the back button, and selects one of the other options (which hides the text entry) and saves...and they get the warning again. This is because TextField `validate` function is run even when the `display_if` is false.

So here are my questions
1. assuming this is by design, how can the validation be conditional too?
2. if a user selects one of the other radio items, is there a way to clear the value in `manual_ip` in the UX?
3. if a user clears the value if `manual_ip`, and saves, is there a way to set `ip_option` to `default`?

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.