Skip to content

Commit

Permalink
Fix submitting forms with empty repeatable subforms (#27999)
Browse files Browse the repository at this point in the history
* Fix submitting forms with empty repeatable subforms

* Use the validation rule
  • Loading branch information
SharkyKZ authored Feb 24, 2020
1 parent ac9b425 commit bd13b49
Showing 1 changed file with 15 additions and 32 deletions.
47 changes: 15 additions & 32 deletions libraries/src/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1559,18 +1559,21 @@ protected function filterField($element, $value)
$return = call_user_func($filter, $value);
}

elseif ($element['type'] == 'subform')
elseif ((string) $element['type'] === 'subform')
{
$field = $this->loadField($element);
$subForm = $field->loadSubForm();

if ($field->multiple && !empty($value))
if ($field->multiple)
{
$return = array();

foreach ($value as $key => $val)
if ($value)
{
$return[$key] = $subForm->filter($val);
foreach ($value as $key => $val)
{
$return[$key] = $subForm->filter($val);
}
}
}
else
Expand Down Expand Up @@ -2150,38 +2153,18 @@ protected function validateField(\SimpleXMLElement $element, $group = null, $val
}
}

if ($valid !== false && $element['type'] == 'subform')
if ($valid !== false && (string) $element['type'] === 'subform')
{
$field = $this->loadField($element);
$subForm = $field->loadSubForm();

if ($field->multiple && $value)
{
foreach ($value as $key => $val)
{
$val = (array) $val;

$valid = $subForm->validate($val);
// Load the subform validation rule.
$rule = $this->loadRuleType('SubForm');

if ($valid === false)
{
break;
}
}
}
else
{
$valid = $subForm->validate($value);
}
// Run the field validation rule test.
$valid = $rule->test($element, $value, $group, $input, $this);

if ($valid === false)
// Check for an error in the validation test.
if ($valid instanceof \Exception)
{
$errors = $subForm->getErrors();

foreach ($errors as $error)
{
return $error;
}
return $valid;
}
}

Expand Down

0 comments on commit bd13b49

Please sign in to comment.