【问题标题】:Drupal 7 - input form readonly and apply styleDrupal 7 - 只读输入表单并应用样式
【发布时间】:2012-12-06 18:46:53
【问题描述】:

我需要将样式和只读属性应用于 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 '),
);

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

在创建表单之前,我会检查这个输入是否应该是只读的并应用一些样式:

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

这可行,但我认为它没有正确编码

html代码显示如下:

<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="">

我怎样才能做得更好?

【问题讨论】:

  • 作为旁注,传递给t() 的字符串必须是英文的。

标签: drupal drupal-7 drupal-fapi


【解决方案1】:

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

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

如果你想在 if 中添加它,你可以这样做:

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

注意,readonly 属性也有“readonly”作为值,所以它是xhtml compliant

现在在您的样式表中添加一个 css 规则:

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

【讨论】:

    【解决方案2】:

    #attributes 必须是键值对数组。

    所以数组应该是这样的

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

    【讨论】:

    • 这个可行,但是inline style is evil,class 和 css 更好。
    • 同意,但在某些任务中您可能需要内联样式。如果你看到pinterest.com,几乎所有的 div 都包含内联样式。
    • 谢谢!我终于使用了 css 类
    • 如何不将 css 内联并仍然与主题无关?
    【解决方案3】:

    其他答案都是正确的。我宁愿使用#disabled,而不是使用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'),
    );
    

    如果值只需要显示而不是编辑,我宁愿使用markup 表单字段,而不是使用文本字段。

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

    【讨论】:

    • 你好,我更喜欢使用只读,因为输入框是只读的,但是我有另一个控件可以修改它们。
    【解决方案4】:

    要使我们的输入字段在 drupal 表单中为只读,请将值 TRUE 设置为 readonly 属性。

    例如,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多