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