2016-02-12 53 views
0

我使用了CRUD并创建了两个模型。我试图在一个视图中创建两种形式。因此,在我的控制器中,我创建了新模型并编写了一个检查表单,如果填充表单以记录它并且两个表单保存两个表单。但它不适合我。这是我的控制器actionCreate:在一个视图中创建两种形式

public function actionCreate() 
{ 
    $model = new UrUserForm(); 
    $userDate= new UserDataForm(); 
    $model->scenario = 'create'; 

    if ($model->load(Yii::$app->request->post())) { 

     try { 
      $model->saveUser(); 
     } catch (Exception $ex) { 
      Yii::$app->session->setFlash('error', Yii::t('app', 'Dane nie zostały zapisane.')); 
     } 

     return $this->redirect(['index']); 
    } elseif(($userDate->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post()))){ 
     try { 
      var_dump($model->saveUser(), $userDate->saveOptionalData()); 
      exit(); 
      $model->saveUser(); 
      $userDate->saveOptionalData(); 

     } catch (Exception $ex) { 
      Yii::$app->session->setFlash('error', Yii::t('app', 'Dane nie zostały zapisane.')); 
     } 


} else { 
     return $this->render('create', [ 
      'model' => $model, 
      'userDate'=> $userDate 
     ]); 
}} 

当我填写这两个表单时不会显示此var_dump。所以我认为我在这个控制器中做了一些错误。也许我会告诉你我的ModelsForm肯定

<?php 

namespace backend\modules\users\models; 

use common\models\User; 
use backend\modules\users\models\UrUser; 
use yii\base\Model; 
use Yii; 
use yii\helpers\Url; 

/** 
* Signup form 
*/ 
class UrUserForm extends Model { 

    public $Login; 
    public $Email; 
    public $Password; 
    public $Sex; 
    public $Country; 
    public $Language; 
    public $Category; 
    public $AccoutType; 
    public $Name; 
    public $Surname; 
    public $BirthDate; 
    public $RulesAccept; 
    public $user; 

    /** 
    * @inheritdoc 
    */ 
    public function rules() { 
     return [ 
      [['Country', 'Language', 'Category'], 'safe'], 
      ['Login', 'filter', 'filter' => 'trim'], 
      [['Login', 'Sex', 'Name', 'Surname', 'BirthDate'], 'required'], 
      ['Login', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Pdany login jest już używany')], 
      ['Login', 'string', 'min' => 2, 'max' => 255], 
      ['Email', 'filter', 'filter' => 'trim'], 
      ['Email', 'required'], 
      ['Email', 'email'], 
      ['BirthDate', 'ageValidator'], 
      ['Email', 'string', 'max' => 255], 
      ['Email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Podany e-mail jest już używany')], 
      ['Password', 'string', 'min' => 6], 
      ['Password', 'required','on' => 'create'], 
     ]; 
    } 

    public function saveUser() { 

     $user = new UrUser(); 
     $user->Login = $this->Login; 
     $user->Email = $this->Email; 
     $user->RulesAccept = 1; 
     $user->Rel_Sex = $this->Sex; 
     $user->Name = $this->Name; 
     $user->BirthDate = $this->BirthDate; 
     $user->Surname = $this->Surname; 
     $user->setPassword($this->Password); 
     $user->generateAuthKey(); 
     $user->Rel_Country = $this->Country; 
     $user->Rel_UserCategory = $this->Category; 
     $user->Rel_Language = $this->Language; 
     $user->status = 10; 
     $this->user = $user; 
     $user->created_at=time(); 
     if ($this->validate() && $user->validate()) { 
      $user->save(); 
      return $user; 
     } 
     return false; 
    } 

    public function updateUser() { 
     $this->user->load($this->toArray(), ''); 
     $this->user->Rel_Country=$this->Country; 
     $this->user->Rel_Language=$this->Language; 
     $this->user->Rel_UserCategory=$this->Category; 
     $this->user->Rel_Sex=$this->Sex; 
     $this->user->updated_at=time(); 
     if (!empty($this->Password)) { 
      $this->user->setPassword($this->Password); 
     } 

     return $this->user->save(); 
    } 

而第二种模式:

<?php 
namespace backend\modules\users\models; 

use common\models\UserData; 
use frontend\modules\settings\models\Profile; 

use yii\base\Model; 
use Yii; 
/** 
* Signup form 
*/ 
class UserDataForm extends Model 
{ 
    public $Address; 
    public $NIP; 
    public $CompanyName; 
    public $Website; 
    public $Phone; 
    public $IsCompany; 
    public $IsPhoneConfirmed; 
    public $CreatedAt; 
    public $UpdateAt; 
    public $Rel_State; 
    public $Rel_Currency; 
    public $IsDeleted; 
    public $Id; 

    /** 
    * @inheritdoc 
    */ 



    public function rules() 
    { 
     return [ 
      [['Address', 'Phone', 'Rel_State', 'Rel_Currency'], 'required'], 
      [['NIP','Id', 'Phone', 'IsCompany', 'IsPhoneConfirmed', 'CreatedAt', 'UpdateAt', 'Rel_State', 'Rel_Currency', 'IsDeleted'], 'integer'], 
      [['Address', 'CompanyName', 'Website'], 'string', 'max' => 45] 
     ]; 
    } 


    public function saveOptionalData() { 

     $model = new UserData(); 
     //$user= 
     $model->Address=$this->Address; 
     $model->Phone=$this->Phone; 
     $model->Rel_State=$this->Rel_State; 
     $model->Rel_Currency= $this->Rel_Currency; 
     $model->NIP=$this->NIP; 
     $model->IsCompany = $this->IsCompany; 
     $model->IsPhoneConfirmed = $this->IsPhoneConfirmed; 
     $model->CompanyName = $this->CompanyName; 
     $model->Website = $this->Website; 

     if ($this->validate() && $model->validate()) { 
      //$user->Rel_RoyalUserData=$model->Id; 
      $model->save(); 
      return $model; 
     } 
     return false; 
    } 

任何人都可以帮我吗?我是Yii的新手,但我试图在文档中搜索答案,但是我失败了。

回答

0

只有一种形式可以在任何时候提交。每个HTML表单标签都有自己的操作,即使它们可以指向相同的操作URL,但一次只能提交一个。如果您想在一个POST操作中处理它们,则必须将所有输入以相同的格式输入。

+0

我该如何做一个表格,以便第二个模型的数据是可选的,尽管它们有必需的字段。我这样做是为了让用户可以填写第一部分并保存它,如果你愿意,也可以根据需要填写其他数据。但正是在这第二个模型中,需要的数据是 – Informer

+0

当我有时间在我的第二个模型时,我需要做些什么,如果用户不想填充可选数据(第二个模型),但在我的第二个模型中,我有所需规则 – Informer

+0

这有点复杂。您可以尝试的一种方法是通过为可选字段调用'yii \ widgets \ ActiveField :: $ enableClientValidation'来阻止Yii生成客户端验证规则,并编写您自己的客户端验证。对于服务器端,仍然可以简单地使用'$ model-> load()'和'$ model-> validate()'。 – slbteam08