Tim Oz Wed Sep 28 18:17:16 -0400 2011

Subject: Perform validation only on create

Hi guys, I'm new here:

I've been trying to us the "'on'=>'create'" option for validation as the wiki suggests,

static $validates_presence_of = array(
array('title', 'message' => 'cannot be blank on a book!', 'on' => 'create')
);

Here is my code,

// Validations
static $validates_presence_of = array(
array('email', 'message' => 'Please enter a valid email address.'),
array('password', 'message' => 'Password must be provided.', 'on' => 'create')
);

It seems the option is completely ignored...am I missing something?

I want to set it up like this so that when a user updates their profile, they can leave their password blank to keep their current one.

Thanks for any help you can offer!


Nicholas Morgan Sun Nov 20 05:28:50 -0500 2011

Hey, I know you posted this a month ago so I would imagine you already found a solution but I thought I'd chime in.

You can pass FALSE as a parameter into the save() method and it will automatically skip validations. It says it on this page, in the 2nd block of code:
http://www.phpactiverecord.org/projects/main/wiki/Validations
It should also be listed in the CRUD page, but it's not, which could explain how you might have missed it.

So, you would do something like $my_model->save(FALSE) whenever you want to skip validations. In your case, you would skip validations when you are updating an existing model.

Hope this helps! o:-)

(1-1/1)