PageRenderTime 24ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/SPF_Form.php

http://spf-php.googlecode.com/
PHP | 278 lines | 112 code | 66 blank | 100 comment | 60 complexity | 57860a591cd0a6c5e15879f153c6c9b0 MD5 | raw file
  1. <?php
  2. /**
  3. * SPF_Form
  4. * @package spf
  5. * @author Simon Downes <simon@simondownes.co.uk>
  6. * @copyright Copyright (c) 2007, Simon Downes
  7. * @license http://www.opensource.org/licenses/mit-license.php
  8. */
  9. /**
  10. * SPF_Form.
  11. *
  12. * @package spf
  13. * @author Simon Downes <simon@simondownes.co.uk>
  14. * @copyright Copyright (c) 2007, Simon Downes
  15. * @license http://www.opensource.org/licenses/mit-license.php
  16. */
  17. class SPF_Form extends SPF_Object {
  18. /**
  19. * The name (and id) of the form element.
  20. * @var string
  21. */
  22. public $id;
  23. /**
  24. * The url to which the form is submitted.
  25. * @var string
  26. */
  27. public $action;
  28. /**
  29. * The HTTP method used to submit the form.
  30. * @var string
  31. */
  32. public $method;
  33. /**
  34. * An array of field objects that the form consists of.
  35. * @var array
  36. */
  37. public $fields;
  38. /**
  39. * Constructor.
  40. *
  41. * @return void
  42. */
  43. public function __construct( $id, $action = '', $method = 'post' ) {
  44. parent::__construct();
  45. $this->id = $id;
  46. $this->action = $action;
  47. $this->method = $method;
  48. $this->fields = array();
  49. } // __construct
  50. /**
  51. * Destructor.
  52. *
  53. * @return void
  54. */
  55. public function __destruct() {
  56. parent::__destruct();
  57. } // __destruct
  58. private function add_field($type, $id, $required, $default_value, $read_only, $datatype, $extra_chars, $max_length, $min, $max, $options, $checked) {
  59. $this->fields[$id] = array('type' => $type,
  60. 'required' => $required,
  61. 'value' => $default_value,
  62. 'read_only' => $read_only,
  63. 'datatype' => $datatype,
  64. 'extra_chars' => $extra_chars,
  65. 'max_length' => $max_length,
  66. 'min' => $min,
  67. 'max' => $max,
  68. 'options' => $options,
  69. 'checked' => $checked,
  70. 'error_msg' => '');
  71. } // add_field
  72. /**
  73. * Adds a text input to the form.
  74. *
  75. * @final
  76. * @return void
  77. */
  78. final public function add_text_field( $id, $required, $default_value = '', $datatype = 'custom', $extra_chars = '', $read_only = false, $max_length = 0, $min = '', $max = '' ) {
  79. $this->add_field( 'text', $id, $required, $default_value, $read_only, $datatype, $extra_chars, $max_length, $min, $max, NULL, false );
  80. } // add_text_field
  81. /**
  82. * Adds a password input to the form.
  83. *
  84. * @final
  85. * @return void
  86. */
  87. final public function add_password_field( $id, $required, $default_value = '', $read_only = false, $max_length = 0 ) {
  88. $this->add_field( 'password', $id, $required, $default_value, $read_only, '', '', $max_length, '', '', NULL, false );
  89. } // add_password_field
  90. /**
  91. * Adds a select box input to the form.
  92. *
  93. * @final
  94. * @return void
  95. */
  96. final public function add_select_field( $id, $required, $options, $default_value = '', $read_only = false ) {
  97. $this->add_field( 'select', $id, $required, $default_value, $read_only, '', '', 0, '', '', $options, false );
  98. } // add_select_field
  99. /**
  100. * Adds a checkbox input to the form.
  101. *
  102. * @final
  103. * @return void
  104. */
  105. final public function add_checkbox_field ( $id, $required, $value, $checked = false ) {
  106. $this->add_field( 'checkbox', $id, $required, $value, false, '', '', 0, '', '', NULL, $checked );
  107. } // add_checkbox_field
  108. /**
  109. * Adds a hidden field to the form.
  110. *
  111. * @final
  112. * @return void
  113. */
  114. final public function add_hidden_field ( $id, $default_value ) {
  115. $this->add_field( 'hidden', $id, false, $default_value, false, '', '', 0, '', '', NULL, false );
  116. } // add_hidden_field
  117. final public function to_array() {
  118. return array( 'id' => $this->id,
  119. 'action' => $this->action,
  120. 'method' => $this->method,
  121. 'fields' => $this->fields );
  122. } // to_array
  123. /**
  124. * Determines if the form was submitted to the current request.
  125. *
  126. * @final
  127. * @return boolean
  128. */
  129. final public function is_submitted() {
  130. // check request type matches
  131. if( $_SERVER['REQUEST_METHOD'] != strtoupper($this->method) )
  132. return false;
  133. // check form submission field has been set
  134. if( (($this->method == 'post') && SPF::post('submit_'. $this->id) !== NULL)
  135. || (($this->method == 'get') && SPF::get('submit_'. $this->id) !== NULL) )
  136. return false;
  137. return true;
  138. } // is_submitted
  139. /**
  140. * Determines if any of the fields in the form have validation errors set.
  141. *
  142. * @final
  143. * @return boolean
  144. */
  145. final public function has_errors() {
  146. foreach ( $this->fields as &$field ) {
  147. if ($field['error_msg'] != '')
  148. return true;
  149. } // foreach
  150. return false;
  151. } // has_errors
  152. /**
  153. * Loads the values of the form from the request array into the field objects.
  154. *
  155. * @final
  156. * @return void
  157. */
  158. final public function load_vars() {
  159. // don't load values if the form wasn't actually submitted
  160. if( !$this->is_submitted() )
  161. return;
  162. // loop through each field
  163. foreach ( $this->fields as $field_name => &$field ) {
  164. if( $field['type'] == 'checkbox' ) {
  165. if( $this->method == 'post' )
  166. $field['checked'] = SPF::post($field_name) == $field['value'];
  167. else
  168. $field['checked'] = SPF::get($field_name) == $field['value'];
  169. }
  170. else {
  171. if( $this->method == 'post' )
  172. $field['value'] = SPF::post($field_name, $field['value']);
  173. else
  174. $field['value'] = SPF::get($field_name, $field['value']);
  175. }
  176. } // foreach
  177. } // load_vars
  178. /**
  179. * Checks the contents of each field against it's specified datatype and set an error message if
  180. * the content is invalid.
  181. *
  182. * @return void
  183. */
  184. public function validate () {
  185. // loop through each field
  186. foreach ( $this->fields as &$field ) {
  187. if ($field['required'] && (!isset($field['value']) || $field['value'] === '') )
  188. $field['error_msg'] = 'Required';
  189. else if( $field['datatype'] == '' || $field['datatype'] == 'custom' )
  190. $field['error_msg'] = '';
  191. else if( $field['datatype'] == 'alpha' && !SPF_Validator::is_alpha($field['value'], $field['extra_chars']) )
  192. $field['error_msg'] = 'May only contain letters (A-Z)';
  193. else if( $field['datatype'] == 'alphanumeric' && !SPF_Validator::is_alphanumeric($field['value'], $field['extra_chars']) )
  194. $field['error_msg'] = 'May only contain letters (A-Z) and digits (0-9)';
  195. else if( $field['datatype'] == 'numeric' && !SPF_Validator::is_numeric($field['value']) )
  196. $field['error_msg'] = 'Must be a numeric value';
  197. else if( $field['datatype'] == 'integer' && !SPF_Validator::is_integer($field['value']) )
  198. $field['error_msg'] = 'Must be an integer value';
  199. else if( $field['datatype'] == 'float' && !SPF_Validator::is_float($field['value']) )
  200. $field['error_msg'] = 'Must be a decimal value';
  201. else if( $field['datatype'] == 'email' && !SPF_Validator::is_email($field['value']) )
  202. $field['error_msg'] = 'Invalid Email Address';
  203. else if( $field['datatype'] == 'date' && !SPF_Validator::is_date($field['value']) )
  204. $field['error_msg'] = 'Invalid Date';
  205. else if( $field['datatype'] == 'name' && !SPF_Validator::is_name($field['value']) )
  206. $field['error_msg'] = 'Invalid Name';
  207. else
  208. $field['error_msg'] = '';
  209. } // foreach
  210. } // validate
  211. } // SPF_Form
  212. ?>