FormContext.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 fieldNames()
  127. {
  128. return $this->_form->schema()->fields();
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public function type($field)
  134. {
  135. return $this->_form->schema()->fieldType($field);
  136. }
  137. /**
  138. * {@inheritDoc}
  139. */
  140. public function attributes($field)
  141. {
  142. $column = (array)$this->_form->schema()->field($field);
  143. $whiteList = ['length' => null, 'precision' => null];
  144. return array_intersect_key($column, $whiteList);
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function hasError($field)
  150. {
  151. $errors = $this->error($field);
  152. return count($errors) > 0;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function error($field)
  158. {
  159. return array_values((array)Hash::get($this->_form->errors(), $field, []));
  160. }
  161. }