Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 49a7aaa

Browse files
committed
Delegate to InputInterface::isValid when data not exists
1 parent 9759253 commit 49a7aaa

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

src/BaseInputFilter.php

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -259,36 +259,6 @@ protected function validateInputs(array $inputs, $data = [], $context = null)
259259
continue;
260260
}
261261

262-
$hasFallback = ($input instanceof Input && $input->hasFallback());
263-
264-
// If the value is required, not present in the data set, and
265-
// has no fallback, validation fails.
266-
if (!array_key_exists($name, $data)
267-
&& $input->isRequired()
268-
&& !$hasFallback
269-
) {
270-
$input->setErrorMessage('Value is required');
271-
$this->invalidInputs[$name] = $input;
272-
273-
if ($input->breakOnFailure()) {
274-
return false;
275-
}
276-
277-
$valid = false;
278-
continue;
279-
}
280-
281-
// If the value is required, not present in the data set, and
282-
// has a fallback, validation passes, and we set the input
283-
// value to the fallback.
284-
if (!array_key_exists($name, $data)
285-
&& $input->isRequired()
286-
&& $hasFallback
287-
) {
288-
$input->setValue($input->getFallbackValue());
289-
continue;
290-
}
291-
292262
// Make sure we have a value (empty) for validation of context
293263
if (!array_key_exists($name, $data)) {
294264
$data[$name] = null;
@@ -543,7 +513,7 @@ protected function populate()
543513
$input->clearRawValues();
544514
}
545515

546-
if (!isset($this->data[$name])) {
516+
if (!array_key_exists($name, $this->data)) {
547517
// No value; clear value in this input
548518
if ($input instanceof InputFilterInterface) {
549519
$input->setData([]);
@@ -555,6 +525,11 @@ protected function populate()
555525
continue;
556526
}
557527

528+
if ($input instanceof Input) {
529+
$input->resetValue();
530+
continue;
531+
}
532+
558533
$input->setValue(null);
559534
continue;
560535
}

src/Input.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,22 @@ public function merge(InputInterface $input)
371371
public function isValid($context = null)
372372
{
373373
$value = $this->getValue();
374+
$hasValue = $this->hasValue();
374375
$empty = ($value === null || $value === '' || $value === []);
375376
$required = $this->isRequired();
376377
$allowEmpty = $this->allowEmpty();
377378
$continueIfEmpty = $this->continueIfEmpty();
378379

380+
if (! $hasValue && $required && ! $this->hasFallback()) {
381+
$this->setErrorMessage('Value is required');
382+
return false;
383+
}
384+
385+
if (! $hasValue && $required && $this->hasFallback()) {
386+
$this->setValue($this->getFallbackValue());
387+
return true;
388+
}
389+
379390
if ($empty && ! $required && ! $continueIfEmpty) {
380391
return true;
381392
}

0 commit comments

Comments
 (0)