Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Drupal 7-输入表单只读和应用样式_Drupal_Drupal 7_Drupal Fapi - Fatal编程技术网

Drupal 7-输入表单只读和应用样式

Drupal 7-输入表单只读和应用样式,drupal,drupal-7,drupal-fapi,Drupal,Drupal 7,Drupal Fapi,我需要将样式和只读属性应用于drupal表单的输入元素。 我编码如下: $form['precio'] = array( '#type' => 'textfield', '#title' => t('Precio'), '#default_value' => ''.$precio, '#size' => 20, '#required' => TRUE, '#attributes' => array($inputAtributo =>

我需要将样式和只读属性应用于drupal表单的输入元素。 我编码如下:

$form['precio'] = array(
  '#type' => 'textfield',
  '#title' => t('Precio'),
  '#default_value' => ''.$precio,
  '#size' => 20,
  '#required' => TRUE,
  '#attributes' => array($inputAtributo => 1),
  '#description' => t('Modifica el precio '),
);
<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="">
'#attributes'=>数组($InputTributo=>1)中,

在创建表单之前,我检查此输入是否应为只读,并应用一些样式:

if ($tarifa !='' & $tarifa=='N')
$inputAtributo=" readonly style=\"background: none repeat scroll 0 0 #EAEAEA;\" ";
else
$inputAtributo = "";
这是可行的,但我认为它的编码不正确

html代码显示以下内容:

$form['precio'] = array(
  '#type' => 'textfield',
  '#title' => t('Precio'),
  '#default_value' => ''.$precio,
  '#size' => 20,
  '#required' => TRUE,
  '#attributes' => array($inputAtributo => 1),
  '#description' => t('Modifica el precio '),
);
<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="">

我如何才能做得更好?

#属性
不打算与样式一起使用。您必须提供一个数组,其中包含重现html属性的键值。类和css比直接在html中添加样式要好

“#属性”=数组(
'class'=>array('readonly-input'),
“只读”=>“只读”,
)
如果要将其添加到If中,可以执行以下操作:

if($tarifa!=''&$tarifa=='N'){
$form['precio']['#attributes']['class'][]='readonly input';
$form['precio']['#attributes']['readonly']='readonly';
}
请注意,readonly属性也有“readonly”作为值,所以它是

现在在样式表中添加css规则:

.readonly输入{背景:无重复滚动0#EAEAEA;}
#属性
必须是键值对数组

所以数组应该是这样的

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

其他答案是正确的。我宁愿使用readonly而不是readonly。此外,如果表单字段为只读或禁用,则不需要#required,因为用户无法更改该值

$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'),
);
如果只需要显示值而不需要编辑,我宁愿使用表单字段,而不是使用文本字段

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

要将输入字段设置为drupal格式的只读字段,请将值TRUE设置为readonly属性

比如说,


作为旁注,传递给
t()
的字符串必须是英文的。这很有效,但是类和css更好。同意,但在某些任务中可能需要内联样式。如果你看到所有的div都包含内联样式,谢谢!我最后使用了css类,怎么可能不将css内联,仍然是主题无关的呢?嗨,我更喜欢使用只读,因为输入框是只读的,但我有另一个控件来修改它们。