0

I have two tables :

senderinfos :

  id   |   Name   | email_add |  Address   |
============================================
  1    | Amit     | 1@1.com   | park Street|

receiverinfos:

  id  |  Name  | email_add|  Address  |
=======================================
  1   |  SOS   | 2@2.com      | park1

I am giving the model,view,And Controller Code :

Model

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
    )),


    );

 }
?>

View

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

            }

I want to insert the records in two table with perfect validation.But my validation part in not working perfectly.That means when u see my view page u'll see sender information,receiver information,money which is wrapped by html legend.I want to do validation required field at a time after clicking submit button.In this code whats happening is that when i press submit button sender information validation is working but receiver information portion validation is not working.when i put any value in sender portion,that time receiver information portion's validation works only.What should i have to do?

4

1 回答 1

0

首先; 显然,您的应用程序中的“发送者”和“接收者”之间存在“关系”,但在您的模型/数据库定义中没有。依赖数据库的“自动递增”ID(例如,接收者 1 和发送者 1 属于一起)是不可靠的。自动递增 ID在两个表之间“不同步”,您最终会遇到'receiver 123'实际应该“连接”到'sender 125'.

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

在这两种情况下,您的应用程序中都应该有一个表/模型将两者“连接”起来,否则您只会有一个随机的“发送”和“接收”人的列表,但无法知道哪个人想要汇款给哪个收件人

如果您不打算“重用”发件人/收件人,那么正确的数据库设计应该是这样的:

id| sender_name | sender_email | sender_address | receiver_name | receiver_email | receiver_address
1 | Amid        | 1@1.com      | Parkstreet     | SOS           | 2@2.com        | park 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

于 2013-02-26T23:31:40.640 回答