Html 如何禁用 Zend 表单中的表单元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1759003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:21:58  来源:igfitidea点击:

How to disable a form element in a Zend Form?

htmlcsszend-frameworkzend-form

提问by Andrew

I want to display a Zend Form with one of the elements shown as disabled. I'm setting the value so the user can see it, but I want to disable it so the user can't edit it. This may also involve some sort of css/javascript to ensure that it looks like, and is not editable by the user. This is my element:

我想显示一个 Zend 表单,其中一个元素显示为禁用。我正在设置值以便用户可以看到它,但我想禁用它以便用户无法编辑它。这也可能涉及某种 css/javascript 以确保它看起来像,并且用户不可编辑。这是我的元素:

    $this->addElement('text', 'username', array(
        'label'      => 'Username:',
        'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array(
            array('StringLength', false, array(2, 50))
        )
    ));

回答by smack0007

You should be able to use:

您应该能够使用:

$this->username->setAttrib('disabled', 'disabled');

I think you can as well:

我认为你也可以:

$this->addElement('text', 'username', array(
    'label'      => 'Username:',
    'required'   => true,
    'filters'    => array('StringTrim'),
    'validators' => array(
        array('StringLength', false, array(2, 50))
    ),
    'attribs'    => array('disabled' => 'disabled')
));

回答by cwhisperer

This works fine... Just to complete the help: If you are in a controller you can do:

这工作正常......只是为了完成帮助:如果您在控制器中,您可以执行以下操作:

$form->selRole->setAttribs(array('disable' => 'disable'));

selRole is the name of a select field

selRole 是选择字段的名称

回答by Philip F

In latest zf2.2.1 you can do this in your controller;

在最新的 zf2.2.1 中,您可以在控制器中执行此操作;

$form->get('username')->setAttributes(array(
    'disabled' => 'disabled',  
)); 

回答by tigerlee

$form->getElement("username")->setAttribs(array('disabled' => 'disabled', ));

or

或者

$form->getElement("username")->setAttrib('disabled', 'disabled');

回答by user3011968

Apply this code into your Application

将此代码应用到您的应用程序中

$formelement->setAttrib('readonly', 'true');
$formelement->setAttrib('style', 'pointer-events: none');

回答by frgtv10

Only this was working for me, when using a file elementwhen setting after form was submitted:

file element在提交表单后使用when 设置时,只有这对我有用:

$element->setValueDisabled(true);

回答by Dennis

// disable checkbox using JS add-on
$checkbox->setAttribute('onclick', 'return false'); 

Benefit: retains the original color of the box but does not let the user change the value of the box.

好处:保留框的原始颜色但不让用户更改框的值。

Using disabledmethod of other answers changes the color of checkbox to "grayed out". Method described here, does not.

使用disabled其他答案的方法将复选框的颜色更改为“变灰”。这里描述的方法,没有。

回答by CptChaos

@Dennis:

@丹尼斯:

Disabling Javascript is enough to enable the form again, so you can't truly rely on Javascript. Using native HTML disables it better, but is also simply worked around, by removing the disabled attribute.

禁用 Javascript 足以再次启用表单,因此您不能真正依赖 Javascript。使用原生 HTML 可以更好地禁用它,但也可以通过删除 disabled 属性来简单地解决。

Best option is showing the values you want instead of the form itself and disable the form and/or it's elements.

最好的选择是显示您想要的值而不是表单本身并禁用表单和/或其元素。

Wish I could add the comment directly to your post, but I'm some rep short.

希望我可以将评论直接添加到您的帖子中,但我的代表很短。

回答by Houssem

$var->setAttribs(array('disabled' => 'disabled'));

$var->setAttribs(array('disabled' => 'disabled'));