T
T
Timur Tuz2013-07-18 18:19:10
Yii
Timur Tuz, 2013-07-18 18:19:10

How to apply the model field validator to an arbitrary variable in Yii?

Firework! actually interests a subject. Yii has a model validation mechanism before saving data, I want someone to use it so that the code is more slender and not use bicycles. How can you check a variable that is “not a model field”? Let's say I have an email variable that comes from a form.
Of course, you can create an empty model, in it a field, set up validation rules. And assign the value of my variable to it, but somehow it turns out cumbersome. Or is a bicycle better in this case?
Upd: I found one option through the creation of a blank form model .

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
p4s8x, 2013-07-18
@TTA

Something like this?

$email="[email protected]";
$validator=new CEmailValidator;
     if(!$validator->validateValue($email)){
            throw new CHttpException(500,'Not email');
     } else
         echo 'OK';

R
rakot, 2013-07-18
@rakot

In fact, nothing is cumbersome, there are special CFormModels for forms, look at the feedback form as a reference. Check email today, something else tomorrow, better do it right away.

D
Dialog, 2013-07-18
@Dialog

There is CModel, if of course we are talking about a model. But as an option, you can use the factory in yii to create a CValidator and validate any value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question