Answer the question
In order to leave comments, you need to log in
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
Something like this?
$email="[email protected]";
$validator=new CEmailValidator;
if(!$validator->validateValue($email)){
throw new CHttpException(500,'Not email');
} else
echo 'OK';
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question