Created
March 26, 2010 20:30
-
-
Save SeanJA/345351 to your computer and use it in GitHub Desktop.
starting of a form class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//I hate typing in htmlspecialchars() all the time | |
function h($var) { | |
return htmlspecialchars($var); | |
} | |
//cake's inflector class | |
require_once('inflector.class.php'); | |
class form { | |
/** | |
* Contains the form as a string | |
* @var string | |
*/ | |
protected $form = ''; | |
/** | |
* Is the form open already? | |
* @var bool | |
*/ | |
private $open = false; | |
/** | |
* Will the form elements be printed out to the screen as you go or stored as a string for later? | |
* @var bool | |
*/ | |
private $echo; | |
/** | |
* The form method | |
* @var string post or get | |
*/ | |
private $method; | |
/** | |
* Automatically add labels to the form elements | |
* @var bool | |
*/ | |
private $label; | |
/** | |
* | |
* @var Inflector | |
*/ | |
private $inflector; | |
/** | |
* | |
* @param bool $echo Will the form elements be echoed out as you make them, or will they be stored for output later? | |
* @param bool $label | |
*/ | |
function __construct($echo = true, $label=true) { | |
$this->inflector = new Inflector(); | |
$this->echo = $echo; | |
$this->label = $label; | |
} | |
/** | |
* Print out the form | |
* @return string | |
*/ | |
public function __toString() { | |
return $this->form; | |
} | |
/** | |
* Get the form as a string | |
* @return string | |
*/ | |
public function getString(){ | |
if($this->echo){ | |
throw new Exception('You cannot get the form this way unless you set $echo to false in the constructor.'); | |
} | |
return $this->form; | |
} | |
/** | |
* Get a $_POST variable | |
* @var string $var | |
*/ | |
protected function post($var) { | |
return array_key_exists($var, $_POST)? $_POST[$var]:null; | |
} | |
/** | |
* Get a $_GET variable | |
* @var string $var | |
*/ | |
protected function get($var) { | |
return array_key_exists($var, $_GET)? $_GET[$var]:null; | |
} | |
/** | |
* Clear the values in the form | |
*/ | |
public function clear() { | |
$this->form = ''; | |
} | |
/** | |
* Create an open form tag | |
* @param string $action where this form will be submitting to (default the current page $_SERVER['PHP_SELF'] | |
* @param string $method the form method (post or get, default get) | |
* @param array $attributes an array of attributes that you will be adding to the element (default empty) | |
*/ | |
public function open($action='', $method='get', array $attributes = array()) { | |
if($this->open) { | |
throw new Exception("The form is already open."); | |
} | |
if(!$action) { | |
$action = $_SERVER['PHP_SELF']; | |
} | |
$this->method = strtolower($method); | |
$this->open = true; | |
$form = '<form action="'.h($action).'" method="'.h($method).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' >'; | |
$this->returnElement($form); | |
} | |
/** | |
* Close the current form | |
*/ | |
public function close() { | |
if(!$this->open){ | |
throw new Exception('The form is not open.'); | |
} | |
$this->returnElement('</form>'); | |
} | |
/** | |
* Add a text input box to your form | |
* @param string $name The name of the element | |
* @param string $value The default value the element will have (the post or get variable will be used if the form returns here) | |
* @param string $id The id of the element (will use the name of the element if not specified) | |
* @param string $label The label for the element, will only be used if $label is true in the constructor (will default to a humanized version of the name if you do not enter it) | |
* @param array $attributes an array of attributes that you will be adding to the element (default empty) | |
*/ | |
public function text($name, $value='', $id='', $label='', array $attributes = array()) { | |
if(!$id) { | |
$id = $name; | |
} | |
if(!$label) { | |
$label = $this->inflector->humanize($name); | |
} | |
if(strpos($label, ':') === false) { | |
$label .= ':'; | |
} | |
unset($attributes['name']); | |
unset($attributes['value']); | |
unset($attributes['id']); | |
$value = ($this->{$this->method}($name))? $_POST[$name]:$value; | |
$form = ''; | |
if($this->label) { | |
$form .= $this->generateLabel($label, $id); | |
} | |
$form .= '<input type="text" name="'.h($name).'" id="'.h($id).'" value="'.h($value).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' />'; | |
$this->returnElement($form); | |
} | |
/** | |
* Add a password input box to your form | |
* @param string $name The name of the element | |
* @param string $id The id of the element (will use the name of the element if not specified) | |
* @param string $label The label for the element, will only be used if $label is true in the constructor (will default to a humanized version of the name if you do not enter it) | |
* @param array $attributes an array of attributes that you will be adding to the element (default empty) | |
*/ | |
public function password($name, $id='', $label='', array $attributes = array()) { | |
if(!$id) { | |
$id = $name; | |
} | |
if(!$label) { | |
$label = $this->inflector->humanize($name); | |
} | |
if(strpos($label, ':') === false) { | |
$label .= ':'; | |
} | |
unset($attributes['name']); | |
unset($attributes['value']); | |
unset($attributes['id']); | |
unset($attributes['autocomplete']); | |
$form = ''; | |
if($this->label) { | |
$form .= $this->generateLabel($label, $id); | |
} | |
$form .= '<input type="password" name="'.h($name).'" id="'.h($id).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' />'; | |
$this->returnElement($form); | |
} | |
/** | |
* Add a credit card input box to your form, automatically has the attribute autocomplete="off" | |
* @link http://www.petefreitag.com/item/481.cfm | |
* @param string $name The name of the element | |
* @param string $id The id of the element (will use the name of the element if not specified) | |
* @param string $label The label for the element, will only be used if $label is true in the constructor (will default to a humanized version of the name if you do not enter it) | |
* @param array $attributes an array of attributes that you will be adding to the element (default empty) | |
*/ | |
public function creditCard($name, $id='', $label='', array $attributes = array()) { | |
if(!$id) { | |
$id = $name; | |
} | |
if(!$label) { | |
$label = $this->inflector->humanize($name); | |
} | |
if(strpos($label, ':') === false) { | |
$label .= ':'; | |
} | |
unset($attributes['name']); | |
unset($attributes['value']); | |
unset($attributes['id']); | |
unset($attributes['autocomplete']); | |
$form = ''; | |
if($this->label) { | |
$form .= $this->generateLabel($label, $id); | |
} | |
$form .= '<input type="text" autocomplete="off" name="'.h($name).'" id="'.h($id).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' />'; | |
$this->returnElement($form); | |
} | |
/** | |
* Add a credit card validation input box to your form, automatically has the attribute autocomplete="off" | |
* @link http://www.petefreitag.com/item/481.cfm | |
* @param string $name The name of the element | |
* @param string $id The id of the element (will use the name of the element if not specified) | |
* @param string $label The label for the element, will only be used if $label is true in the constructor (will default to a humanized version of the name if you do not enter it) | |
* @param array $attributes an array of attributes that you will be adding to the element (default empty) | |
*/ | |
public function ccv($name, $id='', $label='', array $attributes = array()) { | |
if(!$id) { | |
$id = $name; | |
} | |
if(!$label) { | |
$label = $this->inflector->humanize($name); | |
} | |
if(strpos($label, ':') === false) { | |
$label .= ':'; | |
} | |
unset($attributes['name']); | |
unset($attributes['value']); | |
unset($attributes['id']); | |
unset($attributes['autocomplete']); | |
$form = ''; | |
if($this->label) { | |
$form .= $this->generateLabel($label, $id); | |
} | |
$form .= '<input type="text" autocomplete="off" name="'.h($name).'" id="'.h($id).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' />'; | |
$this->returnElement($form); | |
} | |
protected function generateLabel($label, $for) { | |
$form = '<label for="'.h($for).'">'.$label.'</label>'; | |
return $form; | |
} | |
public function label($label, $for='', array $attributes = array()) { | |
$form = '<label for="'.h($for).'" '; | |
$form .= $this->getAttributes($attributes); | |
$form .= ' >'.$label.'</label>'; | |
return $this->returnElement($form); | |
} | |
protected function getAttributes(array $attributes) { | |
$return = ''; | |
foreach($attributes as $k=>$a) { | |
$return .= ' '.h($k).'="'.h($a).'" '; | |
} | |
return $return; | |
} | |
protected function returnElement($form) { | |
if($this->echo) { | |
echo $form; | |
} else { | |
$this->form .= $form; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment