Zend Framework and dojo: tricky checkbox

22 Jan

Few weeks back, I used dojo check box in my Zend framework application. Scenario  was a bit different at that time. I was submitting Zend dojo form on the click event of the checkbox. When I created a form and submitted that onclik, It didn’t give me desired results.

I set its checked value to 1 and unchecked value to 0, however when I submited it always gave me a single value. I was looking for 0 and 1, but it always gave me 0. I thought that It was because I was not using proper submit button for the posting of form.

However when today I used checkbox in normal zend dojo form, it gave me the same result. Oh God, how to fix it.

However as I have played with dojo js files, so I got the solution.

Let’s tell you the trick.

First of all create check box as

 

$checkbox= $form->createElement(

                        ‘checkBox’,

                        ‘checkbox’,

                        array(

‘label’ => ‘Tricky check box’,

‘checkedValue’ => 1,

‘uncheckedValue’ => 0

)

);

Now add onclick event to this checkbox element as

$checkbox->setAttrib(‘onclick’,’changeValue()’);

 

Now in your view, where you display this form, write the following javascript function

<script>

function changeValue()

{

if(dijit.byId(‘checkbox’).checked){

            dijit.byId(‘checkbox’).setValue(‘1’);

} else {

            dijit.byId(‘checkbox’).setValue(‘0’);

            dijit.byId(‘checkbox’).checked=false;

}

}

</script>

 

The above code isn’t simple?

Hmm. First we see if we have checked the checkbox, if yes we assign value 1 to it, if not we assign it value 0 and set its checked status to false. This will uncheck the checkbox.

That’s it. Submit your form and you will get desired results.

Doesn’t it help you. Do you have smarter solution than this.

Tell me……

2 Responses to “Zend Framework and dojo: tricky checkbox”

  1. zendfan January 25, 2009 at 1:04 pm #

    Hi.. You have good tutorials here about zend framework an dojo… But you can try to do tutorials about zend framework and jquery that is more popular =)

  2. Sekar February 20, 2009 at 5:29 am #

    Hi

    The Above code is very useful to me. But i have one doubt, i change value function youn used dijit.byId. Is ‘dijit’ is the name of the form,If i set like this it show’s error like “dijit undefined”

    Can u give solution?

    Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: