0

我必须在页脚和单个页面中使用一次表单,即index.ctp. 为此,我有一个名为contactforms. 我创建了一个模型Contactform.php

<?php
App::uses('AppModel', 'Model');
/**
 * Contactform Model
 *
 */
class Contactform extends AppModel {
/**
 * Validation rules
 *
 * @var array
 */
      var $useTable = false;

    public $validate = array(
        'firstname' => array(
            'notempty' => array(
                'rule' => array('notempty')             
            )
        ),
        'contactno' => array(
            'notempty' => array(
                'rule' => array('notempty')             
            )
        ),
        'email' => array(
            'notempty' => array(
                'rule' => array('notempty')         
            )
        )
    );
}
?>

我有一个控制器,我试图从中发送电子邮件

<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class ContactformsController extends AppController {


    public function index() {
        $this->Contactform->recursive = 0;
        $this->set('contactforms', $this->paginate());
    }


        public function contact() {

        $email = new CakeEmail();
            if(isset($this->params['requested']) && $this->params['requested']==true)
    {
         if ($this->request->is('post')) 
            {
             $this->Contactform->set($this->request->data);
                if($this->Contactform->save($this->request->data)) 
                {
                 $name=$this->request->data['Contactform']['firstname'];
                 $lastname=$this->request->data['Contactform']['lastname'];
                 $contact=$this->request->data['Contactform']['contactno'];
                 $mail= $this->request->data['Contactform']['email'];
                 $email->from(array($mail => $name));
                     $email->to('abc@gmail.com');                      
                     $message= $this->request->data['Contactform']['message'];
                     $email->subject('Wombats contact form information');        
                 if($email->send($message))
                 {
                    $this->Session->setFlash('Quote Processed..Thank You For Visiting Our Website!!!');
                                     $this->redirect($this->referer());
                    }   
                    }
                 }

               }
        }
}
?>

然后我创建了一个我在页脚和索引文件中使用的元素。 contact.ctp好像

<?php echo $this->Html->css('contactfooter.css');?>

<?php $contactforms = $this->requestAction('Contactforms/contact') ?>
<div class="frm">
<?php echo $this->Form->create('Contactform'); ?> 
<div class="firstrow">
    <div class="first">
        <?php echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','div'=>'firstname','style'=>'width:130px; height:20px;' ));?>
        <?php // echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','style'=>'width:130px; height:20px; float:left; margin-right:5px;','error'=>array('attributes'=>array('wrap'=>'div','class'=>'errorfirst'))));?>
    </div>
    <div class="second">
        <?php echo $this->Form->input('lastname',array('label'=>false,'placeholder'=>'lastname','div'=>'lastname','style'=>'width:140px; height:20px; '));?>
    </div>   
</div>
<!--<div class="secondrow">-->
<?php echo $this->Form->input('contactno',array('label'=>false,'placeholder'=>'contactno','div'=>'contactno','style'=>'width:270px; height:20px; margin-bottom:10px;'));?>
<!--</div>-->

<?php echo $this->Form->input('email',array('label'=>false,'placeholder'=>'email','div'=>'email','style'=>'width:270px; height:20px; '));?>
<?php echo $this->Form->input('message',array('label'=>false,'placeholder'=>'message','div'=>'message','style'=>'width:270px; height:25px;margin-top:10px; '));?>
</div>
<!--<br>-->
<div class="sub">
<?php echo $this->Form->end('SUBMIT'); ?> 
    </div>

当我单击一个表单的提交时,其他表单也会验证。我尝试了很多但不知道如何解决任何人都可以帮忙。

编辑:-@Nunser 我对这些名字感到很困惑,对此感到抱歉。我已经根据你所说的更改了我的代码,但它仍然只验证一种形式。我有一个疑问,根据你的说法,我也应该改变视图和元素,但我只有元素。请你帮我发布我编辑的代码

我将索引页面中的元素称为

<?php echo $this->element('Contactform/contact',array('source'=>'index')); ?>\

并从默认页面作为

<?php echo $this->element('Contactform/contact'); ?>

我的控制器动作是

public function contact() {

        $email = new CakeEmail();

        if(isset($this->params['requested']) && $this->params['requested']==true){

        if ($this->request->is('post')) 

        {
            $index = 'Contactform';
            if (isset($this->request->data['Contactformindex']))
                $index = 'Contactformindex';

        $this->Contactform->set($this->request->data[$index]);

        if($this->Contactform->save($this->request->data[$index])) 

        {

        $name=$this->request->data[$index]['firstname'];

        $lastname=$this->request->data[$index]['lastname'];

        $contact=$this->request->data[$index]['contactno'];

        $mail= $this->request->data[$index]['email'];

        $email->from(array($mail => $name));



        $email->to('skyhi13@gmail.com');               

                $message= $this->request->data[$index]['message'];

                $email->subject('Wombats contact form information');

        //$email->send($message);

        if($email->send($message))

        {
        $this->Session->setFlash('Quote Processed..Thank You For Visiting Our Website!!!');

        //$this->render('/view/elements/quotes/quoteform.ctp');

//        $this->autoRender=FALSE;

        $this->redirect($this->referer());

        }    


        }
        else {
            $this->set('formName',$index);
        }

      }

          }

        }

在元素 ctp 文件中,我更改为

<?php if (!empty($this->validationErrors['Contactform'])) {
     $this->validationErrors[$formName] = $this->validationErrors['Contactform'];
}?>
<div class="frm">
<?php
    if(isset($source)&& $source == 'index')
        echo $this->Form->create('Contactformindex'); 
    else
        echo $this->Form->create('Contactform'); 
 ?>   
<div class="firstrow">
    <div class="first">
        <?php echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','div'=>'firstname','style'=>'width:130px; height:20px;' ));?>
        <?php // echo $this->Form->input('firstname',array('label'=>false,'placeholder'=>'firstname','style'=>'width:130px; height:20px; float:left; margin-right:5px;','error'=>array('attributes'=>array('wrap'=>'div','class'=>'errorfirst'))));?>
    </div>
    <div class="second">
        <?php echo $this->Form->input('lastname',array('label'=>false,'placeholder'=>'lastname','div'=>'lastname','style'=>'width:140px; height:20px; '));?>
    </div>   
</div>
<!--<div class="secondrow">-->
<?php echo $this->Form->input('contactno',array('label'=>false,'placeholder'=>'contactno','div'=>'contactno','style'=>'width:270px; height:20px; margin-bottom:10px;'));?>
<!--</div>-->

<?php echo $this->Form->input('email',array('label'=>false,'placeholder'=>'email','div'=>'email','style'=>'width:270px; height:20px; '));?>
<?php echo $this->Form->input('message',array('label'=>false,'placeholder'=>'message','div'=>'message','style'=>'width:270px; height:25px;margin-top:10px; '));?>
</div>
<!--<br>-->
<div class="sub">
<?php echo $this->Form->end('SUBMIT'); ?> 
    </div>

使用此代码仍然只验证一个表单,而表单是我没有调用source的表单index,当单击index submit button它时,它会验证另一个表单。我不确定我是否必须使用Fromindex您指定的相同名称,这有关系吗?我无法找到我哪里出错了。请提前帮助和感谢。

4

1 回答 1

0

首先,你为什么这样做

<?php $contactforms = $this->requestAction('Contactforms/contact') ?>

在你的元素中?除了减慢处理速度外,我看不到该 requestAction 的使用...

好的,但是您的问题...
由于您正在调用一个元素,因此没有简单的方法可以避免两种表单的验证。通常,当有两个表单对应同一个模型时,不验证两者的方法是像这样更改表单的“名称”

<!--in the first form-->
<?php echo $this->Form->create('Contactform1'); ?> 
<!--in the second form-->
<?php echo $this->Form->create('Contactform2'); ?> 

但是,由于您正在使用元素创建该表单,因此没有简单的方法可以在一个地方而不是在另一个地方更改表单的名称......

所以这是你应该做的(可能有其他方法可以做你想做的事,但这是我能想到的)。当您调用contact.ctp 元素时,将变量传递给它

echo $this->element('contact', array(
   "source" => "index"
));

这样,您就知道您正在调用 index.ctp 中的元素,例如。然后,在元素中,根据变量更改表单声明

//the beginning of your element
<?php
    if (isset($source) && $source == 'index')
      echo $this->Form->create('FromIndex');
    else
      echo $this->Form->create('FromContact');
?>

您还需要修改您的操作,以读取 inFromIndex或 in 中的数据FromContact,具体取决于数据的来源

public function contact() {

   $email = new CakeEmail();
   if(isset($this->params['requested']) && $this->params['requested']==true)  {
     if ($this->request->is('post'))  {

        //change the index of the array depending on where it came form
        $index = 'FromContact';
        if (isset($this->request->data['FromIndex']))
           $index = 'FromIndex';

         $this->Contactform->set($this->request->data[$index]);
            if($this->Contactform->save($this->request->data[$index])) 
            {
             $name=$this->request->data[$index]['firstname'];
             $lastname=$this->request->data[$index]['lastname'];
             $contact=$this->request->data[$index]['contactno'];
             $mail= $this->request->data[$index]['email'];
             $email->from(array($mail => $name));
                 $email->to('abc@gmail.com');                      
                 $message= $this->request->data[$index]['message'];
                 $email->subject('Wombats contact form information');        
             if($email->send($message))
             {
                $this->Session->setFlash('Quote Processed..Thank You For Visiting Our Website!!!');
                                 $this->redirect($this->referer());
                }   
                }
             }

           }
    }
}

当您保存或设置某些内容时(除非是 with或那些类型的函数saveAssociatedsaveAll当您保存多个模型时),无需在要保存的数组中指定模型。

$saveMe = array('User'=>array('name'=>'John'));
$saveMeToo = array('name'=>'John');
$this->User->save($saveMe); //will work
$this->User->save($saveMeToo); //will work too

这就是为什么$this->request->data索引的更改会以任何一种方式起作用的原因。但验证将针对特定索引(FromContactFromIndex)。

注意:我还没有测试过代码,所以一定要检查是否缺少括号、未闭合'和类似的东西。

EDiT
@winnie 指出验证只发生在一种形式上,那是因为我忽略了一些东西。如果在视图中的此变量中设置了某些内容,则验证错误会显示在$this->validationErrors['ModelName']视图中。这就是我错过的。因此,重新更改动作以将$index模型的传递给视图,以便视图知道哪个表单调用了动作,就像这样

public function contact() {

   $email = new CakeEmail();
   if(isset($this->params['requested']) && $this->params['requested']==true)  {
     if ($this->request->is('post'))  {

        //change the index of the array depending on where it came form
        $index = 'FromContact';
        if (isset($this->request->data['FromIndex']))
           $index = 'FromIndex';

         $this->Contactform->set($this->request->data[$index]);
            if($this->Contactform->save($this->request->data[$index])) 
            {
                //this if is exactly the same as the other one
            } else {
                //now, if there's no save, there's some validation errors
                //tell the view which form called this save
                $this->set('formName', $index);

            }
    }
}

现在在视图中,您需要将模型中出现的错误复制到我们提供的“假模型名称”中。在您的视图的第一行执行此操作(以及在元素中)

if (!empty($this->validationErrors['Contactform'])) {
     $this->validationErrors[$formName] = $this->validationErrors['Contactform'];
}
于 2013-07-01T17:28:48.817 回答