Have you found yourself wanting to remove validation on a specific field in a specific form? Yes? Excellent, you found the right place.
If you haven’t had the need for this, a great example of where you would want to accomplish this would be in a users edit form. Normally in CakePHP we would encrypt the password, especially if you are using the AuthComponent, so we wouldn’t want the password prefilled on our form. Instead, we’ll make it blank and place a note underneath that says “Only enter a password if you wish to change it”.
The above shouldn’t be difficult, however, when you go to use the form, you will get some unexpected results. I keep getting a message saying that I must enter a password, because I had setup validation on that field for the registration process. Below is a simple solution to this problem.
The solution is really quite simple. By default the password field will be in the $this->data when you submit the form. To remove validation, you need to unset() the value from the variable you pass to the save() function. Below is an example of how to accomplish this:
[code]if (empty($this->data[‘User’][‘password’])) {
unset($this->data[‘User’][‘password’]);
}
[/code]
As long as we unset() the field, CakePHP will not validate our field. This one was a bit tricky for me at first and I was looking for more complicated solutions, until I found this easy answer. Hopefully, I have saved you some time with this.
If you find yourself utilizing this quite frequently and potentially multiple times inside your controller (or beforeValidate() callback function) I would suggest creating a function that performs this for you. Perhaps a function that accepts a single value or an array of values as well as your $this->data and then have the function loop through each array item and unset() the values that you told it to, if they are empty.
I invite anyone to share a function similar to this, if you have one.
Pingback: Button to demo F1 car in Manchester | Auto Racing Fanatics
Pingback: hera
Pingback: remote virus repair
Pingback: Goozle Zone
Pingback: bad credit loans
Pingback: ania quisumbing
Pingback: www.onebuckresume.com
Pingback: ultimate power profits
Pingback: zig zagz
Pingback: Personal Injury Solicitors in Manchester
Pingback: personalized pens pencils
Pingback: ZigZag
Pingback: calaguas tour
Pingback: ZCode System
Pingback: new york asian escorts
Pingback: Blog
Pingback: Glass
Pingback: Success
Pingback: Information
Pingback: Collecting
Pingback: Learn