0

I'm new to Yii framework.I'm using the form.php to update the fields of the table. So now I use this form with three submit buttons - [Save, Accept, Reject]. The form now has the following fields.

<div class="row">
    <?php //$model->ReviewedDate=date('Y-m-d H:i:s');?>
    <?php echo $form->labelEx($model,'ReviewedDate'); ?>
    <?php echo $form->textField($model,'ReviewedDate',array('value'=>'0000-00-00 00:00:00','readonly' => true));te  ?>
    <?php echo $form->error($model,'ReviewedDate'); ?>
            </div>
<div class="row">
    <?php echo $form->labelEx($model,'Approved'); ?>
    <?php echo $form->textField($model,'Approved'); ?>
    <?php echo $form->error($model,'Approved'); ?>
</div>    
<div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('confirm' => 'Are you sure to save')); ?></div>

Above there is Approved field.Now, when I click on save all the other fields has to be updated except for approved. (Approved is 0 by default). So when I click on Approve button it should update Approved as well as other fields. When I click on Reject, it should update the approved field to 0. How can I do this.

user2770039
  • 59
  • 1
  • 2
  • 14

3 Answers3

3

You can use three submit button and can manage functionality as per that. In the form create 3 buttons as per below :

<?php echo CHtml::submitButton('Save', array('name' => 'save')); ?>
<?php echo CHtml::submitButton('Accept', array('name' => 'accept')); ?>
<?php echo CHtml::submitButton('Reject', array('name' => 'reject')); ?>

In the controller check which button is clicked as per below :

<?php 
if(isset($_POST['save'])){
    //save submit button is click and code for save button will be here    
}
if(isset($_POST['accept'])){
    //accept submit button is click and code for accept button will be here    
}
if(isset($_POST['reject'])){
    //reject submit button is click and code for reject button will be here    
} ?>

All the best :)

Maulik Bhojani
  • 442
  • 4
  • 9
  • `=Html::submitButton('Disconnect', ['id'=>'d','name'=>'dco','class' => 'btn btn-primary']);?>` and in contoller `var_dump(isset($_POST['dco'])); die();` gives me `bool(false)` – Moeez May 26 '19 at 10:33
0

Instead of having 3 submit buttons, I'd suggest you use a dropdown list so your users can pick the desired action. Then you check for the value of the dropdown control in order to either "Save", "Accept" or "Reject".

echo CHtml::dropDownList('action', '', array('Accept', 'Reject'));

And in your controller:

if (isset($_POST['ModelName'])) {
    switch ($_POST['action']) {
        case 'Accept':
            # code for Acceptance
            break;

        case 'Reject':
            # code for Rejection
            break;
    }
    //Continue with Saving the Model data here
}
  • `=Html::submitButton('Disconnect', ['id'=>'d','name'=>'dco','class' => 'btn btn-primary']);?>` and in contoller `var_dump(isset($_POST['dco'])); die();` gives me `bool(false)` – Moeez May 26 '19 at 10:33
0

You can add a hiddenField with the action:

<?php echo $form->hiddenField($model, 'typeSubmit'); ?> // Add 'typeSubmit' attribute on the model

And 3 submit buttons. Each button puts on the hidden field the type of Submit.

<?php echo CHtml::submitButton('Save', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("save");')); ?> // #ModelName = $model name class.
<?php echo CHtml::submitButton('Accept', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("accept");')); ?>
<?php echo CHtml::submitButton('Reject', array('class'=>'btn','onclick'=>'$("#ModelName_typeSubmit").val("reject");')); ?>
Daniel Vaquero
  • 1,315
  • 1
  • 8
  • 13
  • I tried this but does not work - hiddenField($model, 'Approved'); ?> 'btn','onclick'=>'$("MessageTemplate_Approved").val("save");')); ?> 'btn','onclick'=>'$("MessageTemplate_Approved").val("accept");')); ?> 'btn','onclick'=>'$("MessageTemplate_Approved").val("reject");')); ?> – user2770039 Sep 27 '13 at 10:14