Validations

Is there anyway to use Model validation functions without the model?

You can use the validator class itself, but I think that is an useless effort.

If you need to validate some user input, maybe you need also to collect it. If your input will not saved on database, use CFormModel.

Ahh what I am talking about, is validating GET variables for extra security.

For instance if a user signs up, and the application sends the user an email, to confirm the sign up process with a random generated key, how do I validate this key to make sure it is not malicious. I could think of tons of other possibilities but this one will do for now.

Or for instance just an id in the URL to collect information from the database.

I know you are just going to say, well if the ID does not exist in the database then the database will not fetch anything. But this may not work in "all" cases where I need may need to use GET.

You don’t need to use get.

Not directly anyway.

Just pass it as a parameter to your activate action in your user controller:




http://your_site/user/activate/2309487234kl2l3kj42lkjfkjsdhfawu



When you generate the activation key, then pad it with hashed username and a timestamp, so that you can let it expire.

In your action, un-hash the parameter, do some checks, etc.

Just like you would with regular parameters.

My two cents. ;)

Ahh ok,

Well ok the URLs that I am using are like this: -

http://localhost:8888/hermes/?r=client/index

I do not know how you have gotten your URL like that.

Also you said you do not need to use GET, so how do you access the parameter using code in that URL you have shown me?

First, you don’t need to pass $_GET paramaters with Yii - since Yii 1.1.4 it deals with those auto:

Action Parameter Binding

That means that if you have a parameter called $whatever in an action, it will automatically get $_GET[‘whatever’] as $whatever. :)

That’s neat.

Second, to have URLs ‘like that’ you need to configure the UrlManager, like described here:

Yii Definitive Guide - user-friendly-urls

I takes a .htaccess file and some rules.

Maybe like this:


'<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',



It will create urls like site/controller/action/parameter. :)




That means that if you have a parameter called $whatever in an action, it will automatically get $_GET['whatever'] as $whatever.



Yeah that is neat, does this work with post?

Post in action urls won’t work.

But if you have a form, it’s really easy:


if(post is set)


$model->attributes = $_POST['myForm'];




Thanks, yeah so it only really works with GET then,

Yeah its cool, I am really enjoying the Yii framework so far, seems to beat all the other frameworks by miles.