2013-03-25 54 views
0

大家好,thansk给大家阅读。yii如何根据下拉选项执行动态验证

Iam制作一个网站只是为了好玩和学习yii。该网站是一个可用于举报盗版链接的应用程序。

我有一个输入字段,我提供了一个链接,比我有一个下拉菜单,您可以从中选择您要提交的链接类型。根据您选择的值,我希望在提交的链接上执行不同的验证规则。如果我不清楚我的完成情况,请随时访问www.youtubetv.it,在主页上您将看到一个输入字段和一个下拉菜单。

代码如下;在模型

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'), 
    ); 
} 

<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> 

当前验证规则我将不胜感激,如果有人可以告诉我的,我怎么能验证“URL”如果有人从下拉列表中选择电影的例子。

请随时要求澄清,如果我不清楚

回答

3

Yii有所谓情景的验证规则,你需要的是“开”键添加你喜欢的任何有价值的方案名称在你想要的规则中。然后为您的模型设置场景为$ model-> scenario ='您的场景'; 例如

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'), 
    ); 
} 

,然后在操作使用:

... 
$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'; 
... 

顺便说一句,你可以看到你已经在你的规则的场景在“CREATION_DATE”属性。 Scenation'insert'是新记录的默认情况。在Yii中有更多的默认情景,你可以在here

+0

嘿感谢指向我在正确的方向, 1')//正如我从你的网站'1'所假设的那样是一个电影 $ model-> scenario ='urlcheck';像这样的工作 – Gunnit 2013-03-25 16:40:10