蛋糕PHP多模型验证

问题描述:

我有两个表:蛋糕PHP多模型验证

senderinfos:

 

    id | Name | email_add | Address | 
============================================ 
    1 | Amit  | [email protected] | park Street| 

receiverinfos:

 

    id | Name | email_add| Address | 
======================================= 
    1 | SOS | [email protected]  | park1 

我给模型,视图和控制器代码:

型号

Senderinfo:

<?php 
App::uses('AppModel', 'Model'); 
/** 
* Admin Login Model 
* 
*/ 
class Senderinfo extends AppModel 
{ 
    public $name='Senderinfo'; 
    public $usetables='senderinfos'; 

    public $validate = array(


     'contact' => 
     array(
     'rule'  => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...) 
     'allowEmpty' => false, 
     'message' => 'Please Enter Contact.' 
    ), 

    'name' => 
     array(
     'rule'  => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...) 
     'allowEmpty' => false, 
     'message' => 'Please Enter Sender Name.' 
    ), 


    'email_add' => array(

      'email_add' => array(
      'rule' => 'email', 
      'allowEmpty' => true, 
      'message' => 'Please Enter Valid Email', 
      'last' => true 
    )), 


    ); 

} 
?> 

Receiveinfo:

<?php 
App::uses('AppModel', 'Model'); 
/** 
* Admin Login Model 
* 
*/ 
class Receiverinfo extends AppModel 
{ 
    public $name='Receiverinfo'; 
    public $usetables='receiverinfos'; 

    public $validate = array(


     'contact' => 
     array(
     'rule'  => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...) 
     'allowEmpty' => false, 
     'message' => 'Please Enter Contact.' 
    ), 

    'name' => 
     array(
     'rule'  => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...) 
     'allowEmpty' => false, 
     'message' => 'Please Enter Sender Name.' 
    ), 


    'email_add' => array(

      'email_add' => array(
      'rule' => 'email', 
      'allowEmpty' => true, 
      'message' => 'Please Enter Valid Email', 
      'last' => true 
    )), 


    ); 

} 
?> 

查看

send_money.ctp

