Drupal 7 - an input form for reading and applying style

I need to apply the style and read-only property to the input element of the drupal form. I encoded the following:

$form['precio'] = array( '#type' => 'textfield', '#title' => t('Precio'), '#default_value' => ''.$precio, '#size' => 20, '#required' => TRUE, '#attributes' => array($inputAtributo => 1), '#description' => t('Modifica el precio '), ); 

And with '#attributes' => array($inputAtributo => 1),

Before creating the form, I check if this input should be read-only and apply some style:

 if ($tarifa !='' & $tarifa=='N') $inputAtributo=" readonly style=\"background: none repeat scroll 0 0 #EAEAEA;\" "; else $inputAtributo = ""; 

This works, but I think it is not encoded correctly

The html code shows the following:

 <input id="edit-precio" class="form-text required" type="text" maxlength="128" size="20" value="258" name="precio" ="1"="" style="background: none repeat scroll 0 0 #EAEAEA;" readonly=""> 

How can I do it better?

+8
drupal drupal-7 drupal-fapi
source share
4 answers

#attributes must be an array of key-value pairs.

so the array should look like

 '#attributes' => array( 'readonly'=>'readonly', 'style'=>'background: none repeat scroll 0 0 #EAEAEA;' ); 
+13
source share

#attributes not intended for use with styles. You must provide an array with the key and values ​​reproducing the html attributes. And class and CSS are better than adding style directly to html.

 '#attributes' = array( 'class' => array('readonly-input'), 'readonly' => 'readonly', ) 

If you want to add it to if, you can do this:

 if ($tarifa !='' & $tarifa=='N') { $form['precio']['#attributes']['class'][] = 'readonly-input'; $form['precio']['#attributes']['readonly'] = 'readonly'; } 

Note that the readonly attribute also has a value of "readonly", which is why it is compatible with xhtml .

Now add the CSS rule to the stylesheet:

 .readonly-input { background: none repeat scroll 0 0 #EAEAEA; } 
+7
source share

Other answers are correct. Instead of using readonly, I would rather use #disabled . Also, if the form field is read-only or disabled, #required is not required because users cannot change the value.

 $form['precio'] = array( '#type' => 'textfield', '#title' => t('Price'), '#default_value' => $precio, '#size' => 20, '#attributes' => array( 'style'=>'background: none repeat scroll 0 0 #EAEAEA;' ), '#description' => t('Change the price'), ); 

Instead of using a text field, I would rather use a markup form field if the value needs to be simply shown, not revised.

 $form['precio'] = array( '#prefix' => '<span style="background: none repeat scroll 0 0 #EAEAEA">', '#suffix' => '</span>', '#markup' => t('<strong>Price:</strong> @price', array('@price' => $precio)), ); 
0
source share

To make our input field read-only in the drupal form, set the TRUE parameter to read-only .

For example,

 $user_name = variable_get('logined_user', 'guest_user'); $form['user_name'] = array( '#type' => 'textfield', '#title' => t('User Name'), '#required' => TRUE, '#default_value' => $user_name, '#description' => t('Logined user name'), '#attributes' => array( 'readonly' => TRUE ) ); 
0
source share

All Articles