/SPF_Form.php
PHP | 278 lines | 112 code | 66 blank | 100 comment | 60 complexity | 57860a591cd0a6c5e15879f153c6c9b0 MD5 | raw file
- <?php
- /**
- * SPF_Form
- * @package spf
- * @author Simon Downes <simon@simondownes.co.uk>
- * @copyright Copyright (c) 2007, Simon Downes
- * @license http://www.opensource.org/licenses/mit-license.php
- */
-
- /**
- * SPF_Form.
- *
- * @package spf
- * @author Simon Downes <simon@simondownes.co.uk>
- * @copyright Copyright (c) 2007, Simon Downes
- * @license http://www.opensource.org/licenses/mit-license.php
- */
- class SPF_Form extends SPF_Object {
-
- /**
- * The name (and id) of the form element.
- * @var string
- */
- public $id;
-
- /**
- * The url to which the form is submitted.
- * @var string
- */
- public $action;
-
- /**
- * The HTTP method used to submit the form.
- * @var string
- */
- public $method;
-
- /**
- * An array of field objects that the form consists of.
- * @var array
- */
- public $fields;
-
- /**
- * Constructor.
- *
- * @return void
- */
- public function __construct( $id, $action = '', $method = 'post' ) {
-
- parent::__construct();
-
- $this->id = $id;
- $this->action = $action;
- $this->method = $method;
-
- $this->fields = array();
-
- } // __construct
-
- /**
- * Destructor.
- *
- * @return void
- */
- public function __destruct() {
- parent::__destruct();
- } // __destruct
-
- private function add_field($type, $id, $required, $default_value, $read_only, $datatype, $extra_chars, $max_length, $min, $max, $options, $checked) {
-
- $this->fields[$id] = array('type' => $type,
- 'required' => $required,
- 'value' => $default_value,
- 'read_only' => $read_only,
- 'datatype' => $datatype,
- 'extra_chars' => $extra_chars,
- 'max_length' => $max_length,
- 'min' => $min,
- 'max' => $max,
- 'options' => $options,
- 'checked' => $checked,
- 'error_msg' => '');
-
- } // add_field
-
- /**
- * Adds a text input to the form.
- *
- * @final
- * @return void
- */
- final public function add_text_field( $id, $required, $default_value = '', $datatype = 'custom', $extra_chars = '', $read_only = false, $max_length = 0, $min = '', $max = '' ) {
-
- $this->add_field( 'text', $id, $required, $default_value, $read_only, $datatype, $extra_chars, $max_length, $min, $max, NULL, false );
-
- } // add_text_field
-
- /**
- * Adds a password input to the form.
- *
- * @final
- * @return void
- */
- final public function add_password_field( $id, $required, $default_value = '', $read_only = false, $max_length = 0 ) {
-
- $this->add_field( 'password', $id, $required, $default_value, $read_only, '', '', $max_length, '', '', NULL, false );
-
- } // add_password_field
-
- /**
- * Adds a select box input to the form.
- *
- * @final
- * @return void
- */
- final public function add_select_field( $id, $required, $options, $default_value = '', $read_only = false ) {
-
- $this->add_field( 'select', $id, $required, $default_value, $read_only, '', '', 0, '', '', $options, false );
-
- } // add_select_field
-
- /**
- * Adds a checkbox input to the form.
- *
- * @final
- * @return void
- */
- final public function add_checkbox_field ( $id, $required, $value, $checked = false ) {
-
- $this->add_field( 'checkbox', $id, $required, $value, false, '', '', 0, '', '', NULL, $checked );
-
- } // add_checkbox_field
-
- /**
- * Adds a hidden field to the form.
- *
- * @final
- * @return void
- */
- final public function add_hidden_field ( $id, $default_value ) {
-
- $this->add_field( 'hidden', $id, false, $default_value, false, '', '', 0, '', '', NULL, false );
-
- } // add_hidden_field
-
- final public function to_array() {
-
- return array( 'id' => $this->id,
- 'action' => $this->action,
- 'method' => $this->method,
- 'fields' => $this->fields );
-
- } // to_array
-
- /**
- * Determines if the form was submitted to the current request.
- *
- * @final
- * @return boolean
- */
- final public function is_submitted() {
-
- // check request type matches
- if( $_SERVER['REQUEST_METHOD'] != strtoupper($this->method) )
- return false;
-
- // check form submission field has been set
- if( (($this->method == 'post') && SPF::post('submit_'. $this->id) !== NULL)
- || (($this->method == 'get') && SPF::get('submit_'. $this->id) !== NULL) )
- return false;
-
- return true;
-
- } // is_submitted
-
- /**
- * Determines if any of the fields in the form have validation errors set.
- *
- * @final
- * @return boolean
- */
- final public function has_errors() {
-
- foreach ( $this->fields as &$field ) {
-
- if ($field['error_msg'] != '')
- return true;
-
- } // foreach
-
- return false;
-
- } // has_errors
-
- /**
- * Loads the values of the form from the request array into the field objects.
- *
- * @final
- * @return void
- */
- final public function load_vars() {
-
- // don't load values if the form wasn't actually submitted
- if( !$this->is_submitted() )
- return;
-
- // loop through each field
- foreach ( $this->fields as $field_name => &$field ) {
-
- if( $field['type'] == 'checkbox' ) {
- if( $this->method == 'post' )
- $field['checked'] = SPF::post($field_name) == $field['value'];
- else
- $field['checked'] = SPF::get($field_name) == $field['value'];
- }
- else {
- if( $this->method == 'post' )
- $field['value'] = SPF::post($field_name, $field['value']);
- else
- $field['value'] = SPF::get($field_name, $field['value']);
- }
-
- } // foreach
-
- } // load_vars
-
- /**
- * Checks the contents of each field against it's specified datatype and set an error message if
- * the content is invalid.
- *
- * @return void
- */
- public function validate () {
-
- // loop through each field
- foreach ( $this->fields as &$field ) {
-
- if ($field['required'] && (!isset($field['value']) || $field['value'] === '') )
- $field['error_msg'] = 'Required';
-
- else if( $field['datatype'] == '' || $field['datatype'] == 'custom' )
- $field['error_msg'] = '';
-
- else if( $field['datatype'] == 'alpha' && !SPF_Validator::is_alpha($field['value'], $field['extra_chars']) )
- $field['error_msg'] = 'May only contain letters (A-Z)';
-
- else if( $field['datatype'] == 'alphanumeric' && !SPF_Validator::is_alphanumeric($field['value'], $field['extra_chars']) )
- $field['error_msg'] = 'May only contain letters (A-Z) and digits (0-9)';
-
- else if( $field['datatype'] == 'numeric' && !SPF_Validator::is_numeric($field['value']) )
- $field['error_msg'] = 'Must be a numeric value';
-
- else if( $field['datatype'] == 'integer' && !SPF_Validator::is_integer($field['value']) )
- $field['error_msg'] = 'Must be an integer value';
-
- else if( $field['datatype'] == 'float' && !SPF_Validator::is_float($field['value']) )
- $field['error_msg'] = 'Must be a decimal value';
-
- else if( $field['datatype'] == 'email' && !SPF_Validator::is_email($field['value']) )
- $field['error_msg'] = 'Invalid Email Address';
-
- else if( $field['datatype'] == 'date' && !SPF_Validator::is_date($field['value']) )
- $field['error_msg'] = 'Invalid Date';
-
- else if( $field['datatype'] == 'name' && !SPF_Validator::is_name($field['value']) )
- $field['error_msg'] = 'Invalid Name';
-
- else
- $field['error_msg'] = '';
-
- } // foreach
-
- } // validate
-
- } // SPF_Form
-
- ?>