How to disable a form element in a Zend Form?
Asked Answered
D

9

26

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:

    $this->addElement('text', 'username', array(
        'label'      => 'Username:',
        'required'   => true,
        'filters'    => array('StringTrim'),
        'validators' => array(
            array('StringLength', false, array(2, 50))
        )
    ));
Domiciliate answered 18/11, 2009 at 20:52 Comment(0)
L
51

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')
));
Lactation answered 18/11, 2009 at 21:5 Comment(2)
Note that you should use 'disable' instead of 'disabled' on a CheckBox, because of this bug : framework.zend.com/issues/browse/ZF-9149Jammiejammin
Same here, this worked and was just what I needed. Quick and effective.Carborundum
L
6

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

Lectureship answered 26/6, 2012 at 7:28 Comment(1)
Does disable really work instead of disabled or is this an error.Catharina
Q
1

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

$form->get('username')->setAttributes(array(
    'disabled' => 'disabled',  
)); 
Quixotic answered 18/6, 2013 at 0:58 Comment(0)
D
1
$form->getElement("username")->setAttribs(array('disabled' => 'disabled', ));

or

$form->getElement("username")->setAttrib('disabled', 'disabled');
Drolet answered 8/8, 2013 at 2:10 Comment(0)
B
0

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

Brandabrandais answered 12/12, 2012 at 15:42 Comment(0)
F
0

Apply this code into your Application

$formelement->setAttrib('readonly', 'true');
$formelement->setAttrib('style', 'pointer-events: none');
Forlini answered 26/4, 2014 at 14:21 Comment(0)
P
0

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

$element->setValueDisabled(true);
Price answered 29/5, 2015 at 12:7 Comment(0)
P
0
// 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 disabled method of other answers changes the color of checkbox to "grayed out". Method described here, does not.

Plating answered 23/2, 2016 at 20:45 Comment(0)
C
0

@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.

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.

Christensen answered 11/11, 2016 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.