Form.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Form;
  16. use Cake\Validation\Validator;
  17. /**
  18. * Form abstraction used to create forms not tied to ORM backed models,
  19. * or to other permanent datastores. Ideal for implementing forms on top of
  20. * API services, or contact forms.
  21. *
  22. * ### Building a form
  23. *
  24. * This class is most useful when subclassed. In a subclass you
  25. * should define the `_buildSchema`, `_buildValidator` and optionally,
  26. * the `_execute` methods. These allow you to declare your form's
  27. * fields, validation and primary action respectively.
  28. *
  29. * You can also define the validation and schema by chaining method
  30. * calls off of `$form->schema()` and `$form->validator()`.
  31. *
  32. * Forms are conventionally placed in the `App\Form` namespace.
  33. */
  34. class Form
  35. {
  36. /**
  37. * Schema class.
  38. *
  39. * @var string
  40. */
  41. protected $_schemaClass = '\Cake\Form\Schema';
  42. /**
  43. * The schema used by this form.
  44. *
  45. * @var \Cake\Form\Schema
  46. */
  47. protected $_schema;
  48. /**
  49. * The errors if any
  50. *
  51. * @var array
  52. */
  53. protected $_errors = [];
  54. /**
  55. * The validator used by this form.
  56. *
  57. * @var \Cake\Validation\Validator
  58. */
  59. protected $_validator;
  60. /**
  61. * Get/Set the schema for this form.
  62. *
  63. * This method will call `_buildSchema()` when the schema
  64. * is first built. This hook method lets you configure the
  65. * schema or load a pre-defined one.
  66. *
  67. * @param \Cake\Form\Schema|null $schema The schema to set, or null.
  68. * @return \Cake\Form\Schema the schema instance.
  69. */
  70. public function schema(Schema $schema = null)
  71. {
  72. if ($schema === null && empty($this->_schema)) {
  73. $schema = $this->_buildSchema(new $this->_schemaClass);
  74. }
  75. if ($schema) {
  76. $this->_schema = $schema;
  77. }
  78. return $this->_schema;
  79. }
  80. /**
  81. * A hook method intended to be implemented by subclasses.
  82. *
  83. * You can use this method to define the schema using
  84. * the methods on Cake\Form\Schema, or loads a pre-defined
  85. * schema from a concrete class.
  86. *
  87. * @param \Cake\Form\Schema $schema The schema to customize.
  88. * @return \Cake\Form\Schema The schema to use.
  89. */
  90. protected function _buildSchema(Schema $schema)
  91. {
  92. return $schema;
  93. }
  94. /**
  95. * Get/Set the validator for this form.
  96. *
  97. * This method will call `_buildValidator()` when the validator
  98. * is first built. This hook method lets you configure the
  99. * validator or load a pre-defined one.
  100. *
  101. * @param \Cake\Validation\Validator|null $validator The validator to set, or null.
  102. * @return \Cake\Validation\Validator the validator instance.
  103. */
  104. public function validator(Validator $validator = null)
  105. {
  106. if ($validator === null && empty($this->_validator)) {
  107. $validator = $this->_buildValidator(new Validator());
  108. }
  109. if ($validator) {
  110. $this->_validator = $validator;
  111. }
  112. return $this->_validator;
  113. }
  114. /**
  115. * A hook method intended to be implemented by subclasses.
  116. *
  117. * You can use this method to define the validator using
  118. * the methods on Cake\Validation\Validator or loads a pre-defined
  119. * validator from a concrete class.
  120. *
  121. * @param \Cake\Validation\Validator $validator The validator to customize.
  122. * @return \Cake\Validation\Validator The validator to use.
  123. */
  124. protected function _buildValidator(Validator $validator)
  125. {
  126. return $validator;
  127. }
  128. /**
  129. * Used to check if $data passes this form's validation.
  130. *
  131. * @param array $data The data to check.
  132. * @return bool Whether or not the data is valid.
  133. */
  134. public function validate(array $data)
  135. {
  136. $validator = $this->validator();
  137. $this->_errors = $validator->errors($data);
  138. return count($this->_errors) === 0;
  139. }
  140. /**
  141. * Get the errors in the form
  142. *
  143. * Will return the errors from the last call
  144. * to `validate()` or `execute()`.
  145. *
  146. * @return array Last set validation errors.
  147. */
  148. public function errors()
  149. {
  150. return $this->_errors;
  151. }
  152. /**
  153. * Set the errors in the form.
  154. *
  155. * ```
  156. * $errors = [
  157. * 'field_name' => ['rule_name' => 'message']
  158. * ];
  159. *
  160. * $form->setErrors($errors);
  161. * ```
  162. *
  163. * @since 3.5.1
  164. * @param array $errors Errors list.
  165. * @return $this
  166. */
  167. public function setErrors(array $errors)
  168. {
  169. $this->_errors = $errors;
  170. return $this;
  171. }
  172. /**
  173. * Execute the form if it is valid.
  174. *
  175. * First validates the form, then calls the `_execute()` hook method.
  176. * This hook method can be implemented in subclasses to perform
  177. * the action of the form. This may be sending email, interacting
  178. * with a remote API, or anything else you may need.
  179. *
  180. * @param array $data Form data.
  181. * @return bool False on validation failure, otherwise returns the
  182. * result of the `_execute()` method.
  183. */
  184. public function execute(array $data)
  185. {
  186. if (!$this->validate($data)) {
  187. return false;
  188. }
  189. return $this->_execute($data);
  190. }
  191. /**
  192. * Hook method to be implemented in subclasses.
  193. *
  194. * Used by `execute()` to execute the form's action.
  195. *
  196. * @param array $data Form data.
  197. * @return bool
  198. */
  199. protected function _execute(array $data)
  200. {
  201. return true;
  202. }
  203. /**
  204. * Get the printable version of a Form instance.
  205. *
  206. * @return array
  207. */
  208. public function __debugInfo()
  209. {
  210. $special = [
  211. '_schema' => $this->schema()->__debugInfo(),
  212. '_errors' => $this->errors(),
  213. '_validator' => $this->validator()->__debugInfo()
  214. ];
  215. return $special + get_object_vars($this);
  216. }
  217. }