zend, remove require=true from jquery

199 Views Asked by At

I need to change the requirment of an input when a select change. I have this declaration on my form:

$name = $this->addElement('text', 'name', array(
                'required' => true,
                'label' => 'Name',
                'filters'    => array('StringTrim'),
            ));

I try with jquery in phtml file (view) :

$('#select').change(function(){
    $('#name').prop('disabled', true);
})

but not work, the input continue to be required. Anyone can heklp me?

Stefania

2

There are 2 best solutions below

5
On BEST ANSWER

You must remove required from the PHP side.

I can propose you this:

  1. In your form, add an hidden element like this:

    $change_select = new Zend_Form_Element_Hidden('change_select');
     $this->addElement($change_select);
    
  2. In your javascript :

    $('#select').change(function(){
        $("#change_select").val("change");
    })
    
  3. In your controller, try something like this:

    // $form  is your form in your controller like : 
    // this $form = new Application_Form_Album();
    
    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($formData['change_select'] == 'change'){
            $form->getElement('name')->setRequired(false);
        }
        if ($form->isValid($formData)) {
            ....
    
1
On

Try to remove required attribute

$('#select').change(function(){
    $('#name').removeProp('required');
})