如何在yii2中使用单个控制器中的多个场景?

时间:2016-03-24 04:48:43

标签: javascript php html css yii2

嗨所有如何在yii2.i中的单个控制器动作中使用多个场景有一个下拉onchange取决于onchange表单字段将改变。所以我使用场景但控制器只接受一个场景。是否可以在一个场景中使用多个场景单动。请帮助..

Model:

 public function rules()
    {
        return [
            //[['discount_type', 'discount_name', 'discount_percentage', 'discount_from_quantity','discount_to_quantity' ,'discount_start_date', 'discount_end_date', 'discount_status', 'access_code', 'code_discount', 'no_of_tickets', 'status','ticket_name'], 'required'],
            [['discount_type','ticket_name'],'required'],
            [['event_id', 'ticket_id', 'discount_from_quantity','discount_to_quantity', 'no_of_tickets', 'status'], 'integer'],
            [['discount_type', 'discount_status','ticket_name'], 'string'],
            [['discount_start_date', 'discount_end_date', 'timestamp'], 'safe'],
            [['discount_name'], 'string', 'max' => 255],
            [['discount_percentage', 'code_discount','access_code'], 'string', 'max' => 100,],
            [['discount_start_date', 'discount_end_date','access_code','no_of_tickets'],'required','on' => 'access'],
            [['discount_start_date', 'discount_end_date','code_discount','no_of_tickets','discount_percentage'],'required','on' => 'code'],
            [['discount_name','discount_percentage','discount_start_date', 'discount_end_date','no_of_tickets','discount_from_quantity','discount_to_quantity'],'required','on' => 'group'],
            ];
           
    }
Controller:

public function actionCreate($id)
    {
		//echo $id;
        $model_discount= new EventticketDiscount();
       $model_discount->scenario = 'code';
        $model_ticketid = new EventTicket_ex();
        
        $ticketid=$model_ticketid->find()->where(['event_id'=>$id])->one();
        
        $ticketname=ArrayHelper::map($model_ticketid->find()->where(['event_id'=>$id])->all(),'ticket_id','ticket_name'); 
        
   
        if ($model_discount->load(Yii::$app->request->post())) {
			
			$model_discount->attributes=$_POST['EventticketDiscount'];

         if(isset($_POST['EventticketDiscount']['ticket_name']) && $_POST['EventticketDiscount']['ticket_name']!==array())
            $model_discount->ticket_name=implode(',',$_POST['EventticketDiscount']['ticket_name']);
            
           
		    $model_discount->event_id = $id;
		    $model_discount->ticket_id = $ticketid->ticket_id;
			$model_discount->save();

    
            return $this->redirect(['view', 'id' => $model_discount->discount_id]);
        } else {
            return $this->renderAjax('create', [
                'model_discount' => $model_discount,'ticketname'=>$ticketname,
            ]);
        }
    }
<?php $form = ActiveForm::begin(); ?>
     <?= $form->field($model_discount, 'discount_type')
                ->dropDownList(ZHtml::enumItem($model_discount, 'discount_type'),['prompt'=>'Select Type','onchange'=>'js:discountShow(this.value)'])
                ->label(FALSE)
                ?> 
           
    <?= $form->field($model_discount, 'event_id')->hiddenInput(['value'=>""])->label(false); ?>

    <?= $form->field($model_discount, 'ticket_id')->hiddenInput(['value'=>""])->label(false); ?>
    
    
    <div id="discountname" style="display:none">
		

    <?= $form->field($model_discount, 'discount_name')->textInput(['maxlength' => true]) ?>
    </div>

    <div id="discountpercentage" style="display:none">
    <?= $form->field($model_discount, 'discount_percentage')->textInput(['maxlength' => true]) ?>
    </div>
    
    <div id="discountdate" style="display:none">

    <?= $form->field($model_discount, 'discount_start_date')->textInput() ?>

    <?= $form->field($model_discount, 'discount_end_date')->textInput() ?>
    </div>


    <div id="accesscode" style="display:none">
    <?= $form->field($model_discount, 'access_code')->textInput(['maxlength' => true]) ?>
    </div>
    
     <div id="discountcode" style="display:none">

    <?= $form->field($model_discount, 'code_discount')->textInput(['maxlength' => true]) ?>
    </div>

   <div id="discountcount" style="display:none">
    <?= $form->field($model_discount, 'no_of_tickets')->textInput() ?>
   </div>
   
    <div id="discountquantity" style="display:none">
    <?= $form->field($model_discount, 'discount_from_quantity')->textInput() ?>
    
    <?= $form->field($model_discount, 'discount_to_quantity')->textInput() ?>
    </div>
    
    <div id="ticketname" style="display:none">
    <?= $form ->field($model_discount, 'ticket_name[]')
           ->checkboxList($ticketname)->label('Select Tickets');?>
    </div>
    
   
   <div id="discountstatus" style="display:none">
    <!-- $form->field($model_discount, 'discount_status')->dropDownList(ZHtml::enumItem($model_discount, 'discount_status'),['prompt' => 'Select Status'])-->

    <?= $form->field($model_discount, 'status')->hiddenInput(['value'=>1])->label(false); ?>

    <?= $form->field($model_discount, 'timestamp')->hiddenInput()->label(false);?>
    </div>

    <div class="form-group">
        <?= Html::submitButton($model_discount->isNewRecord ? 'Create' : 'Update', ['class' => $model_discount->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    <?php ActiveForm::end(); ?>

    </div>

    

<script>
	function discountShow(type)
	{
		alert(type);		
	
		
		if(type=="Group Discount"){
		         document.getElementById('discountname').style.display="block";
		         document.getElementById('ticketname').style.display="block";
                 document.getElementById('discountpercentage').style.display="block";
                 document.getElementById('discountdate').style.display="block";
                 document.getElementById('discountcount').style.display="block";
                 document.getElementById('discountquantity').style.display="block";
                 document.getElementById('discountstatus').style.display="block";
                 document.getElementById('accesscode').style.display="none";
                 document.getElementById('discountcode').style.display="none";
                
               
			}
                else if(type=="Access Code Discount"){
					
				 document.getElementById('discountname').style.display="none";
                 document.getElementById('discountpercentage').style.display="none"; 
                 document.getElementById('discountquantity').style.display="none";
                 document.getElementById('discountcode').style.display="none";
                 document.getElementById('discountdate').style.display="block";
                 document.getElementById('accesscode').style.display="block";
                 document.getElementById('discountcount').style.display="block";
                 document.getElementById('discountstatus').style.display="block";
                 document.getElementById('ticketname').style.display="block";
                 //return false;
				}
		
             else if(type=="Code Discount"){
				 document.getElementById('discountpercentage').style.display="block";
                 document.getElementById('discountdate').style.display="block";
                 document.getElementById('discountcode').style.display="block";
                 document.getElementById('discountcount').style.display="block";
                 document.getElementById('discountstatus').style.display="block";
                 document.getElementById('discountname').style.display="none";
                  document.getElementById('accesscode').style.display="none";
                  document.getElementById('discountquantity').style.display="none";
                  document.getElementById('ticketname').style.display="block";
				}
				
		
			

2 个答案:

答案 0 :(得分:0)

您必须在activeform的选项中设置方案选项。如果你愿意,你可以用jquery来改变它。还要为表单添加一个id,以便客户端验证也能正常工作。

答案 1 :(得分:0)

你可以这样使用

[['username', 'email', 'password'], 'required', 'on' => [self::SCENARIO_REGISTER, self::SCENARIO_OTHER]]