FormContext.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\View\Form;
  16. use Cake\Http\ServerRequest;
  17. use Cake\Utility\Hash;
  18. /**
  19. * Provides a context provider for Cake\Form\Form instances.
  20. *
  21. * This context provider simply fulfils the interface requirements
  22. * that FormHelper has and allows access to the request data.
  23. */
  24. class FormContext implements ContextInterface
  25. {
  26. /**
  27. * The request object.
  28. *
  29. * @var \Cake\Http\ServerRequest
  30. */
  31. protected $_request;
  32. /**
  33. * The form object.
  34. *
  35. * @var \Cake\Form\Form
  36. */
  37. protected $_form;
  38. /**
  39. * Constructor.
  40. *
  41. * @param \Cake\Http\ServerRequest $request The request object.
  42. * @param array $context Context info.
  43. */
  44. public function __construct(ServerRequest $request, array $context)
  45. {
  46. $this->_request = $request;
  47. $context += [
  48. 'entity' => null,
  49. ];
  50. $this->_form = $context['entity'];
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function primaryKey()
  56. {
  57. return [];
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. public function isPrimaryKey($field)
  63. {
  64. return false;
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function isCreate()
  70. {
  71. return true;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function val($field, $options = [])
  77. {
  78. $options += [
  79. 'default' => null,
  80. 'schemaDefault' => true
  81. ];
  82. $val = $this->_request->getData($field);
  83. if ($val !== null) {
  84. return $val;
  85. }
  86. $val = $this->_form->getData($field);
  87. if ($val !== null) {
  88. return $val;
  89. }
  90. if ($options['default'] !== null || !$options['schemaDefault']) {
  91. return $options['default'];
  92. }
  93. return $this->_schemaDefault($field);
  94. }
  95. /**
  96. * Get default value from form schema for given field.
  97. *
  98. * @param string $field Field name.
  99. * @return mixed
  100. */
  101. protected function _schemaDefault($field)
  102. {
  103. $field = $this->_form->schema()->field($field);
  104. if ($field) {
  105. return $field['default'];
  106. }
  107. return null;
  108. }
  109. /**
  110. * {@inheritDoc}
  111. */
  112. public function isRequired($field)
  113. {
  114. $validator = $this->_form->getValidator();
  115. if (!$validator->hasField($field)) {
  116. return false;
  117. }
  118. if ($this->type($field) !== 'boolean') {
  119. return $validator->isEmptyAllowed($field, $this->isCreate()) === false;
  120. }
  121. return false;
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function getRequiredMessage($field)
  127. {
  128. $parts = explode('.', $field);
  129. $validator = $this->_form->getValidator();
  130. $fieldName = array_pop($parts);
  131. if (!$validator->hasField($fieldName)) {
  132. return null;
  133. }
  134. $ruleset = $validator->field($fieldName);
  135. $requiredMessage = $validator->getRequiredMessage($fieldName);
  136. $emptyMessage = $validator->getNotEmptyMessage($fieldName);
  137. if ($ruleset->isPresenceRequired() && $requiredMessage) {
  138. return $requiredMessage;
  139. }
  140. if (!$ruleset->isEmptyAllowed() && $emptyMessage) {
  141. return $emptyMessage;
  142. }
  143. return null;
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function getMaxLength($field)
  149. {
  150. $validator = $this->_form->getValidator();
  151. if (!$validator->hasField($field)) {
  152. return null;
  153. }
  154. foreach ($validator->field($field)->rules() as $rule) {
  155. if ($rule->get('rule') === 'maxLength') {
  156. return $rule->get('pass')[0];
  157. }
  158. }
  159. return null;
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function fieldNames()
  165. {
  166. return $this->_form->schema()->fields();
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function type($field)
  172. {
  173. return $this->_form->schema()->fieldType($field);
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function attributes($field)
  179. {
  180. $column = (array)$this->_form->schema()->field($field);
  181. $whiteList = ['length' => null, 'precision' => null];
  182. return array_intersect_key($column, $whiteList);
  183. }
  184. /**
  185. * {@inheritDoc}
  186. */
  187. public function hasError($field)
  188. {
  189. $errors = $this->error($field);
  190. return count($errors) > 0;
  191. }
  192. /**
  193. * {@inheritDoc}
  194. */
  195. public function error($field)
  196. {
  197. return array_values((array)Hash::get($this->_form->getErrors(), $field, []));
  198. }
  199. }