-1

I am new to cakephp. I have created a form to add users. I want to allow only characters for username. My code for validation is as follows:

public $validate = array(
       "username" => array(
           "required" => array(
                "rule" => array("notEmpty"),
                "message" => "Name is required"
            ) 
        ),
        "age" => array(
           "required" => array(
                "rule" => array("notEmpty"),
                "message" => "Age is required"
            )
        )
);

what should i add to allow only characters for username.

codename
  • 25
  • 1
  • 4

1 Answers1

1

Use the "custom" rule, which takes a regex:

"rule" => array('custom', '[a-zA-Z]'),

See Regex to match only letters

CakePHP Validation manual page.

Community
  • 1
  • 1
floriank
  • 25,546
  • 9
  • 42
  • 66