Yii 2.0将数据从表单传递到控制器

时间:2015-04-17 08:49:17

标签: php yii2

最近,我从CodeIgniter迁移到Yii2.0。

我有一个用户可以查看和更新​​数据的视图表单 我的表格看起来像这样:

<form role="form" name="damnedForm" action="">
   <div class="form-group">
        <label>SKU Code</label>
        <input class="form-control" name="pSku" value="<?= $model->sku ?>">
   </div>

   <div class="form-group">
         <label>Name</label>
         <input class="form-control" name="pName" value="<?= $model->name ?>">
   </div>

   <div>
         <button type="submit" class="btn btn-danger">Submit me</button>
   </div>
</form>

我有一个名为ProductsController的控制器,方法为:

    function actionUpdate($id) {
      // bla bla bla // 
    }

我的问题是:

如何将表单中的所有数据传递给控制器​​?我是否必须制作手动发布/获取方法并在控制器上捕获它?或者我可以使用ActiveForm类吗?

2 个答案:

答案 0 :(得分:2)

//您对表单的查看应该是这样的摘录

<div class="row">
    <div class="col-sm-6">
        <div class="the-box">
            <h4 class="small-title">Create New Department</h4>
            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'department-Department-form',
                'enableClientValidation' => true,
                'enableAjaxValidation' => false,
            ));
            ?>

            <!--<form role="form">-->
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_name'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_name', array(
                    'id' => 'dept_name',
                    'class' => 'form-control',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_name'); ?>
                </small>

            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_contact'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_contact', array(
                    'id' => 'dept_contact',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_contact'); ?>
                </small>
            </div>




            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_hod'); ?>
                <?php
                echo $form->dropDownList($modelDepartment, 'dept_hod', $employeeList, $htmlOptions = array(
                    'class' => 'form-control chosen-select'
                ));
                ?>

                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_hod'); ?>
                </small>
            </div>
            <div class="form-group">
                <?php echo $form->labelEx($modelDepartment, 'dept_email'); ?>
                <?php
                echo $form->textField($modelDepartment, 'dept_email', array(
                    'id' => 'dept_email',
                    'class' => 'form-control',
//                        'placeholder' => 'ID Number',
                ));
                ?>
                <small style="color: #e9573f;"><?php echo $form->error($modelDepartment, 'dept_email'); ?>
                </small>
            </div>

            <button type="submit" class="btn btn-success"><i class="fa fa-sign-in"></i> Create</button>
            <?php $this->endWidget(); ?>
            <!--</form>-->
        </div><!-- /.the-box -->
    </div>
    <div class="col-sm-6">
        <div class="the-box">
            <?php
            echo '<pre>';
            print_r($dtData);
            ?>
        </div>
    </div>
</div>
//Your Model should be something like this format
class DepartmentForm extends CFormModel {

    public $dept_name;
    public $dept_contact;
    public $dept_hod;
    public $dept_email;

    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('dept_name, dept_contact, dept_hod, dept_email', 'required'),
            array('dept_hod', 'numerical', 'integerOnly' => true),
            array('dept_name, dept_contact', 'length', 'max' => 255),
            array('dept_email', 'length', 'max' => 150),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('dept_id, dept_name, dept_contact, dept_hod, dept_email', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels() {
        return array(
            'dept_id' => 'Dept',
            'dept_name' => 'Department Name',
            'dept_contact' => 'Department Contact',
            'dept_hod' => 'Department HOD',
            'dept_email' => 'Department Email',
        );
    }

}
//Your Controller should be something like this
public function actionManageDepartments() {

        $modelDept = new DepartmentForm(); //Initialize the model above
        $sql = new TSqlResource();


        //Handles the Work Profile
        if (Yii::app()->request->getPost('DepartmentForm')) {
            $modelDept->attributes = Yii::app()->request->getPost('DepartmentForm');
            if ($modelDept->validate()) {
                $data = array(
//                    dept_name, dept_contact, dept_hod, dept_email
                    'dept_name' => $modelDept->attributes ['dept_name'],
                    'dept_contact' => $modelDept->attributes ['dept_contact'],
                    'dept_hod' => $modelDept->attributes ['dept_hod'],
                    'dept_email' => $modelDept->attributes ['dept_email'],
                    'created_by' => Yii::app()->session['user']['profile_id'],
                );
                //insert into database
                $dataSql = $sql->postDepartment($data);

                if ($dataSql == true) {
                    YII::app()->user->setFlash('alert alert-success', ' Successfully Created <strong>' . $data['dept_name'] . '</strong>  Department. ');
                } else {
                    YII::app()->user->setFlash('alert alert-danger', 'Sorry  an Error Occured while adding <strong>' . $data['dept_name'] . '</strong> Department. Contact Admin for assistance ');
                }
            }
        }
//        #end work profile post
        $this->render('manageDepartments', array(
            'modelDepartment' => $modelDept,
            'dtData' => $dtData,
                )
        );
    }

答案 1 :(得分:1)

如果您还不知道,Yii2附带了一个名为Gii的梦幻gode生成器工具。只要您处于index.php?r=gii环境中,就可以使用dev访问它。

如果您使用此工具为模型创建CRUD,则可以查看代码如何在视图和控制器中编写和收集表单。

我推荐这种方法,因为它是做表格的“yii-way”。

欢迎来到Yii!