Plusieurs valeurs pour déclencher #states


18

Comment puis-je avoir plusieurs valeurs pour déclencher les # états de l'API Form?

Disons par exemple, je voulais que ce champ soit visible non seulement si la valeur est 5 (fonctionne actuellement ci-dessous), mais je voulais rendre le champ visible si les valeurs sont 3, 4 ou 5.

'#states' => array(
    'visible' => array(
       ':input[name="field_star_rating"]' => array('value' => t('5')),
    ),
),

En tant que note, j'ai essayé ce qui suit et cela ne fonctionne pas . Cela ne fonctionne que si la valeur est '4'

'#states' => array(
    'visible' => array(
        ':input[name="field_star_rating"]' => array('value' => t('5')),
        ':input[name="field_star_rating"]' => array('value' => t('4')),
    ),
),

Cela ne fonctionne pas non plus , cela ne fonctionne que si la valeur est '4':

'#states' => array(
    'visible' => array(
        ':input[name="field_star_rating"]' => array('value' => t('5'), 'value' => t('4')),
    ),
),

Réponses:


39

Voici ce dont vous avez besoin:

'#states' => array(
    'visible' => array(
        ':input[name="field_star_rating"]' => array(
            array('value' => t('5')),
            array('value' => t('4'))
        ),
    ),
),

C'est en effet la bonne façon, celle actuelle marquée comme correcte est fausse. Consultez ce numéro pour plus d'informations: drupal.org/node/735528
— Robin

L'API #states a certainement fait du chemin depuis 2011. Marquage comme correct.
— Citricguy

c'est une excellente réponse, et je suis venu ici de Google, comme une douzaine de fois ... une prime est en route.
— AyeshK

Cela fonctionne bien pour une vue normale. Après avoir exécuté n'importe quel 'ajax', il a ajouté encore et encore
— Guru

3

La seule façon de comprendre est d'utiliser #ajax en D7.

Voici quelques conseils utiles que j'aurais aimé savoir avant de commencer.

  1. #ajax dans le formulaire API est génial et mérite d'être étudié
  2. #states ne prend pas en charge OR ou XOR (sans correctif? http://drupal.org/node/735528 )
  3. dpm ($ form); et var_dump ($ form_state) sur une fonction d'envoi personnalisée sont inestimables

Voici une version modifiée d'un des exemples AJAX du module d'exemples.

function plugin_autotextfields($form, &$form_state) {

    $form['star_rating'] = array(
        '#type' => 'select',
        '#title' => t('Star Rating'),
        '#options' => array('_none' => '- select -', 5 => '5 Star', 4 => '4 Star', 3 => '3 Star', 2 => '2 Star', 1 => '1 Star'),
        '#ajax' => array(
            'callback' => 'plugin_autotextfields_callback',
            'wrapper' => 'textfields',
            'effect' => 'fade',
        ),
    );

    $form['textfields'] = array(
        '#title' => t("Fieldset Name"),
        '#prefix' => '<div id="textfields">',
        '#suffix' => '</div>',
        '#type' => 'fieldset',
        '#description' => t('Where the field will be placed'),
    );

    if (!empty($form_state['values']['star_rating']) && $form_state['values']['star_rating'] == 5) {
        $form['textfields']['review'] = array(
            '#type' => 'textfield',
            '#title' => t('Message if 5 stars'),
        );
    } else if (!empty($form_state['values']['star_rating'])) {
        $form['textfields']['review'] = array(
            '#type' => 'textfield',
            '#title' => t('Message if not 5 stars'),
        );
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Click Me'),
    );

    return $form;
}

function omfg_autotextfields_callback($form, $form_state) {
    return $form['textfields'];
}

J'espère que cela aide quelqu'un qui rencontre le même problème :)


Hou la la! Réponse très utile mon ami. J'ai enveloppé ma tête autour d'un problème avec #states et maintenant ça marche, mais #ajax serait évidemment plus facile maintenant que vous m'avez giflé avec un indice. Et cette astuce de débogage en bonus? Désolé, je dois vous payer avec un maigre karma. ;)
— stefgosselin

3
 $form['student_type'] = array(
    '#type' => 'checkboxes',
    '#options' => array(
      'high_school'   => t('High School'),
      'undergraduate' => t('Undergraduate'),
      'graduate'      => t('Graduate'),
    ),
    '#title' => t('What type of student are you?')
  );

// High school information.
  $form['high_school']['tests_taken'] = array(
    '#type' => 'textfield',
    '#title' => t('What standardized tests did you take?'),
    '#states' => array(
      'visible' => array(   // action to take.
        ':input[name="student_type[high_school]"]' => array('checked' => TRUE),
        ':input[name="student_type[undergraduate]"]' => array('checked' => TRUE),
        ':input[name="student_type[graduate]"]' => array('checked' => FALSE),
      ),
    ),
  );

PS Voir le module d'exemples pour plus de fonctionnalités "form_example / form_example_states.inc"

En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.