http://cakeforge.org/snippet/detail.php?type=snippet&id=6

@jonathan,

If you plan to distribute your application I would suggest not using short tags, since some host may turn them off.

Jonathan Snook said on June 26, 2006

Larry, re:short tags: agreed. The app I'm developing won't be for distribution. :)

Dan said on July 09, 2006

"RoR has liquid"

Emil, RoR is using eRuby for views, which is ruby embedded in html.

Liquid (which is a ruby port of the django templating) can be used as a plugin, and is basically only used when wanting to do some sort of theming...

Liquid was developed for the use of theming of individual Shopify shops... I dont know of many products/applications actually using liquid...

Emil said on August 25, 2006

Ok, I'm sorry - never really tried RoR. Just got amazed over Shopify when it was in beta.

Larry & Jonathan: I'v started develop my own personal homepage/blog and I will do it without any template engine but with cake! I'll be back when I'v tried it for "real".

Tobi said on September 03, 2006

What I am asking myself is, how does cake handle the difference between an INSERT and an UPDATE? For example: I defined the password has to be filled, but by editing the form I don't want the user to fill in the password, just if he wants to.

Jonathan Snook said on September 05, 2006

Tobi: when using Modelname->save($data), it'll do an INSERT if it doesn't have a model ID and an UPDATE if it does. If you don't want the user to have to enter in the password you can either (A) spit their password back into the form (assuming you're going unencrypted) or (B) simply check if the password field is empty on edit and if so, unset that variable. I believe CakePHP will only update fields that have data in them.

Nick Busey said on October 04, 2006

I don't know where you got the idea that you can't declare more than one requirement per field. The following is perfectly valid:

class Profile extends AppModel
{
var $name = 'Profile';
var $validate = array(
'firstname' => VALID_NOT_EMPTY,
'lastname' => VALID_NOT_EMPTY,
'password' => VALID_NOT_EMPTY,
'email' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL
);
}

Though unnecessary, just to demonstrate. You can use Cake's validation for anything, no need for custom functions.

Jonathan Snook said on October 04, 2006

Nick: those issues may be resolved in recent builds but wasn't when this article was posted. That's where I "got the idea".

Chris Hoeppner said on January 03, 2007

I have written a custom validates routine that checks for any declared "validateFieldName()" functions and runs them, passing any error messages into an array called (doh!) $errorMessages. After all found functions are called, it checks to see if there's any messages in $errorMessages, and returns false (preventing the save operation) if there are some. If I wanted, I could have called the parent::validates function before returning anything, but I rarely use the $validate array for anything.

@Nick: That's nice if all you need is to check if the field is not empty or is a valid email. I use those constants in my validate functions too. But what about checking if someone is double-registering, of if the two password fields match.

Code igniter has a more flexible validation system. Not that I prefer CI, it's just that.

Cake for everyone!

Jim Zaghini said on January 09, 2007

Thanks mate,

You have saved my feeble brain. It will not be exploding today

:)

Marko Samuli Kirves said on May 04, 2007

Invalidation from the controller did not work in 1.2. Saving the model clears fields in validationErrors array.

I solved this by setting new validation rules that fail with custom messages if I found in controller that data is not valid.

Jonathan Snook said on May 04, 2007

@Marko: likely with the next alpha release of 1.2 you should be able to do custom messages with custom validation. It should be sweet.

Harro said on May 09, 2007

I seem to have a problem accessing $this->data[] in the model. Do you know what this issue could be?

Thanks, great work your doing here.

Jonathan Snook said on May 10, 2007

If you don't have anything in $this->data, are you sure the form is set up correctly? It's hard to say but if you haven't already, check out the Google group and post your question there. You'll probably have more success in getting your question answered.

Norm said on September 24, 2007

Say if you want to use $validates but not worry about every field in the $validates array being required. Do something along these lines in your model:

function beforeValidate(){

foreach($this->data['ModelName'] as $k => $v){
if(empty($v)){
unset($this->validate[$k]);
}
}

return true;
}

It will only validate when the field is not empty.

Brian Katzung said on January 28, 2009

Regarding templating vs PHP tags (from June 26, 2006):

I came to PHP from Perl, so it was natural for me to think in terms of heredocs:


echo <<<HTML
<title>$title</title>
HTML;

While it doesn't deal with iteration or other "complex" coding, it does take care of all those finger-busting tags for simple variable substitution in large HTML blocks.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.
f2富二代官网入口