<content> 
      <div class="pg_title txtLeft">Send Money</div> 
       <?php echo $this->Form->create('Agents', array('action' => 'send_money'));?> 

        <div style="display:none;"><input type="hidden" value="POST" name="_method"></div> 
        <fieldset title="SENDER'S INFORMATION"><legend>SENDER'S INFORMATION</legend> 
        <table> 
         <tbody> 
          <tr> 
           <td><label>Contact No.<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Senderinfo.contact',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Sender's Name<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Senderinfo.name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Email Address</label></td> 
           <td><?php echo $this->Form->input('Senderinfo.email_add',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Mailing Address</label></td> 
           <td><?php echo $this->Form->input('Senderinfo.address',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td> 
          </tr> 
          </tbody> 
        </table> 
        </fieldset> 
        <fieldset title=""><legend>RECEIVER'S INFORMATION</legend> 
        <table> 
         <tbody> 
          <tr> 
           <td><label>Contact No.<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Receiverinfo.contact',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Receiver's Name<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Receiverinfo.name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Email Address</label></td> 
           <td><?php echo $this->Form->input('Receiverinfo.email_add',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Mailing Address</label></td> 
           <td><?php echo $this->Form->input('Receiverinfo.address',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td> 
          </tr> 

          </tbody> 
        </table> 
        </fieldset> 

        <fieldset><legend>MONEY TRANSFER</legend> 
        <table> 
         <tbody> 
          <tr> 
           <td><label>Amount(tk)<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Transaction.sending_amount',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 
          <tr> 
           <td><label>Charge(tk)<span class="red">*</span></label></td> 
           <td><?php echo $this->Form->input('Transaction.charge',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px')); ?></td> 
          </tr> 

          <tr> 
          <?php 
          $foo = ""; 
          $options = array('1' => 'Paid','0' => ' Due'); 
          $attributes = array(
           'legend' => false, 
           'value' => true, 
           'checked'=> ($foo == "Paid"), 
          ); 
          ?> 
          <td> 
          <?php echo $this->Form->radio('transaction.status',$options, $attributes);?> 
          </td> 
          </tr> 


         </tbody> 
        </table> 
        </fieldset> 
        <fieldset><legend>CUSTOM MESSAGE</legend> 
        <table> 
         <tbody> 
          <tr> 
           <td style="width: 158px;"><label>Message</label></td> 
           <td><?php echo $this->Form->input('transaction.message',array('label'=>false,'div'=>false,'type'=>'textarea','cols'=>39,'rows'=>3,'class'=>'textarea','style'=>'width:330px')); ?></td> 
          </tr> 
         </tbody> 
        </table> 
        </fieldset> 
        <table> 
         <tbody> 
          <tr> 
           <td colspan="2" align="right"><input type="reset" value="Reset"> | <?php echo $this->Form->submit('SEND', array('div' => false,'formnovalidate' => true));?></td> 
          </tr> 
         </tbody> 
        </table> 
       <?php print $this->Form->end();?> 
    </content> 
    <div class="clear"></div> 



Controller : 

    public function send_money() 
      { 
        $this->layout='agent'; 
        if(empty($this->data) == false) 
        { 
          if($this->Senderinfo->save($this->data)) 
          { 

           $this->Receiverinfo->save($this->data); 
           //$this->Session->setFlash('Data inserted successfully.'); 
           //$this->redirect('send_money'); 
          } 

          /*if($this->Receiverinfo->save($this->data)) 
          { 
           //$this->Session->setFlash('Data inserted successfully.'); 
           $this->redirect('send_money'); 
          } 
          */ 
        } 
        else 
        { 
         //$this->set('errors', $this->Senderinfo->invalidFields()); 
         //$this->set('errors', $this->Receiverinfo->invalidFields());  
        } 

      } 

我想两个表完美validation.But插入记录我的验证部分工作不完美。这意味着当你看到我的视图页你会看到发件人信息,收件人信息,钱包裹在html图例中。我想在点击提交按钮之后一次做验证必填字段。在这段代码中,发生什么事是当我按下提交按钮发件人信息验证正在工作,但接收者信息部分验证不起作用。当我在发件人部分放置任何值时,那个时间接收者信息部分的验证仅起作用。我该怎么办?

首先;显然你的应用程序中'sender'和'receiver'之间存在'关系',但不存在于你的模型/数据库定义中。 依靠数据库的“自动递增”ID(例如,接收方1和发送方1属于一起)不可靠。自动递增ID 在两个表之间出现“不同步”,并且最终会出现'receiver 123'实际上应连接到'sender 125'的情况。

如果我正确理解这个表单的作用是在数据库中创建一个“金钱交易”。发送方和接收方应该(在这种情况下)可能将而不是存储在单独的数据库中,除非您要提供从列表中选择现有发送方/接收方的选项。

在这两种情况下,应用程序中都应该有一个表格/模型,用于“连接”两者,否则只会随机列出一个人“发送”和“接收”资金,但无法知道人想寄钱给收件人

如果你不打算“再利用”的发送/接收的,不是一个正确的数据库设计应该是这样的:

id| sender_name | sender_email | sender_address | receiver_name | receiver_email | receiver_address 
1 | Amid  | [email protected]  | Parkstreet  | SOS   | [email protected]  | park 1 

I将这张表命名为'汇款'和模型'Moneytransfer'

将两者都放在1模型中将简化验证并将保证发件人和收件人信息都存在且正确。

如果你真的想手动保存两个单独的记录(根据我上面的解释,我会强烈建议你这样做),你应该这样做;

  • 验证手动两个记录通过$this->Mymodel->validates($data)
  • 围都在一个事务和回滚保存,如果其中一个出现故障的控制器内手动验证

的更多信息可以在这里找到: http://book.cakephp.org/2.0/en/models/data-validation/validating-data-from-the-controller.html

有关在CakePHP中使用事务的更多信息,请访问: http://book.cakephp.org/2.0/en/models/transactions.html

[更新]

进一步翻阅你的“形”,还有“似乎”是一个“交易”模式呢? 如果这种情况和模型关系在CakePHP中正确定义,您将能够使用Model :: saveAssociated()保存所有相关的记录。这也将附上所有(原子)交易节省

在这里看到: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array