如何在同一模型中使用多表验证

时间:2016-11-23 07:41:21

标签: php cakephp model cakephp-3.0

我想在同一页面上验证两个表单并将其插入到Cakephp 3.0中的数据库中。请建议我在不同表格的同一页面中使用它的完美方式

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(); ?>

1 个答案:

答案 0 :(得分:0)

使用beforeSave()在处理实际保存操作之前执行任何验证。或者根据你的代码,我理解的是你需要在2个不同的模型上执行保存但只需要一个表单。如果是这种情况,请在代码中进行以下更改:

  1. 而不是两种形式,将其合并为一种形式。
  2. 然后根据您在控制器中的要求划分字段。
  3. 根据您的方案,通过beforeSave和ajax执行任何验证。
  4. 然后,将同一操作中的实际保存操作处理为多个模型/表。
  5. 如果您需要任何其他帮助,请告诉我。