Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 700 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal 7 - input form readonly and apply style

#1
I need to apply style and read-only property to an input element of a drupal form.
I coded 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 at `'#attributes' => array($inputAtributo => 1),`

Before create form, I check if this input should be readonly 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's not coded 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 I can do this better?
Reply

#2
`#attributes` is not intented to be used with styles. You must provide an array with key and values reproducing html attributes. And class and css is better than adding style directly into html.

<!-- language: lang-php -->

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

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

<!-- language: lang-php -->

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

Note that readonly attribute has also "readonly" as value, so it's [xhtml compliant](

[To see links please register here]

).

Now add a css rule in your stylesheet:

<!-- language: lang-css -->

.readonly-input { background: none repeat scroll 0 0 #EAEAEA; }
Reply

#3
`#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;'
);
Reply

#4
The other answers are correct. Instead of using _readonly_, I would rather use [#disabled][1]. Also, if the form field is read-only, or disabled, #required is not necessary, since the 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 textfield, I would rather use a [markup][2] form field, if the value needs just to be shown, and not edited.

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





[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
To make our input field as read-only in drupal form, set the value **TRUE** to **readonly** attribute.

*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
)
);

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through