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

Commit 7f29c92

Browse files
committed
Delegate to InputInterface::isValid when data not exists
1 parent 08fb9ea commit 7f29c92

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
@@ -254,43 +254,13 @@ protected function validateInputs(array $inputs, $data = [], $context = null)
254254
continue;
255255
}
256256

257-
$hasFallback = ($input instanceof Input && $input->hasFallback());
258-
259257
// If input is optional (not required), and value is not set, then ignore.
260258
if (!array_key_exists($name, $data)
261259
&& !$input->isRequired()
262260
) {
263261
continue;
264262
}
265263

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

548-
if (!isset($this->data[$name])) {
518+
if (!array_key_exists($name, $this->data)) {
549519
// No value; clear value in this input
550520
if ($input instanceof InputFilterInterface) {
551521
$input->setData([]);
@@ -557,6 +527,11 @@ protected function populate()
557527
continue;
558528
}
559529

530+
if ($input instanceof Input) {
531+
$input->resetValue();
532+
continue;
533+
}
534+
560535
$input->setValue(null);
561536
continue;
562537
}

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)