I have a comment form, there are only two fields in there, are: name and comment, other parameters like photoid is not entered by user. I send the data via ajax and I need to display some errors if the data is wrong.
I think I need five rules:
[*]Name is empty
[*]Comment is empty
[*]Name length higher than 32 characters
[*]Comment length higher than 256 characters
[*]And finally if user is already commented this photo earlier than one hour ago
I have a Comment model, it’s AR model, and I create the form manually, without form creator. It looks like:
<form action="<?php echo $therighturlhere; ?>"> <fieldset> <legend>New comment</legend> <div class="row"> <span>Name</span> <input type="text" name="name" /> </div> <div class="row"> <span>Comment</span> <textarea name="comment"></textarea> </div> <div class="row"> <input type="submit" value="Add" /> </div> </fieldset> </form>
I created four rules in Comment model:
return array( array('name', 'required', 'message'=>'Enter the name'), array('comment', 'required', 'message'=>'Enter the comment'), array('name', 'length', 'max' => 32, 'tooLong'=>'Name is too long'), array('comment', 'length', 'max' => 256, 'tooLong'=>'Comment is too long'), );
The fifth rule I don’t know how to realize, bacause I don’t know how this validate works. In my ajaxcontroller I write:
$model = new Comment(); $model->name = $_POST['name']; $model->comment = $_POST['comment']; $model->validate(); echo CJSON::encode($model->getErrors()); exit();
And in firebug I have the response:
As you can see no of needed validation error messages here, only that the parameters is not entered. So do you know how can I send these messages? And maybe you know how to create the error message for the five rule? I know how to check if user already commented, but don’t know what to do with message
Sure I can make these messages manually, but want to use the yii validator.