2015-04-17 111 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, 
       ) 
     ); 
    }