PeterG
PeterG

Reputation: 782

Re-using Yii Framework CValidator Rules on Client Side

I'm trying to figure out a way to re-use the validation rules of Yii's CValidator classes, for example CStringValidator (http://www.yiiframework.com/doc/api/1.1/CStringValidator).

I'm thinking if I can get the rules in JSON-format, I can pass the json to the javascript and build a validator function to enforce them (or build the javascript on the PHP side and send the script down to the client).

Does Yii provide anyway serialize-type method to take a validator such as CString and dump out the "rules" it uses to do validation? (something like /[\d\w]+/, just to give a simplistic example)?

Due to the nature of this project we're not able to use CActiveForm (which provides built-in client-side form validation).

Upvotes: 1

Views: 377

Answers (1)

jarchuleta
jarchuleta

Reputation: 1241

You could write an action that would return the rules in JSON pretty easily.

public function actionGetRules()
{
    echo CJSON::encode(Model::rules());
}

Upvotes: 2

Related Questions