Yii Framework Forum: Form validation with ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form validation with ajax Rate Topic: -----

#1 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 21 July 2010 - 08:27 AM

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:
  • 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:
{"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.
0

#2 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 21 July 2010 - 12:17 PM

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.
0

#3 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 21 July 2010 - 12:44 PM

cleaned
0

#4 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 21 July 2010 - 01:15 PM

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:
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 July 2010 - 03:47 AM

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.
0

#6 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 22 July 2010 - 04:48 AM

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.
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 22 July 2010 - 06:55 AM

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

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

#8 User is offline   frantic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 172
  • Joined: 01-March 10
  • Location:Saint-Petersburg

Posted 22 July 2010 - 08:42 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users