下拉列表验证无效


Dropdown list validation not Working

这是我的下拉列表

 <?php
    $dataCategory=ArrayHelper::map(Movies::find()->asArray()->all(),
 'id', 'movie_name');
  echo $form->field($model, 'movie_id')->dropDownList($dataCategory, 
             ['prompt'=>'-Choose a Movie-','onchange'=>'
             $.get( "'.Url::toRoute('screenticketbooking/dependdrop').'", 
             { id: $(this).val() } )
            .done(function( data )
             {
             $( "select#title" ).html( data );
             });
             '])->label(''); 
    ?> 

      <?php 
            $dataPost=ArrayHelper::map(MovieShows::find()->where('movie_id=:
mov_id',['mov_id'=>$model->movie_id])->asArray()->all(), 'id', 'start_date'); 
    echo $form->field($model, 'show_date')
    ->dropDownList($dataPost, 
    ['id'=>'title','prompt'=>'-Select a Date-']
    )->label('');   
    ?>  

我的模型规则

  public function rules()
        {
            return [
                [['booking_id', 'location_id', 'movie_id', 'theatre_id', 'screen_id', 'show_time_id', 'screen_class_id', 'seat_id', 'show_date', 'is_deleted'], 'required'],
                [['booking_id', 'location_id', 'movie_id', 'theatre_id', 'screen_id', 'show_time_id', 'screen_class_id', 'seat_id', 'created_by', 'updated_by', 'is_deleted'], 'integer'],
                [['show_date', 'created_at', 'updated_at'], 'safe'],
            ];
        }

我已经根据模型中的要求设置了show_datemovie_id,但验证不适用于"show_date"(即使没有设置,点击ok也会转到另一个页面)

我应该做些什么来验证第二个下拉列表中的字段???

我想这就是问题所在:

$( "select#title" ).html( data );

您可以将其更改为:

$( "select#title" ).val( data );

但这取决于数据是什么。你可以看看这里。

您可以尝试这个

$( "#<?php echo Html::getInputId($model, 'show_date');?>" ).html( data );

而不是

$( "select#title" ).html( data );

下面的代码获取HTML元素的动态id。同时从第二个下拉列表中删除您的自定义id,即""id"=>"title""。

<?php echo Html::getInputId($model, 'show_date');?> 

例如:

<?php echo $form->field($model,'stu_padd_state')->dropDownList($stateList,
        [
            'prompt'=>'---Select State---',
            'onchange'=>'
                $.get( "'.Url::toRoute('dependent/astud_p_city').'", { id: $(this).val() } )
                    .done(function( data ) {
                        $( "#'.Html::getInputId($model, 'stu_padd_city').'" ).html( data );
                    }
                );'    
        ]       
            ); 
    ?>
<?php echo $form->field($model,'stu_padd_city')->dropDownList($cityList, ['prompt'=>'---Select City---']);?>