GitHub Updates
Multiple Repos
I wasn't very smart about this whole Git fad when I first started my GitHub account and just dumped all my CakePHP code into one repo. Turns out this isn't the way to do it, since you have to clone the whole repo, even if you just want one piece of code. My bad. Everything has now been split into it's own repo to make it easier for you guys to fix my shitty code. You're welcome.
jQuery Validation Updated
My jQuery Validation helper has now been pluginized. Actually, it's now a plugin within a plugin. Both the CakePHP helper and the jQuery code are plugins. It's not a straight drop-in for the old version, so make sure you read the directions. Basically it does all the same stuff as before - converts your Cake validation rules from the model to work in JavaScript, where it can. The cool thing about making the JavaScript into a proper jQuery plugin is that you can now add custom handling without changing the original source.
Callbacks
At the moment there are three callbacks: beforeFilter, setError and afterFilter.
beforeFilter - called before validation is started. No params.
afterFilter - called after validation is done. Passed an array of errors.
setError - called anytime a validation error is found. Passed the field and the error message.
So if you wanted some alternate handling for errors you could do something like this:
$.fn.validate.setError = function(field, message) {
//don't ever do this or I will come for you
alert(message);
}
Custom Rules
The old version would skip right over the custom validation rules, since it didn't know how to handle them. This new version includes the rules and if a function with the same name exists it uses that to validate. It's up to you to write the custom function and you're free to use Ajax here. For example if you had a custom validation rule "myRule" you would define the function like this:
$.fn.validate.myRule= function(val, params) {
//do some validation here and return either true or false.
}
params is the same as they are defined in the model's validation.
Model Lazy Loading
My first attempt at this was moderately successful, but some flaws were pointed out in the comments. A commenter also pointed out some code in the CakePHP bin that was attempting to accomplish the same thing. The oldest instance I could find was this one. The were some things I liked about this version, namely it didn't remove the defined associations. But I also didn't like the was it took over the __constructLinkedModel method. After going around and around with this I've come to the conclusion that it's the only way.
I've updated my version to use the same principles as jose_zap's CakePHP bin version. Here's the differences:
- DRYier
- Runs as a plugin
- catch
resetAssociations- This is called after afindoperation and inadvertently builds all the models, completely defeating the purpose of this code. Basically I'm just removing the functionality, which only matters if you usedbindModel. If you're using this code block there really isn't any reason to be usingbindModel, so I figure there's no harm here.
This update should fix the issues with counterCache and HABTM inputs.
Coming Up
I plan on rolling pretty much all my code into plugins. It's just so much easier to distribute and keeps everything contained. Keep an eye on my repo if you're interested - I won't be bothering with blog posts for every update.

29 Comments
Glad you came forward. Nice piece of code. Took me awhile to figure out why you did it this way, but after playing with it, it seems like it's the only way to make it work without changing the core.
Did you have any other ideas to make it better?
The & ACL issue I had was in the behavior, not the component. Did you have the same problem in the component?
I don't know if it's a mistake and you should name the function "myRule" as well. If not, IMHO the name of your custom validation rule is irrelevant isn't it?
BTW, that plugin is amazing. I've been using it for about 6 months, and am looking forward to integrate it with my custom AJAX validations.
Lazy Loading is very interesting, thanks José & Matt!
I find a solution for "resetAssociations". It is using "property_exists". "property_exists" doesn't call "__isset" ! ^^
So, see that and have fun :
function resetAssociations() {
if (!empty($this->__backAssociation)) {
foreach ($this->__associations as $type) {
if (isset($this->__backAssociation[$type])) {
$this->{$type} = $this->__backAssociation[$type];
}
}
$this->__backAssociation = array();
}
foreach ($this->__associations as $type) {
foreach ($this->{$type} as $key => $name) {
if(!property_exists($this, $key)) {
continue;
}
if (!empty($this->{$key}->__backAssociation)) {
$this->{$key}->resetAssociations();
}
}
}
$this->__backAssociation = array();
return true;
}
Nice solution. I'll probably just leave my version with the empty function, since I can't imagine people needing bindModel. I'll leave this here for anyone who does though.
Thanks.
I only have one suggestion: Why not name it LazyAppModel so you let core code (which extends AppModel) alone and extend form LazyAppModel those models that you want to be lazy.
* failed when 'width' param is used in assoc conf
*
http://bin.cakephp.org/view/205218919
Like : $this->Post->contain(array('Category', 'Tag'), false);
http://bin.cakephp.org/view/205218919
I've just noticed that the API of the ValidationHelper has changed :-). Instead of using 'formId', it uses 'form' now as the key (so it accepts any jQuery selector).
However, the documentation still talks about the formId, which won't work because you've got to pass it "#$formId".
Multiple repos is much better now.
If you find yourself drunk, bored, or drunk and bored, any chance you might feel like explaining how plugin creation works in a bit more detail for those of us not as much in the know? I thought you wrote an article on this before, but was unable to find reference to it. (Searching your site for "plugin" returns the "BlogFollow plugin" on almost every page.)
Hmmm...don't recall writing an article on CakePHP plugins. This is probably the closest thing. I think the Cookbook Docs cover plugins pretty well - you might want to check that out.
Add new comment