Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CakeForm, an easy way to define custom cakephp forms
<?php
class CakeForm {
var $_boundValues = array();
var $_errors = null;
var $_formHelper = null;
var $_execute = null;
var $_format = 'ul';
var $_fields = array();
/**
* Labels for all fields
*
* Fields to not need to be set here in order to have labels
* If a field is not set, label defaults to Inflected version
* of the fieldName
*
* If set to null, then no label is created
*
* @var array
* @todo implement
*/
var $_labels = array();
/**
* Rules to implement for each field
*
* Rules are implemented exactly as model validation
* rules are implemented
*
* @var array
* @todo implement
*/
var $_rules = array();
function bindValues() {
}
function errors() {}
function isValid() {}
/**
* Executes an internally
*
* @return void
* @author Jose Diaz-Gonzalez
*/
function execute() {
if (!empty($this->_execute)) {
return $this->{$this->_execute}($this->_boundValues);
}
}
/**
* Constructs the inner portion of a form, sans create/input
*
* @return void
* @author Jose Diaz-Gonzalez
*/
function __toString() {
$output = array();
foreach ($this->_fields as $field => $type) {
$input = $this->_input($field, $type);
if ($this->_format === 'table') {
$output[] = "<tr><td>{$input}</td></tr>";
} else if (in_array($this->_format, array('ol', 'ul'))) {
$output[] = "<li>{$input}</li>";
} else if ($this->_format === 'span') {
$output[] = "<span>{$input}</span>";
}
}
if ($this->_format === 'table') {
array_unshift($ouput, '<table><tbody>');
$output[] = '</table></tbody>';
} else if (in_array($this->_format, array('ol', 'ul'))) {
array_unshift($ouput, '<ol>');
$output[] = '</ol>';
} else if ($this->_format === 'span') {
array_unshift($ouput, '<ul>');
$output[] = '</ul>';
}
return implode($output);
}
function _input($fieldName, $options = array()) {
if (is_string($options)) $options = array('type' => $options);
if (!$this->_formHelper) {
App::import('Helper', 'Form');
$this->_formHelper = new FormHelper;
}
switch ($options['type']) {
case 'hidden':
$input = $this->_formHelper->hidden($fieldName, $options);
$format = array('input');
break;
case 'checkbox':
$input = $this->_formHelper->checkbox($fieldName, $options);
break;
case 'radio':
$radioOptions = (isset($options['options'])) ? (array) $options['options'] : array();
$input = $this->_formHelper->radio($fieldName, $radioOptions, $options);
break;
case 'text':
case 'password':
case 'file':
$input = $this->_formHelper->{$type}($fieldName, $options);
break;
case 'select':
$options += array('options' => array());
$list = $options['options'];
unset($options['options']);
$selected = (isset($options['selected'])) ? $options['selected'] : null;
$input = $this->_formHelper->select($fieldName, $list, $selected, $options);
break;
case 'textarea':
default:
$input = $this->_formHelper->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
break;
}
return $input;
}
}
<?php
class LoginForm extends CakeForm {
var $_name = 'Form';
var $_execute = 'authenticate'; // Defaults to null, or a function in this class
var $_format = 'ul'; // [table, ol, ul, span, none]
var $_fields = array(
'username' => 'text',
'password' => 'password',
);
var $_labels = array(
'username' => 'Email or Username',
'password' => 'Password'
);
var $_rules = array(
'username' => array(
'notempty' => array(
'message' => 'Cannot be left empty'),
'minlength' => array(
'length' => 4,
'message' => 'Must be at least 4 characters'
),
'string' => array(
'message' => 'Invalid input'
),
),
'password' => array(
'string' => array(
'message' => 'Invalid input'
),
)
);
function authenticate($params) {
$User = ClassRegistry::init('User');
return $User->authenticate($params['username'], $params['password']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment