While working on some validation required in CakePHP, I came across this annoying error.
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php, line 2202]
That error has come up before in the CakePHP trac tickets, but it was for a different part of validation. Here’s what it means and how to fix it.
The error means that you need to wrap your regular expression in delimiters, which are backslashes. Here is an example. The following is your regex: (Pirate|Ninja|Alien). You need to make it into the following, regex: /(Pirate|Ninja|Alien)/.
However, if you’re like me and you found this error while using CakePHP validation, the solution is different and involves, more than likely, some array, somewhere.
Here is my CakePHP validation variable for one of my models.
public $validate = array( "poster_name" > array( "required" > true ), "poster_email" > array( "rule" > "email", "message" > "Enter a valid email address." ), "start_showing" > array( "rule" > "date", "message" > "Enter a valid inital showing date." ), "end_showing" > array( "rule" > "date", "message" > "Enter a valid end show date." ) );
It looks right, doesn’t it? However, take a look at the poster_name field requirements. It’s only required, nothing too complicated that should mess with regular expressions, rright? Clearly not!
The following code works without any error messages.
public $validate = array( "poster_name" > array( "required" > array(true) ), "poster_email" > array( "rule" > "email", "message" > "Enter a valid email address." ), "start_showing" > array( "rule" > "date", "message" > "Enter a valid inital showing date." ), "end_showing" > array( "rule" > "date", "message" > "Enter a valid end show date." ) );
Notice the array around the true value for required? That’s the solution to the problem!
Thank you for this. I am going through Apress’s book on CakePHP (Beggining CakePHP, From Novice to Professional) and their validation script had that very error. Fortunately for me a Google search on the error message, took me right here. In fact I will echo it here to help others find your explanation quickly.
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php, line 2202]
Amen Kim!
I too am going through the Apress book and ran in the error. I still wonder if this is a Cake bug… or just maybe a slight oversight in Apress.
I was getting this very same error and it was driving me nuts. Thank you, thank you, thank you for this post clearing up the necessity for setting required as an array.
You are getting the error because you are not creating the validate array properly. And what you are doing is a hack. You need to specify a ‘rule’ for each field. ‘required’ is NOT a rule. I suggest you re-read the manual http://book.cakephp.org/view/127/One-Rule-Per-Field
hey, thanks, helped me a lot
Many fields of mine were lost when $this->save() is called only after half an hour I found that the fields that I’m left with is the field with ‘require’ => ‘true’ hack.
I know better now how not to use this hack. LOL
I don’t know why that solves the problems, but it just works, btw i was using notempty rule, which it’s part of the manual.
try this:
var $validate = array
(
‘employee_name’=>array
(
‘rule’=>’notEmpty’,
‘message’=>’Please give employee name’
)
);
it worked for me…
thanks, you saved my time. i dont understand why the cookbook is not updated. I only see this in 1.3 version i downloaded a few days ago. In my older version on 1.3 stable this is not the case. just if it helps someone.
Merged
hmm on second thoughts i do agree with Admad but not completely. ‘required’ is a rule however the array key ‘rule’ must be specified with a ruleName http://book.cakephp.org/view/127/One-Rule-Per-Field . then this array hack is not needed. I found this because i have multiple validation rules and although your workaround is quite ok and it works, it will not always work. good job though.