2015-04-17 110 views
0

最近,我从CodeIgniter迁移到Yii2.0。Yii 2.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类吗?

回答

1

如果你还不知道,Yii2带有一个叫做Gii的神奇的gode-generator工具。只要您处于dev环境中,您可以使用index.php?r=gii访问它。

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

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

欢迎来到Yii!

+0

我已阅读并使用GII,都是由它产生我的形式,但我想要做的就是我要改变它,手动设计我的表单不是通过使用自动表单呈现yii提供的 –

+0

我不知道你为什么要这样做,但是然后你必须使用普通的php来收集你的数据在动作中,并将它分配给模型'$ model-> yourAttribute = $ _POST ['yourFormElementId']'。编辑:我不能强调以足够有意义的方式做事的价值。如果你自己写,你就不会在你的表单中得到验证反馈,它需要更多的时间,而且不安全。 –

2

//您的表单视图应该是这样的exerpt

<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, 
       ) 
     ); 
    }