Reputation: 1074
Hello everybody and thansk for reading .
Iam making a website just for fun and to learn yii. The website is a app that can be used for reporting pirated links.
I have an input filed in which i provide a link and than i have a dropdown menu from where you can select the type of link you are submitting. Depending on the value you select i want different validation rules to perform on the link submitted. If i was not clear in my explenation feel free to visit www.youtubetv.it , on the main page you will see a input field and a dropdown.
The code is as follows ;
<div class="span4">
<div class="input-prepend"><span class="add-on" style="height: 50px;">
<i class="icon-4x icon-globe" style="line-height: 54px;"></i></span>
<?php
echo $form->textField($model, 'link', array(
'prepend' => '<i class="icon-4x icon-globe" style="line-height: 54px;"></i>',
'class' => 'span12', 'maxlength' => 999,
'style' => 'height:60px;font-size: 22px;width: 400px;',
));
?>
</div>
</div>
<div class="span4 offset1">
<?php
echo $form->dropDownList($model, 'category', CHtml::listData(Lookup::model()->findAll('type="kind"'), 'code', 'name'), array(
'prompt' => 'Select Type',
'class' => 'span12',
'style' => 'height:60px;font-size: 22px;',
));
?>
</div>
Current validation rules in the model
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, link', 'required'),
array('id_user', 'numerical', 'integerOnly' => true),
array('link', 'length', 'max' => 999),
array('link', 'url',
'allowEmpty' => false,
'defaultScheme' => null,
//'pattern' => 'esspressione regolare',
'message' => 'The specified model does not exist.',
'validSchemes' => (array('http', 'https'))
),
array('category, web_page', 'length', 'max' => 255),
array('creation_date', 'default',
'value' => new CDbExpression('NOW()'),
'setOnEmpty' => false,
'on' => 'insert'),
array('id_public_link, category, id_user, link, creation_date', 'safe', 'on' => 'search'),
);
}
I would be grateful if someone could show me an example of how i could validate the "url" if someone selects Movie from the dropdown list.
please feel free to ask for clarification if i was not clear
Upvotes: 0
Views: 1398
Reputation: 585
Yii has so called scenario for validation rules, what you need is to add 'on' key with any scenario name you like as a value in a rule you want. And then set scenario for your model as $model->scenario = 'Your scenario'; e.g.
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, link', 'required'),
array('id_user', 'numerical', 'integerOnly' => true),
array('link', 'length', 'max' => 999),
array('link', 'url',
'allowEmpty' => false,
'defaultScheme' => null,
//'pattern' => 'esspressione regolare',
'message' => 'The specified model does not exist.',
'validSchemes' => (array('http', 'https')),
'on'=>'urlcheck'
),
array('category, web_page', 'length', 'max' => 255),
array('creation_date', 'default',
'value' => new CDbExpression('NOW()'),
'setOnEmpty' => false,
'on' => 'insert'),
array('id_public_link, category, id_user, link, creation_date', 'safe', 'on' => 'search'),
);
}
And then in your action use:
...
$type = isset($_POST['Lookup']['type'])?$_POST['Lookup']['type']:false;
if($type === '1') //as I assume from your website '1' is a Movie
$model->scenario = 'urlcheck';
...
Btw, as you can see you already have a scenario in your rule on a 'creation_date' attribute. Scenation 'insert' is default scenarion for a new records. There more default scenarions in Yii you can learn at here
Upvotes: 3