Skip to content

Instantly share code, notes, and snippets.

@markstory
Created January 9, 2015 03:16
Show Gist options
  • Save markstory/2da2042a5e487ee110c1 to your computer and use it in GitHub Desktop.
Save markstory/2da2042a5e487ee110c1 to your computer and use it in GitHub Desktop.
<h1>Best contact form evar</h1>
<?php
echo $this->Form->create($form);
echo $this->Form->input('name');
echo $this->Form->input('email');
debug($this->Form->isFieldError('email'));
debug($this->Form->error('email'));
echo $this->Form->input('text');
echo $this->Form->button('submit');
echo $this->Form->end();
<?php
namespace App\Controller;
use Cake\Form\Form;
use App\Controller\AppController;
class UsersController extends AppController
{
public function contact()
{
$form = new Form;
$form->schema()
->addField('name', 'string')
->addField('email', 'string')
->addField('text', 'text');
$form->validator()
->requirePresence('name')
->add('name', 'len', ['rule' => ['minLength', 10]])
->add('email', 'format', ['rule' => 'email']);
if ($this->request->is('post')) {
$valid = $form->validate($this->request->data);
if ($valid) {
$this->Flash->success('nice work!');
} else {
$this->Flash->error('nope');
}
}
$this->set(compact('form'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment