2016-11-23 4 views
-1

同じページで2つのフォームを検証して、Cakephp 3.0のデータベースに挿入したいのですが、別のテーブルの同じページ内で使用するのに最適な方法を教えてください。同一モデル内でバリデーションを行うmutipleテーブルの使い方

AboutController

<?php 
namespace App\Controller; 
use Cake\ORM\TableRegistry; 
use App\Controller\AppController; 
class AboutController extends AppController 
{ 
public function index() 
    { 
     $tempsub2 = $this->Aboutt->newEntity($this->request->data); 
     if ($this->request->is('post')) { 
      $tempsub2 = $this->Aboutt->patchEntity($tempsub2, $this->request->data); 
      if($this->Aboutt->save($tempsub2))  
       { 
     $this->redirect(['controller'=>'Main','action' => 'index']); 
       }  
     } 
     $this->set(compact('tempsub2')); 
     $this->set('_serialize', ['tempsub2']); 

    $tempsub = $this->Aboutt->newEntity($this->request->data); 
     if ($this->request->is('post')) { 
      $tempsub = $this->Aboutt->patchEntity($tempsub, $this->request->data); 
      if($this->Aboutt->save($tempsub))  
       { 
     $this->redirect(['controller'=>'Main','action' => 'index']); 
       }  
     } 
     $this->set(compact('tempsub')); 
     $this->set('_serialize', ['tempsub']); 
    } 
} 

Abouttable.php

<?php 
namespace App\Model\Table; 

use App\Model\Entity\recommenda; 
use Cake\ORM\Query; 
use Cake\ORM\RulesChecker; 
use Cake\Validation\Validator; 
use Cake\ORM\Table; 

class AboutTable extends Table 
{ 


    public function initialize(array $config) 
    { 

     parent::initialize($config); 


     $this->table('about'); 
    } 

    public function validationDefault(Validator $validator) 
    { 
     $validator = new Validator(); 
     $validator 
     ->add('email', 'validFormat', ['rule' => 'email','message' => 'E-mail must be valid']); 
     return $validator; 

    } 
    public function buildRules(RulesChecker $rules) 
    { 
     $rules->add($rules->isUnique(['email'])); 
     return $rules; 
    } 
} 

index.ctp

<?php echo $this->Form->create($tempsub); ?> 
      <div class="col-md-9"> 
       <div class="input-container"> 
       <!-- <input type="text" id='email' name='email' placeholder="Write your E-mail ID and Click Subscribe Button" id="sub-2"> 
       --> 
       <?php 
            echo $this->Form->input('email',array('type' => 'text','label'=>false,"placeholder"=>"Write your E-mail ID and Click Subscribe Button")); ?> 
       <button>Subscribe</button> 
       </div> 
      </div> 
      <?php echo $this->Form->end(); ?> 

<?php echo $this->Form->create($tempsub2); ?> 
      <div class="col-md-9"> 
       <div class="input-container"> 
       <!-- <input type="text" id='email' name='email' placeholder="Write your E-mail ID and Click Subscribe Button" id="sub-2"> 
       --> 
       <?php 
            echo $this->Form->input('email',array('type' => 'text','label'=>false,"placeholder"=>"Write your E-mail ID and Click Subscribe Button")); ?> 
       <button>Subscribe</button> 
       </div> 
      </div> 
      <?php echo $this->Form->end(); ?> 

答えて

0

実際の保存アクションを処理する前にbeforeSave()を使用して検証を実行します。または、コードごとに、理解したことは、2つの異なるモデルで保存する必要があるということですが、単一の形式で保存する必要があります。その場合は、コードに以下の変更を加えてください。

  1. 2つのフォームの代わりに、1つにまとめてください。
  2. 次に、コントローラの要件に従ってフィールドを分割します。
  3. シナリオごとにbeforeSaveとajaxを使用して検証を実行します。
  4. そして、実際の保存アクションを同じアクションから複数のモデル/テーブルに処理します。

他のヘルプが必要な場合は、教えてください。

+0

ありがとうございました。私は、cakephp 3.0でどのようにajaxが動作するのか、またjstはページに単一のフォームがあるときに保存関数を使用して検証する方法を知っています。 あなたはあなたのウェブサイトから入手可能な連絡先のjstテキストuです。誰かが私を適切に導くことができれば、私にとっては助けになるでしょう。 –

+0

ありがとう!答えが有用な場合はアップボートしてください。とにかく、私はあなたのAjaxのニーズにも解決策を共有します。 –

関連する問題