Form validation with ajax

Hi!

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:

[list=1]

[*]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

[/list]

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:


{"name":[null],"comment":[null]}

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.

I work it out. It’s because of wrong encoding of the Comment.php file :blink:

I needed to show you the true messages ( in russian ), bacause the file was in ANSI and the messages has been converted via CJSON::encode into the null. So I just resave the file in UTF-8 and all works fine.

cleaned

Guys I want here to ask. Do you know the tool for cleaning the text before saving to database? I mean that cutes the XSS and any HTML from the comment? This one doesn’t works


CHtmlPurifier::purify($_POST['comment'])

I’m talking with myself :lol:

Remember in general to encode all file in UTF-8 without bom.

That avoid any possible problem with encoding. I had your same problems when I passed from linux to windows editors.

Zaccaria, I have done it already. I use RapidPHP, in settings I set the file’s default encoding to utf-8 without BOM, so I was wondered when I saw my Comment.php in ANSI :blink:

Maybe you know how to clean the comment text? I now use the standard preg_replace from PHP manual.

I think that the best is to simply do htmlentities($text).

This will replace all <, > and so on with &gt, &lt and so on

Ok, thanks. I just thought yii have own capabilities :)