Thursday, September 30, 2010

CakePHP ignores notEmpty validation rule when the field's name is not a key in $data! (Bug or Feature?)

Hi everybody

I have baked the following very straight-forward model:

class Post extends AppModel {
var $name = 'Post';
var $displayField = 'name';
var $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty')
),
),
);
}

It only needs a value for "name" to be saved.

Now I've written the following 3 tests:

function testShouldNotBeValidWithEmptyName() {
$this->Post->create(array('name' => ''));
$this->assertFalse($this->Post->validates());
}

function testShouldNotBeValidWithoutName() {
$this->Post->create();
$this->assertFalse($this->Post->validates()); // Fails!
}

function testShouldNotBeValidWithNullName() {
$this->Post->create(array('name' => null));
$this->assertFalse($this->Post->validates());
}

I expect all three of them to work, but the 2nd one fails! So why does
it validate when there truly is no value for the "name" field? Is this
really the way it should be, or is it a bug?

I'm on version 1.3.4.

Thanks for help
Josh

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: