Yii Framework Forum: how to reuse the rules defined in rules in ajax validation? - Yii Framework Forum

Jump to content

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

how to reuse the rules defined in rules in ajax validation? Rate Topic: -----

#1 User is offline   bugmelody 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 09-December 09

Posted 06 May 2011 - 08:51 AM

sorry for my bad english ..
the title wrong
i want to say "how to reuse the model rules in ajax validation?"

-
for example,

//rules defined in model Post
public function rules()
{
return array(
array('title, content, status', 'required'),
array('status', 'in', 'range'=>array(1,2,3)),
array('title', 'length', 'max'=>128),
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
array('tags', 'normalizeTags'),

array('title, status', 'safe', 'on'=>'search'),
);
}
-------------

class ajaxController extends Controller{
public function actionValidateTags(){
//how to reuse the rules in Post::rules????????
}
}
-----------------------------------------------
0

#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 06 May 2011 - 10:11 AM

Validation is performed in models, not in controllers.
Any controller can validate data for a certain model by creating an instance of the model in its action.
So, why don't you create a model instance in your controller.

BTW, 'ajaxController' seems something very strange to me.
Yii will take care of the ajax validation without much extra coding.
0

#3 User is offline   Asgaroth 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 371
  • Joined: 17-July 09
  • Location:Bogota, Colombia

Posted 06 May 2011 - 11:05 AM

From the api:

Quote

To ensure data validity, server-side validation is always performed. By setting enableAjaxValidation to true, one can enable AJAX-based validation; and by setting enableClientValidation to true, one can enable client-side validation. Note that in order to make the latter two validations work, the user's browser must has its JavaScript enabled. If not, only the server-side validation will be performed.


check http://www.yiiframew...1.1/CActiveForm
- Extension: Dynamic DataProvider Behavior
- Extension: XUpload - jQuery File Upload Extension
- Extension: PhpQuickProfiler - A Web Log Router that will help you profile your application
- Extension: XDateView - A Date grouped Grid View
- Extension: Foundation - An easy to use, powerful, and flexible framework for building prototypes and production code on any kind of device.



- Wiki: How to generate Yii like Documentation
- Wiki: How to re enable logging during unit testing
0

#4 User is offline   bugmelody 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 09-December 09

Posted 06 May 2011 - 10:59 PM

thanks for reply
2 things i'am confusing
1:when the user finish username,he 'tab' to password,then i want to ajax validate username.
what is the best way to organize my code
2.eg. for page like post show ,there is only one input :comment content.
should i create a class :CommentForm extends CFormModel.
i think this way is not perfect.
0

#5 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 07 May 2011 - 11:47 AM

View Postbugmelody, on 06 May 2011 - 10:59 PM, said:

1:when the user finish username,he 'tab' to password,then i want to ajax validate username.
what is the best way to organize my code

Use CActiveForm, and it will do all those things for you.
In fact, yiic-generated skeleton Yii app already has the kind of login form that you are trying to write for yourself.

View Postbugmelody, on 06 May 2011 - 10:59 PM, said:

2.eg. for page like post show ,there is only one input :comment content.
should i create a class :CommentForm extends CFormModel.
i think this way is not perfect.

The number of the fields will not be a problem.
And where do you plan to save those comments?
I think you need some database table to save them, as you see in the blog tutorial of Yii.
If you are going to use a database table, then you should use an active record model for it. And it will make things much simpler and easier.
0

#6 User is offline   bugmelody 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 78
  • Joined: 09-December 09

Posted 07 May 2011 - 06:19 PM

View Postsoftark, on 07 May 2011 - 11:47 AM, said:

Use CActiveForm, and it will do all those things for you.
In fact, yiic-generated skeleton Yii app already has the kind of login form that you are trying to write for yourself.


The number of the fields will not be a problem.
And where do you plan to save those comments?
I think you need some database table to save them, as you see in the blog tutorial of Yii.
If you are going to use a database table, then you should use an active record model for it. And it will make things much simpler and easier.

thans,you resolve my confuse.
but,i look at the CActiveForm ,

when registering,if i want only to ajax validate password,it will be also to validate username,eg.username required,username not exists.(i saw yii source code in CActiveForm::validate)
the extra validate i don't want !
$model->validate($attributes);
foreach($model->getErrors() as $attribute=>$errors)
	$result[CHtml::activeId($model,$attribute)]=$errors;

it wiil validate all attributes .not password only
how to deal with this problem?
0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,041
  • Joined: 16-February 11
  • Location:Japan

Posted 07 May 2011 - 08:11 PM

Define scenario based validation rules in the model.
Creating model
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