1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30:
31: abstract class FormComponent extends LabeledMarkupContainer
32: {
33: 34: 35: 36: 37:
38: private $validators = array();
39:
40: private $emptyInput = false;
41:
42: 43: 44:
45: private $rawInput;
46:
47: 48: 49: 50: 51:
52: private $convertedInput;
53:
54: 55: 56:
57: private $required = false;
58:
59: private $disabled = false;
60:
61: 62: 63: 64: 65:
66: public function add(&$object)
67: {
68: if($object instanceof Validator)
69: {
70: $this->addValidator($object);
71: return;
72: }
73: parent::add($object);
74: }
75:
76: 77: 78: 79:
80: public function addValidator(Validator &$validator)
81: {
82: array_push($this->validators, $validator);
83: }
84:
85: 86: 87: 88:
89: public function getLabel()
90: {
91: return "";
92: }
93:
94: protected function onComponentTag(ComponentTag $tag)
95: {
96: parent::onComponentTag($tag);
97: $tag->put('name', $this->getName());
98:
99: if($this->disabled)
100: {
101: $tag->put('disabled', 'disabled');
102: }
103: }
104:
105: 106: 107: 108:
109: public function getForm()
110: {
111: $form;
112: $callback = function($component) use (&$form)
113: {
114: $form = $component;
115: return Component::VISITOR_STOP_TRAVERSAL;
116: };
117: $this->visitParents(Form::getIdentifier(), $callback);
118: return $form;
119: }
120:
121: 122: 123:
124: public function getRawInput()
125: {
126: return $this->rawInput;
127: }
128:
129: 130: 131: 132:
133: public function getRawInputArray()
134: {
135: $input = $this->getRawInput();
136: if($input==null)
137: {
138: return array();
139: }
140: if(!is_array($input))
141: {
142: throw new \InvalidArgumentException('CheckBoxGroup expected raw input to be an array');
143: }
144: return $input;
145: }
146:
147: 148: 149: 150:
151: public function validate()
152: {
153: if($this->disabled)
154: {
155: return;
156: }
157:
158: if($this->isValid())
159: {
160: $this->validateRequired();
161: }
162:
163: if($this->isValid())
164: {
165: $this->convertInput();
166: }
167:
168: if($this->isValid() && !$this->isEmptyInput())
169: {
170: $validatable = new ValidatableFormComponentWrapper($this);
171: foreach($this->validators as $validator)
172: {
173: $validator->validate($validatable);
174: }
175: }
176:
177: if($this->isValid())
178: {
179: $this->valid();
180: }
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: public function updateModel()
191: {
192: if($this->disabled)
193: {
194: return;
195: }
196: $this->setModelObject($this->convertedInput);
197: }
198:
199: 200: 201: 202:
203: public final function processInput()
204: {
205: $this->inputChanged();
206: $valid = $this->validate();
207:
208: if($this->isValid())
209: {
210: $this->updateModel();
211: }
212: }
213:
214: 215: 216: 217: 218:
219: protected abstract function convertInput();
220:
221: public function isValid()
222: {
223: return !$this->hasErrorMessage();
224: }
225:
226: 227: 228: 229:
230: public function setRequired($required)
231: {
232: Args::isBoolean($required, 'required');
233: $this->required = $required;
234: }
235:
236: public function isRequired()
237: {
238: return $this->required;
239: }
240:
241: public function validateRequired()
242: {
243: if($this->isRequired() && !$this->disabled && ($this->rawInput==null || empty($this->rawInput) || (is_array($this->rawInput) && count($this->rawInput)<1)))
244: {
245: $data = array('name' => $this->getId());
246: $message = $this->getLocalizer()->getString($this->getComponentKey('Required'), new ArrayModel($data));
247: $this->error($message);
248: $this->invalid();
249: }
250: }
251:
252: public function getConvertedInput()
253: {
254: return $this->convertedInput;
255: }
256:
257: protected function setConvertedInput($convertedInput)
258: {
259: $this->convertedInput = $convertedInput;
260: }
261:
262: public function getName()
263: {
264: return str_replace('.', '_', $this->getComponentPath());
265: }
266:
267: 268: 269:
270: public function inputChanged()
271: {
272: if($this->disabled)
273: {
274: return;
275: }
276: $this->emptyInput = false;
277:
278: $raw = $this->getRequest()->getPostedParameter(str_replace('[]', '', $this->getName()));
279:
280: if($raw!=null && !empty($raw) || is_array($raw) && count($raw)>0)
281: {
282: $this->rawInput = $raw;
283: }
284: else
285: {
286: $this->emptyInput = true;
287: $this->rawInput = null;
288: }
289: }
290:
291: public function getValue()
292: {
293: $input = null;
294: if($this->disabled)
295: {
296: $input = $this->getModelObjectAsString();
297: }
298: if($this->rawInput==null)
299: {
300: if($this->emptyInput==true)
301: {
302: return null;
303: }
304: else
305: {
306: $input = $this->getModelObjectAsString();
307: }
308: }
309: else
310: {
311: $input = $this->rawInput;
312: }
313: return htmlentities($input);
314: }
315:
316: public function invalid()
317: {
318:
319: }
320:
321: public function valid()
322: {
323:
324: }
325:
326: public function isEmptyInput()
327: {
328: return $this->emptyInput;
329: }
330:
331: protected abstract function validateModel();
332:
333: public function beforeComponentRender()
334: {
335: parent::beforeComponentRender();
336: $this->validateModel();
337: }
338:
339: public function setDisabled($disabled)
340: {
341: Args::isBoolean($disabled, 'disabled');
342: $this->disabled = $disabled;
343: }
344:
345: public function isMultiPart()
346: {
347: return false;
348: }
349: }
350:
351: ?>
352: