Two forms from the same model. Both forms are checked when one form is transferred.

I need to use the form once in the footer and on a separate page, i.e. index.ctp . For this, I have a database table called contactforms . I created a Contactform.php model

 <?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') ) ) ); } ?> 

I have a controller from where I'm trying to send an email

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

And then I created an element that I used in the footer and then in the index file. contact.ctp looks like

 <?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> 

When I click the submit button of one form, another form is also checked. I tried a lot, but don’t know how to fix it. Someone can help.

EDIT: - @Nunser I am very confused by these names, sorry for that. I changed my code according to what I said, but still checked only one form. I have doubts, according to you, I have to change my mind and elements, but I just have elements. Please help me in publishing my edited code.

I named the item from the index page

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

and from the default page

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

my controller action

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

In the ctp file of the elements I changed as

 <?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> 

Using this code, he still checked only one form, and the form is what I called without source , like index , and when I click on the index submit button it checks the other form. I'm not sure how I need to use the same Fromindex name as you specify, it doesn't matter. I can not find where I am wrong. Please help and thanks in advance.

0
source share
1 answer

First, why are you doing this?

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

in your item? I do not see the use of this requestAction, except to slow down processing ...

Ok, but your problem ...
Since you are invoking an element, there is no easy way to avoid validating both forms. Usually, when there are two forms that correspond to the same model, the way to not check both is to change the "name" of a form like this

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

But, since you create this form using an element, there is no easy way to change the name of the form in one place, and not in another ...

So this is what you should do (there may be other ways to do what you want, but this is what I can think of). When you call the contact.ctp element, pass it a variable

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

With this, you know that you are calling an element from index.ctp, for example. Then in the element change the form declaration depending on the variable

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

You will also need to change the action, read the data in FromIndex or In FromContact , depending on where it came from

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

When you save or set something (if only with saveAssociated , saveAll or such functions, when you save several models), there is no need to specify the model in the array to be saved.

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

This is why changing the indexes of $this->request->data will work anyway. But validation will be for a specific index ( FromContact or FromIndex ).

Note. I have not tested the code, so be sure to check the missing bracket closed ' and those things.

EDIT
@winnie pointed out that validation occurred only for one form, and that is because I forgot something. Validation errors are displayed in the view if there is something specified in the variable $this->validationErrors['ModelName'] . And this is what I missed. So, re-modify the action to pass the $index model into the view, so the view knows what form is called an action, like this

 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 no save, there some validation errors //tell the view which form called this save $this->set('formName', $index); } } } 

Now in the view you need to copy the errors that you get in the model into the "fake model name" we gave the form. In the first line of your view do this (and in the element too)

 if (!empty($this->validationErrors['Contactform'])) { $this->validationErrors[$formName] = $this->validationErrors['Contactform']; } 
0
source

Source: https://habr.com/ru/post/945494/


All Articles