0

i develop a custom field and have some errors on property #states.

The method of using #states in $element is quiet different with the $form.

The code i write is below.

 function cellphonefield_field_widget_form(&$form,&$form_state,$field,$instance,$langcode,$items,$delta,$element)
    {
        $base=$element;

        $element['cellphone']=array(
         '#type' => 'textfield',
         '#title'=>t('Enter your Cell phone Number!'),
         '#description'=>t('System will send you a message '),
         '#default_value'=>isset($items[$delta]['cellphone'])?$items[$delta]['cellphone']:'test for this field',
         '#weight'=>0,
        )+$base;

      $element['send_sms']=array(
         '#type' => 'button',
         '#name'=>'send_sms',
         '#title' => t('validate your cellphone number'),
         '#weight'=>1,
         '#value'=>t('Send the validation code!'),
         '#ajax'=>array(
            'callback'=>'ajax_send_sms_callback_function',
            'wrapper'=>'replace_markup_to_textfield',
            'effect'=>'fade',
            'speed'=>'fast',
            'method'=>'replace',
         ),
         '#states'=>array(
            'disable'=>array(
              ':input[name="profile_main[field_cellphone][und][0][cellphone]"]'=>array('filled'=>TRUE),   
            ),
         ),
        );

    $element['validation_fieldset']=array(
    '#type'=>'markup',
    '#weight'=>2,
    '#prefix'=>'<div id="replace_markup_to_textfield">',
    '#suffix'=>'</div>',
  );

   return $element;
}

No matter what i replace the input name of the #states, it's still not working.

i checked the html form and the code blow

<input type="text" id="edit-profile-main-field-cellphone-und-0-cellphone" name="profile_main[field_cellphone][und][0][cellphone]" value="" size="60" maxlength="128" class="form-text" />

what could the problem be?

0

Did you tried with the input id instead the name?:

'#states'=>array(
  'disable'=>array(
    '#edit-profile-main-field-cellphone-und-0-cellphone'=>array('filled'=>TRUE),   
  ),
),
0

drupal_process_states() takes the parameter $elements and contains the example:

array(
  'visible' => array(
    ':input[name="remote_checkbox"]' => array('checked' => TRUE),
  ),
)

Your example is very similar:

array(
  'disable'=>array(
    ':input[name="profile_main[field_cellphone][und][0][cellphone]"]'=>array('filled'=>TRUE),   
   ),
 )

But I noticed that 'visible' is an adjective while 'disable' is a verb so I checked the list of available states on the drupal_process_states() documentation and found:

The following states may be applied to an element:

  • enabled
  • disabled
  • required
  • optional
  • visible
  • invisible
  • checked
  • unchecked
  • expanded
  • collapsed

So it looks like your problem boils down to a missing letter 'd' on 'disabled'.

0

I created a function for when I need to use #states in hook_field_widget_form. It looks like this:

function jquery_selector($field_name,$langcode,$delta,$item_name){
  $jquery_selector = '\':input[name="' . $field_name . '[' . implode('][',
    array($langcode,$delta,$item_name)) . ']"]\'';
    return $jquery_selector;
 }

In your hook_field_widget_form implementation you can use it like this:

'#states'=>array(
        'disable'=>array(
          jquery_selector($instance['field_name'], $langcode, $delta,
          'cellphone')=>array('filled'=>TRUE),   
        ),
     ),
  • Hello and welcome. Your code looks good, thanks for sharing. Could you maybe add a bit of description to it? How does it work, why this way, etc? If you will, readers may learn to follow your logic and solve similar probems on their own in the future. – Mołot Sep 20 '14 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.