Three new validation rules

Published on and tagged with cakephp  deprecated  feature  model  validation  view

Recently, three new validation methods have been added to the core validation class and hence you can use three more validation rules in your models: boolean, inList, and time.

boolean is self-explanatory: it validates whether a field contains a boolean value (i.e. 0, 1, “0”, “1”, false, or true). Example:

var $validate = array('is_enabled' => array('rule' => array('boolean')));

inList ensures that the respective field only contains a value from the defined array. It is case-sensitive, i.e. if you use the rule from the example below, then the value “Red” would be invalid.

var $validate = array('color' => array('rule' => array('inList', array('red', 'green', 'blue'))));

time determines whether a field contains a valid time value. It supports both the 24 hour format (e.g. 08:10:10) and the 12 hour format (e.g. 8:10:10am). The minutes and seconds are optional in both cases.

var $validate = array('starttime' => array('rule' => array('time')));

Another recent change affected View::renderElement(): it has been deprecated in favor of View::element():

$this->renderElement('my_element', array('param1' => 'a value'));


$this->element('my_element', array('param1' => 'a value'));

I don’t know why View::renderElement() was deprecated and not View::element(), as I think the method name “renderElement” is more expressive than “element”.

Anyway, happy baking :)

14 comments baked

  • Nate Todd

    Thanks. I honestly was just thinking how nice the first two would be not more than 30 min ago. Great to see these defaults added as they simplify validation of fields (checkboxes, selectboxes) that many developers assume are safe to not validate.

    Also, thanks for the heads up on renderElement(). I also agree with you that it makes more sense…

  • Juan Basso

    Nice information. I have translated this for portuguese in cakephp blog for portugueses.

    One think more: need closes parentheses for method renderElement and element in your exemple.

  • Steve Lounsbury

    If I remember correctly from looking at the code, View::element actually ends up calling View::renderElement. View::element provides a caching mechanism around View::renderElement so you can do per element caching.

    Even with that, I too was surprised to see View::renderElement become deprecated as I agree that the name is much better.

  • CakePHP Brasil » Novas opções de validação

    […] post é baseado no post Three new validation rules, de Daniel […]

  • Mark Story

    element() did call renderElement. As for preference of one over the other, element is shorter and fits a bit better with the other view /helper methods. Just like it is no longer generateSelectDiv() just input().

  • cakebaker

    @all: Thanks for your comments!

    @Juan: Thanks for the hint, it is now fixed in the article.

    @Steve: Yes, you remember correctly, View::element was originally a wrapper method for View::renderElement.

    @Mark: Personally I prefer method names to start with a verb because methods do something.

  • CakePHP1.2 で追加される3つのバリデーション | Sun Limited Mt.

    […] 次に公開される CakePHP 1.2 に3つのバリデーションが追加されるようです。 Three new validation rules – cakebaker […]

  • Bret Kuhns

    It’s certainly silly to deprecate renderElement. I also agree that methods are considered the “actions/behaviors” of an object and should appropriately contain action verbs. This isn’t always appropriate, but especially in the case of View::element(), it just makes good sense to specify the method /renders/ the element. Perhaps someone new to cakePHP could assume the method simply returns an instance of an Element object. Without consulting the docs, it’s hard to tell, whereas renderElement() made it clear. Internal documentation via API!

    However, I guess as Mark points out, cakePHP doesn’t really follow the verb convention often anyhow. This may very well make cakePHP more consistent with itself, but not quite what I would’ve had in mind ;)

  • cakebaker

    @Bret: Thanks for your comment!

    The naming of methods is not really consistent in cake, many methods follow the verb convention, others, like most methods of the FormHelper, ignore it. I think that’s the result of not having an explicit naming convention for method names (there is only the convention of having camel-cased method names).

  • Pages tagged "bret"

    […] tagged bretOwn a WordPress blog? Make monetization easier with the WP Affiliate Pro plugin. Three new validation rules saved by 1 others     GravitatingAngel bookmarked on 05/25/08 | […]

  • Rafael Bandeira aka rafaelbandeira3

    I have to disagree with the View::renderElement thread…

    first elements are intended to be part of the view context, so you are already rendering…
    and on the semantic path we got the following:
    echo $this->element(‘login_form’);
    wich one could translate:
    echo this element : login_form

    much better than the deprecated:
    echo $this->renderElement(‘login_form’);
    echo this render element: login_form… wow its so prolix…

    and it’s shorter, wich is one of the most wanted view features: short lines…

    btw, you are one of my gurus! lol
    ps: when I first wrote, i mispelled my email and got a flash complaining the error, but no redirects, and when I came back, the form whas emptied… :( … hate rewriting…


  • cakebaker

    @Rafael: Thanks for your comment!

    I have to admit I never saw it in this way: “echo this element: login_form”. But I think it contains a translation error: “$this” doesn’t refer to the element but the view object ;-) Anyway, you could also say “echo element login_form”.

    On the other hand you can read the code in the order it is processed: “element login_form and echo it” vs. “render element login_form and echo it”.

    Or you could argument that an element is a kind of view and the method name should be simply “render”.

    So, in the end it is probably a philosophical question which name is better.

    I’m sorry the form was emptied. I’m not sure what the cause is of that, as the form is only cleared with Firefox but not with Konqueror. It’s something I have to investigate…

    Oh, and you are the first who calls me one of his gurus, I hope that doesn’t mean you don’t question what I write ;-)

  • Rafael Bandeira aka rafaelbandeira3

    Yeah you’re right.

    I follow one convention for view helper method names and it basically says that it should make sense in the rendering context, wich usually means that verbs are not good choices as the “phrase” is always suffixed with echo or print.

    oh yeah, I use FF…

    no it doesn’t mean it!
    it means that i got you on my google reader, with many stars, on my delicious, in a special cakephp/programming bundle, and that I often “crawl” around your codes, tips and comments, cuz it’s usually good stuff.

  • cakebaker

    @Rafael: Ah, yes, that’s the convention used for the FormHelper & Co. I see why this convention is used, but personally I almost never use it in my own code. For me, methods do something, and so the names have to contain a verb to describe what they do.

© daniel hofstetter. Licensed under a Creative Commons License