Jquery Form Plugin clearForm() issue with CakePHP security component

This week I have had an issue where every time I cleared a form using JavaScript, CakePHP’s security component halt the processing and complained when posting it. I am using the jquery form plugin’s .clearForm() to clear the form, and found the issue, and work around.

At first I didn’t know where to start, the Security component just was rejecting the post. so I started experimenting, and found that it only happened on select boxes. Clearing text or textarea fields were not an issue. After looking around a bit more, I found that version 2.21 of the jquery form plugin (and probably all other version) at around line 578 was changing the selected index of all select boxes to -1. Since -1 is not a valid option (option’s start at 0), the Security component was thinking that the form had been tampered with, and rejected the post.
A simple change in the clearFields() function (the clearForm() function calls the clearFields function for all form elements of type text, select and textarea) fixed this all up:
//original code:
else if (tag == 'select')
            this.selectedIndex = -1;

//replace with:
else if (tag == 'select')
            this.selectedIndex = 0;
I guess the important part here may not be about fixing the jquery form plugin, but more this: however you are clearing your form, if you run into issues with the Security component, make sure that on select boxes you are not assigning them invalid option indexes (-1 in the above case).
That’s it. Now the select box entries go back to the first option (option 0 – which is often blank anyways) when a “clear form” button is pressed, the Security component will accept the post – and everyone is happy