Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-03-2008, 04:00 PM
JGD JGD is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
Default Show/Hide Zend_Form elements by ticking a checkbox

Hi,

Is there a particular way to show/hide elements by ticking a checkbox within a Zend_Form?

My form has been set up using zend_form_elements as oppose to coding them in using Html and as such I'm finding it difficult to add a javascript onlclick event to show/hide another element of the form. Could anyone forward any suggestions?

Any suggestions are appreciated
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-03-2008, 05:28 PM
Senior Member
 
Join Date: Jun 2008
Location: Florida
Posts: 108
Default

I like to avoid javascript as much as possible, but you will need to use some javascript to accomplish this. I personally use the submit(); method for the onClick event or onChange event of the form element, then have logic in your controller to either add, or not add the element based on the value in POST for the checkbox.

For example using a drop-down to change whether or not a textbox appears on the form:
Code:
       
    public function saveAction()
    {
        // get the $showTextField from the POST if there is one
        if ($this->_request->isPost()) {
            $showTextField = $this->_request->getPost('show_text_field');
        } else {
            $showTextField = 0;
        }

        // get the form object
        $this->view->form = $this->getForm($showTextField);
        
        // rest of the controller logic goes here
    }

    private function getForm($showTextField)
    {
        // ...create the form object here

        // show_text_field
        $options = array(1 => 'Yes', 0 => 'No');
        $form->addElement('select', 'show_text_field');
        $form->show_text_field->setLabel('Show Text Field:')
                              ->setOptions(array('onChange' => 'submit();'))
                              ->setRequired(true)
                              ->setMultiOptions($options)
                              ->addValidator('Int')
                              ->addFilter('StripTags');

        if ($showTextField) {
            // first_name
            $form->addElement('text', 'first_name');
            $form->first_name->setLabel('First Name:')
                             ->setOptions(array('size' => 30,
                                                'maxlength' => 50))
                             ->addValidator('stringLength', true, array(1, 50))
                             ->setRequired(false)
                             ->addFilter('StripTags');
        }

        return $form;
    }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-04-2008, 10:14 AM
JGD JGD is offline
Junior Member
 
Join Date: Jul 2008
Posts: 9
Smile Show/Hide form fields

Thanks for the response jweber.

In the end I used prototype.js, adding this to my view script:

<script type="text/javascript" src="../'path to prototype library'/prototype.js"></script>

function init()
{
Event.observe($('checkme'), 'click', toggleComments, false);

toggleComments();
}

/*comments being the element to appear/fade and checkme being the check box used to toggle the control*/

function toggleComments(e){
var dd = $('comments').ancestors()[0];

if ($('checkme').checked == true) {
Effect.Appear(dd);
} else {
Effect.Fade(dd);
}
}

init();

Hope this helps others.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-17-2008, 10:50 AM
Junior Member
 
Join Date: Aug 2008
Posts: 4
Default

If you don't use Prototype for other functionality it doesn't make any sense.

I would rather use onClick and some simple JS function setting field from disabled to enabled and changing the style of a <div> with hidden elements from display: none to block.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:24 AM.