2013-03-25 49 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