Php Geek
Php Geek

Reputation: 1107

Why won't my alphaNumeric validation rules work in my CakePHP model?

I have tried this below validations for my fields.

My fields are Title, Content.

var $validate = array('title' =>array('alphaNumeric'=>array('rule'=>'alphaNumeric','required'=>'true','message'=>'Enter a title for this post',)),
                     'content'=>array('alphaNumeric'=>array('rule'=>'alphaNumeric','required'=>'true','message'=>'Enter some content for this post',)));

But whenever i enter some text in my form and try to submit, it shows error message...

Is there any problem with the validations ?

Here's my form

<h2> Add a Post Here </h2>
Please fill in all the fields.
<?php
echo $this->form->create('Post');
echo $this->form->error('Post.title');
echo $this->form- >input('Post.title',array('id'=>'posttitle','label'=>'title','size'=>'50','maxlength'=>'255','error'=>false));
echo $this->form->error('Post.content');
echo $this->form->input('Post.content',array('id'=>'postcontent','type'=>'textarea','label'=>'Content:','rows'=>'10','error'=>false));
echo $this->form->end(array('label'=>'Submit Post'));
?>

Upvotes: 1

Views: 1549

Answers (2)

user2419413
user2419413

Reputation: 1

I had to change the regular expression as follows to get it to work.

        'alphaNumeric' => array(
            'rule' => array('custom', '/[a-zA-Z0-9, ]+/'),
            'message' => 'Only letters and numbers allowed',

Why won't the built in alphaNumeric rule do this already?

Upvotes: 0

Pitsanu Swangpheaw
Pitsanu Swangpheaw

Reputation: 672

you can use custom regular expression.

'rule'    => array('custom', '[a-zA-Z0-9, ]+'),

Upvotes: 2

Related Questions