Simple Form to Email
- At February 6, 2008
- By Aaron
- In CakePHP
2
I have constructed a simple contact (to email) form in CakePHP 1.2 Beta. Rather than try to explain it all I have included the code for the model, controller, and view here. One note is I had to add a minLength validation check to the textarea as fields defined as ‘text’ in the model schema do not echo errors by default, as string fields do.
Model
class Contact extends AppModel {
var $name = 'Contact';
var $useTable = false;
var $_schema = array(
'firstName' => array('type' => 'string', 'length' => 30),
'lastName' => array('type' => 'string', 'length' => 30),
'email' => array('type' => 'string', 'length' => 30),
'msg' => array('type' => 'text')
);
var $validate = array(
'firstName' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'firstNameAlphaNumeric'
),
'between' => array(
'rule' => array('between', 1, 30),
'message' => 'firstNameBetween'
)
),
'lastName' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'lastNameAlphaNumeric'
),
'between' => array(
'rule' => array('between', 1, 30),
'message' => 'lastNameBetween'
)
),
'email' => array(
'email' => array(
'rule' => array('email', true),
'required' => true,
'message' => 'emailEmail'
),
'between' => array(
'rule' => array('between', 6, 30),
'message' => 'emailBetween'
)
),
'msg' => array(
'rule' => array('minLength', 1),
'message' => 'msgMinLength'
)
);
}
View
< ?= $form->create('Contact', array('url' => '/contact')) ?> < ?php /* = $form->input('firstName', array('maxlength' => 30)) ?> < ?= $form->input('lastName', array('maxlength' => 30)) ?> < ?= $form->input('email', array('maxlength' => 30)) ?> < ?= $form->input('msg', array('type' => 'textarea')) ?> */ echo $form->inputs(array('firstName', 'lastName', 'email', 'msg')); ?> < ?= $form->end(__('submit', true)) ?>
Controller
class ContactController extends AppController {
var $name = 'Contact';
var $uses = 'Contact';
var $components = array('Email');
function index() {
if (isset($this->data)) {
if ($this->Contact->create($this->data) && $this->Contact->validates()) {
$name = $this->data['Contact']['firstName'] . ' ' . $this->data['Contact']['lastName'];
$from = $this->data['Contact']['email'];
$subject = 'Contact Us';
$msg = $this->data['Contact']['msg'];
$this->Email->sendAs = 'both'; // html, text, both
$this->Email->layout = 'contact'; // views/layouts/email/html/contact.ctp
$this->Email->template = 'contact';
// set view variables as normal
$this->set('from', $name);
$this->set('msg', $msg);
$this->Email->to = 'athies@gmail.com';
$this->Email->from = $from;
$this->Email->subject = $subject;
if ($this->Email->send($msg)) {
$this->Session->setFlash(__('contactUsSent', true));
}
}
}
}
}


Richard@Home
Thanks! Just what I was looking for :-)
links for 2008-02-08 « Richard@Home
[...] Aaron Thies » Blog Archive » Simple Form to Email A simple Form to Email component for CakePHP (tags: cakephp email form) [...]