1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\View\Form;
16:
17: use Cake\Http\ServerRequest;
18: use Cake\Utility\Hash;
19:
20: 21: 22: 23: 24: 25:
26: class FormContext implements ContextInterface
27: {
28: 29: 30: 31: 32:
33: protected $_request;
34:
35: 36: 37: 38: 39:
40: protected $_form;
41:
42: 43: 44: 45: 46: 47:
48: public function __construct(ServerRequest $request, array $context)
49: {
50: $this->_request = $request;
51: $context += [
52: 'entity' => null,
53: ];
54: $this->_form = $context['entity'];
55: }
56:
57: 58: 59:
60: public function primaryKey()
61: {
62: return [];
63: }
64:
65: 66: 67:
68: public function isPrimaryKey($field)
69: {
70: return false;
71: }
72:
73: 74: 75:
76: public function isCreate()
77: {
78: return true;
79: }
80:
81: 82: 83:
84: public function val($field, $options = [])
85: {
86: $options += [
87: 'default' => null,
88: 'schemaDefault' => true
89: ];
90:
91: $val = $this->_request->getData($field);
92: if ($val !== null) {
93: return $val;
94: }
95:
96: $val = $this->_form->getData($field);
97: if ($val !== null) {
98: return $val;
99: }
100:
101: if ($options['default'] !== null || !$options['schemaDefault']) {
102: return $options['default'];
103: }
104:
105: return $this->_schemaDefault($field);
106: }
107:
108: 109: 110: 111: 112: 113:
114: protected function _schemaDefault($field)
115: {
116: $field = $this->_form->schema()->field($field);
117: if ($field) {
118: return $field['default'];
119: }
120:
121: return null;
122: }
123:
124: 125: 126:
127: public function isRequired($field)
128: {
129: $validator = $this->_form->getValidator();
130: if (!$validator->hasField($field)) {
131: return false;
132: }
133: if ($this->type($field) !== 'boolean') {
134: return $validator->isEmptyAllowed($field, $this->isCreate()) === false;
135: }
136:
137: return false;
138: }
139:
140: 141: 142:
143: public function getRequiredMessage($field)
144: {
145: $parts = explode('.', $field);
146:
147: $validator = $this->_form->getValidator();
148: $fieldName = array_pop($parts);
149: if (!$validator->hasField($fieldName)) {
150: return null;
151: }
152:
153: $ruleset = $validator->field($fieldName);
154:
155: $requiredMessage = $validator->getRequiredMessage($fieldName);
156: $emptyMessage = $validator->getNotEmptyMessage($fieldName);
157:
158: if ($ruleset->isPresenceRequired() && $requiredMessage) {
159: return $requiredMessage;
160: }
161: if (!$ruleset->isEmptyAllowed() && $emptyMessage) {
162: return $emptyMessage;
163: }
164:
165: return null;
166: }
167:
168: 169: 170:
171: public function getMaxLength($field)
172: {
173: $validator = $this->_form->getValidator();
174: if (!$validator->hasField($field)) {
175: return null;
176: }
177: foreach ($validator->field($field)->rules() as $rule) {
178: if ($rule->get('rule') === 'maxLength') {
179: return $rule->get('pass')[0];
180: }
181: }
182:
183: return null;
184: }
185:
186: 187: 188:
189: public function fieldNames()
190: {
191: return $this->_form->schema()->fields();
192: }
193:
194: 195: 196:
197: public function type($field)
198: {
199: return $this->_form->schema()->fieldType($field);
200: }
201:
202: 203: 204:
205: public function attributes($field)
206: {
207: $column = (array)$this->_form->schema()->field($field);
208: $whiteList = ['length' => null, 'precision' => null];
209:
210: return array_intersect_key($column, $whiteList);
211: }
212:
213: 214: 215:
216: public function hasError($field)
217: {
218: $errors = $this->error($field);
219:
220: return count($errors) > 0;
221: }
222:
223: 224: 225:
226: public function error($field)
227: {
228: return (array)Hash::get($this->_form->getErrors(), $field, []);
229: }
230: }
231: