CakePHP require, notEmpty, allowEmpty, and form validation

CakePHP require, notEmpty, and allowEmpty FAQ: Help, I'm trying to edit my CakePHP model $validate field to make sure a CakePHP form field is required, but 'require' doesn't seem to be working as expected.

After fighting with a CakePHP form I just got this information beat into my head, so I thought I'd share it here. In short, if you want to make a CakePHP form field required, what you really want to use is the CakePHP notEmpty rule, like this:

var $validate = array(
  'username' => array(
    'ruleRequired' => array(
      'rule' => 'notEmpty',
      'message' => 'Please supply a valid Username'
    )
  ),
  'password' => array(
    'ruleRequired' => array(
      'rule' => 'notEmpty',
      'message' => 'Please supply a valid Password'
    )
  ),
);

As you can see, in both of those examples I use the CakePHP notEmpty rule to say that these CakePHP form fields are required.

Now that you've seen this, let's take a look at that pesky CakePHP required validation attribute.

CakePHP required attribute

The CakePHP required attribute confused me for quite a while, as I assumed it was the proper/obvious way to make a CakePHP form field required. However, the "required" attribute is not actually a CakePHP rule, it's just an extra attribute you can specify on a form field in the $validate array.

Here's the key thing to remember about the CakePHP required attribute:

When you specify the CakePHP required attribute, what it really means is that this form field must always be present in the $this->data array.

If you specify that a model's field is required, it means that any time you create a CakePHP form for this particular model, this field must be in your form.

That's all it really means.

A CakePHP require example

I just learned about this the hard way. I was working on a User model, where I had made the "username" field required. This was fine for my login page, which had these fields:

  • username
  • password

And it was also fine for my "register" page, which had these form fields:

  • username
  • password
  • confirm_password
  • email_address

However, the CakePHP form validation process was failing silently for my "Change Password" form, which contained only these fields:

  • old_password
  • password
  • confirm_password

Because (a) my CakePHP form didn't include the username field and (b) I had made the username field required, the validation process kept silently failing.

That, my friends is what CakePHP "required" means.

So, in summary, if you really want to make sure a user has to enter data in a CakePHP form field, use the "notEmpty" CakePHP form rule, and not this required attribute. (You may also be able to use the CakePHP allowEmpty rule and set it to "false", but at this moment I don't know why you'd do that instead of using the CakePHP notEmpty rule.)

Comments

Permalink

Thanks for taking the time to put this online, I'm new to CakePHP and had the same confusion, your post cleared it up!

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.