Form.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. * The schema used by this form.
  38. *
  39. * @var \Cake\Form\Schema
  40. */
  41. protected $_schema;
  42. /**
  43. * The errors if any
  44. *
  45. * @var array
  46. */
  47. protected $_errors = [];
  48. /**
  49. * The validator used by this form.
  50. *
  51. * @var \Cake\Validation\Validator
  52. */
  53. protected $_validator;
  54. /**
  55. * Get/Set the schema for this form.
  56. *
  57. * This method will call `_buildSchema()` when the schema
  58. * is first built. This hook method lets you configure the
  59. * schema or load a pre-defined one.
  60. *
  61. * @param \Cake\Form\Schema|null $schema The schema to set, or null.
  62. * @return \Cake\Form\Schema the schema instance.
  63. */
  64. public function schema(Schema $schema = null)
  65. {
  66. if ($schema === null && empty($this->_schema)) {
  67. $schema = $this->_buildSchema(new Schema());
  68. }
  69. if ($schema) {
  70. $this->_schema = $schema;
  71. }
  72. return $this->_schema;
  73. }
  74. /**
  75. * A hook method intended to be implemented by subclasses.
  76. *
  77. * You can use this method to define the schema using
  78. * the methods on Cake\Form\Schema, or loads a pre-defined
  79. * schema from a concrete class.
  80. *
  81. * @param \Cake\Form\Schema $schema The schema to customize.
  82. * @return \Cake\Form\Schema The schema to use.
  83. */
  84. protected function _buildSchema(Schema $schema)
  85. {
  86. return $schema;
  87. }
  88. /**
  89. * Get/Set the validator for this form.
  90. *
  91. * This method will call `_buildValidator()` when the validator
  92. * is first built. This hook method lets you configure the
  93. * validator or load a pre-defined one.
  94. *
  95. * @param \Cake\Validation\Validator|null $validator The validator to set, or null.
  96. * @return \Cake\Validation\Validator the validator instance.
  97. */
  98. public function validator(Validator $validator = null)
  99. {
  100. if ($validator === null && empty($this->_validator)) {
  101. $validator = $this->_buildValidator(new Validator());
  102. }
  103. if ($validator) {
  104. $this->_validator = $validator;
  105. }
  106. return $this->_validator;
  107. }
  108. /**
  109. * A hook method intended to be implemented by subclasses.
  110. *
  111. * You can use this method to define the validator using
  112. * the methods on Cake\Validation\Validator or loads a pre-defined
  113. * validator from a concrete class.
  114. *
  115. * @param \Cake\Validation\Validator $validator The validator to customize.
  116. * @return \Cake\Validation\Validator The validator to use.
  117. */
  118. protected function _buildValidator(Validator $validator)
  119. {
  120. return $validator;
  121. }
  122. /**
  123. * Used to check if $data passes this form's validation.
  124. *
  125. * @param array $data The data to check.
  126. * @return bool Whether or not the data is valid.
  127. */
  128. public function validate(array $data)
  129. {
  130. $validator = $this->validator();
  131. $this->_errors = $validator->errors($data);
  132. return count($this->_errors) === 0;
  133. }
  134. /**
  135. * Get the errors in the form
  136. *
  137. * Will return the errors from the last call
  138. * to `validate()` or `execute()`.
  139. *
  140. * @return array Last set validation errors.
  141. */
  142. public function errors()
  143. {
  144. return $this->_errors;
  145. }
  146. /**
  147. * Execute the form if it is valid.
  148. *
  149. * First validates the form, then calls the `_execute()` hook method.
  150. * This hook method can be implemented in subclasses to perform
  151. * the action of the form. This may be sending email, interacting
  152. * with a remote API, or anything else you may need.
  153. *
  154. * @param array $data Form data.
  155. * @return bool False on validation failure, otherwise returns the
  156. * result of the `_execute()` method.
  157. */
  158. public function execute(array $data)
  159. {
  160. if (!$this->validate($data)) {
  161. return false;
  162. }
  163. return $this->_execute($data);
  164. }
  165. /**
  166. * Hook method to be implemented in subclasses.
  167. *
  168. * Used by `execute()` to execute the form's action.
  169. *
  170. * @param array $data Form data.
  171. * @return bool
  172. */
  173. protected function _execute(array $data)
  174. {
  175. return true;
  176. }
  177. /**
  178. * Get the printable version of a Form instance.
  179. *
  180. * @return array
  181. */
  182. public function __debugInfo()
  183. {
  184. $special = [
  185. '_schema' => $this->schema()->__debugInfo(),
  186. '_errors' => $this->errors(),
  187. '_validator' => $this->validator()->__debugInfo()
  188. ];
  189. return $special + get_object_vars($this);
  190. }
  191. }