0

みなさん、こんにちは。読んでくれてありがとう。

Iamは楽しみのためにそしてyiiを学ぶためだけにウェブサイトを作っています。このウェブサイトは、海賊版リンクの報告に使用できるアプリです。

リンクを提供する入力ファイルがあり、送信するリンクのタイプを選択できるドロップダウンメニューがあります。選択した値に応じて、送信されたリンクに対して異なる検証ルールを実行します。説明がはっきりしなかった場合は、www.youtubetv.itにアクセスしてください。メインページに、入力フィールドとドロップダウンが表示されます。

コードは次のとおりです。

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

モデルの現在の検証ルール

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

ドロップダウンリストから[映画]を選択した場合に「URL」を検証する方法の例を誰かに教えてもらえれば幸いです。

不明な点がございましたら、お気軽にご不明な点がございましたら、お気軽にお問い合わせください。

4

1 に答える 1

3

Yiiには、検証ルールのいわゆるシナリオがあります。必要なのは、必要なルールの値として、任意のシナリオ名で「on」キーを追加することです。次に、モデルのシナリオを$ 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」属性のシナリオがすでに含まれています。シーン'挿入'は、新しいレコードのデフォルトのシナリオです。Yiiには他にもデフォルトのシナリオがあります。ここで学ぶことができます

于 2013-03-25T14:01:32.003 に答える