preg_match() warning with the supplied code

8 messages Options
Embed this post
Permalink
AMC

preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
Maybe I am missing something obvious, but I keep getting these types of errors when using the validation code as you suggest in the book:
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/model.php, line 2202]

Additionally, the date validator always says the date selected is wrong, but that's how it came out of bake and is in the database. (It keeps saying the supplied date is not correct).

The required one keeps saying the field is required even though there is information on the field and the names match exactly...

Code as used in the Post model:
----
        var $validate = array(
                'name'=>array(
                        'alphaNumeric'=>array(
                                'rule'=>'alphaNumeric',
                                'required'=>true,
                                'message'=>'TheTitlemaynotcontainanysymbols'
                        ),
                        'maxLength'=>array(
                        'rule'=>array('maxLength',80),
                        'message'=>'TheTitlemustnotexceed80characters'
                        )
                ),
                'date'=>array(
                        'rule'=>'date',
                        'required'=>true,
                        'message'=>'You must supply a valid date'
                ),
                'content'=>array(
                        'required'=>true
                )
        );
------

Thank you
davidgolding

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
Hmm... I've tried to replicate the situation and I don't get these errors at all. It appears that the error is occurring in the built-in regular expressions at work in Cake's Model class, but why, I'm not sure. What release of Cake are you using? Perhaps there has been a change or was a change in the version you're using.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]
Todd

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
I didn't post this, but I am using RC2(the main download they have available of 1.2) and having a similar issue.

I should submit this to your errata. (RC2)

If you do:
-----
title' => array('required'=>true)
-----
Not only does the validation fail, but the following pre_match error shows up:
----
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/model.php, line 2193]
-----
It also outputs 'required'  as if that were the message it should give out. Basically, it breaks.

Basically, the 'rule' is required in order to make a field required. If you add any rule (except blank unfortunately) the validation works fine.
So other than inserting your own regular expression, I am not sure what rule to use for a title (could use alphaNumeric - but you may want some other characters in the title).


You should add that to the errata so people aren't scratching their heads when the validation isn't working.

--

However, the date isn't validating for me with the baked datetime field and view. If I figure that out, I'll post again, it's too bad you can't replicate it.

Thank you

michaelGregoire

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
In reply to this post by davidgolding
It's simple to replicate... update CakePHP to RC2.


davidgolding

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
The book was published during RC1 stage. The Validator class has apparently gone through some core changes since then. I must recommend that you use RC1 when using the examples in the book, and if you find errors that are specific to RC2 and above, you'll have to consult the Cake devs. I'll do my best to help out with inconsistencies between the book and most recent Cake releases, but unfortunately, the book is what it is, and was written using the best version of Cake available. Do post that feedback here as well, though; this definitely helps me keep tabs on any inconsistencies between the book and any newer versions of Cake, and I definitely owe it to all of you readers out there to be as faithful to the newest versions of Cake as possible.

As for really changing up alphanumeric and other validation rules, I think the Cake devs will be hard-pressed to implement those until a 2.0 release.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]
crpeterson

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
In reply to this post by Todd

...However, the date isn't validating for me...

I corrected this problem by changing the field type in the database to date, instead of datetime.  It appears to me that the date rule is for validating dates - not dates & times.
Kieron

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
Obviously I'm on this thread because of the same problem FYI I'm using RC3. Naturally I'm not blaming author or book as this is a core change since RC1. It's a great book btw

I'm just posting the alternatives I found that worked so anyone else with this problem doesn't have to look around for half a day to solve the validation issues!

For the alphanumeric validation I did the following, limiting the title to 3 characters or more removes the need for a 'require':

                'name' => array(
                        'custom' => array(
                                'rule' => array('custom', '/[a-z0-9]{3,}$/i'),
                                'message' => 'Only letters and integers, min 3 characters'
                        ),

yes the 'date' validation rule is only for dates not 'datetime', there is no built in 'datetime' validation so your options are a custom function that you can include in your model(or as model behaviour) and call in the validate array under 'rule'. This link to the cookbook explains it a bit clearer:

http://book.cakephp.org/view/150/Custom-Validation-Rules

I haven't implemented one yet but I have made the following changes - in my add/view.ctp I put the following to better control what's displayed and have [Post][date][year] etc that I could test in a custom func:

                echo $form->datetime('Post.date', 'MDY', '24', null, array('label' => false, 'minYear' => date('Y'), 'maxYear' => date('Y')+5), false);

then a brief temporary check in the validate array, instead of 'require' I have a 'rule' - 'notEmpty' which is equivalent.

                'date' => array(
                        'rule' => 'notEmpty',
                        'message' => 'You must supply a valid date'
                ),

for the content I put a different 'require' attribute that seems to be the accepted version now:

                'content' => array(
                        'required' => VALID_NOT_EMPTY

You don't need a 'message' for this as one pops up automatically. Also there's no need now for explicitly calling the form->error as error messages are displayed automatically.

I hope this helps someone - I'd appreciate hearing other peoples changes!
backwardselvis

Re: preg_match() warning with the supplied code

Reply Threaded More More options
Print post
Permalink
In reply to this post by AMC
    var $validate = array(
    //Name field validation logic
      'name' => array(
        array(
          'rule' => 'notEmpty',
          'message' => 'Field cannot be left blank'
        ),
        array(
          'rule' => 'alphaNumeric',
          'message' => 'Field must use alphe numeric characters'
        )
      ),
    //Content field validation logic
      'content' => array(
        array(
          'rule' => 'notEmpty',
          'message' => 'Field cannot be left blank'
        )
      )
    );