1
大家好如何在yii2.i中的單個控制器動作中使用多個場景有一個下拉onchange取決於onchange表單字段將change.so我使用的場景,但控制器只接受一個scenario.is它可以在一個單一的情況下使用多個場景action.please幫助..如何在yii2中的單個控制器中使用多個場景?
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)
{
\t \t //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())) {
\t \t \t
\t \t \t $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']);
\t \t $model_discount->event_id = $id;
\t \t $model_discount->ticket_id = $ticketid->ticket_id;
\t \t \t $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">
\t \t
<?= $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>
\t function discountShow(type)
\t {
\t \t alert(type); \t \t
\t
\t \t
\t \t if(type=="Group Discount"){
\t \t document.getElementById('discountname').style.display="block";
\t \t 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";
\t \t \t }
else if(type=="Access Code Discount"){
\t \t \t \t \t
\t \t \t \t 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;
\t \t \t \t }
\t \t
else if(type=="Code Discount"){
\t \t \t \t 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";
\t \t \t \t }
\t \t \t \t
\t \t
\t \t \t
無法理解你能請解釋一下代碼 – KanimozhiPalanisamy