Recently I’ve discussed this code in the #cakephp IRC channel to someone who was looking to achieve to create a CakePHP contact form, but was struggling with it a little. I then put my code on the pastebin and decided to post it here as well. Might help other people with the same issue.
What we’ll be doing is creating a simple contact form following CakePHP conventions and using the benefit of Cake’s validation rules. It’s actually quite simple to achieve this.
The first thing you’ll want to be doing is to determine what fields your form will need to have and which of those are required and optional. Once you’re clear on that, you can start writing up your model. The model will not actually save the message to the database (although it could if you’d want to save a copy there), but it will just validate the submitted data. In my case the required fields were name, email and message. Here’s the model that goes with it.
class Contact extends AppModel { var $name = 'Contact'; var $useTable = false; var $validate = array( 'name' => array( 'rule' => 'notEmpty', 'message' => 'You have not entered your name.' ), 'email' => array( 'rule' => 'email', 'message' => 'You have entered an invalid e-mail address.' ), 'message' => array( 'rule' => 'notEmpty', 'message' => 'You did not enter a message.' ) ); }
Next up, in order to actually get our validation rules to work, we’ll need to tell our controller to validate the data before e-mailing it. Furthermore, we’ll need to tell our controller to use Cake’s Email component and set the required parameters to send it. If you’re not familiar with the Email component, I suggest your read the cookbook’s article on it. Here’s what the controller looks like.
class ContactsController extends Controller { var $components = array('Email'); function send() { if(!empty($this->data)) { $this->Contact->set($this->data); if($this->Contact->validates()) { if(!empty($this->data['Contact']['company'])) { $this->Email->from = $this->data['Contact']['company'] . ' - ' . $this->data['Contact']['name'] . ' <' . $this->data['Contact']['email'] . '>'; } else { $this->Email->from = $this->data['Contact']['name'] . ' <' . $this->data['Contact']['email'] . '>'; } $this->Email->to = 'email@example.com'; $this->Email->subject = 'Website request'; $this->Email->send($this->data['Contact']['message']); $this->Session->setFlash('Your message has been sent.'); // Display the success.ctp page instead of the form again $this->render('success'); } else { $this->render('index'); } } } function index() { // Placeholder for index. No actual action here, everything is submitted to the send function. } }
Finally, we need our views to show the form and the success message if the message was sent. This is most likely to look different in your case, but just for the complete picture, I’ll share them with you anyway.
app/views/contacts/index.ctp
<p>Thank you for your interest in our company. Leave a message.</p> <?php echo $this->Form->create('Contact', array('action' => 'send')); ?> <table style="border:none;"> <tr> <td>Name</td> <td><?php echo $this->Form->input('Contact.name', array('label' => false, 'maxlength' => 100, 'size' => 40)); ?></td> </tr> <tr> <td>Company</td> <td><?php echo $this->Form->input('Contact.company', array('label' => false, 'maxlength' => 100, 'size' => 40)); ?></td> </tr> <tr> <td>E-Mail</td> <td><?php echo $this->Form->input('Contact.email', array('label' => false, 'maxlength' => 100, 'size' => 40)); ?></td> </tr> <tr> <td style="vertical-align: top;">Your comment</td> <td><?php echo $this->Form->input('Contact.message', array('label' => false, 'cols' => 50, 'rows' => 10)); ?></td> </tr> <tr> <td colspan="2" align="center"><br><?php echo $this->Form->end('Send'); ?></td> </tr> </table>
app/views/contacts/success.ctp
<?php echo $this->Session->flash(); ?> <p>Thank you, we'll get back to you shortly.</p>
That’s all! A nice and simple contact form with proper field validation.
Funciona perfectamente
You forgot the schema, which can be quite useful for the form helper to generate better input html – see http://www.dereuromark.de/2011/12/15/tools-plugin-part-2-contact-form/ for details.
Dear Jan,
Thanks ever so much for posting this on the Internet, I was also struggling a lot with this and being a cake/PHP novice doesn’t help either. I was wondering if you could help me?
I have used all your code and made sure copy/paste is alright, but still having 2 issues.
- email is not send out, is there another place I need to modify something to get this working?
- When submitting the form, I am not getting the success message but rather an error, any idea why?
Thanks for looking into this.
‘Novice’ Nicklas
Hi Jan,
I fixed the error related to the success message.
In class ContactsController extends Controller {
Changed the row with:
var $components = array(‘Email’);
into
var $components = array(‘Email’,'Session’);
Still no luck with sending
Mail sents now, cakePHP setting
Just wonder how to get the Flashmessage to work?
Hi Nicklas,
The flash message can be set like you normally would, if the mail function returned true, just set the flash message using
-
And make sure you echo it in your View using
Dear jan..
i would like to know about the setting regarding the cake.i.e.,after completion of one program how to go for another??i was executed one program.to execute another program the output shows the previous one.kindly post the solution for this..
Define “executing a program”? Do you want to send more than one e-mail based on the form input?