diff --git a/modules/backend/behaviors/FormController.php b/modules/backend/behaviors/FormController.php index 014a5d6bf..a266fcb86 100644 --- a/modules/backend/behaviors/FormController.php +++ b/modules/backend/behaviors/FormController.php @@ -8,6 +8,7 @@ use Event; use Redirect; use Backend; use Backend\Classes\ControllerBehavior; +use October\Rain\Html\Helper as HtmlHelper; use October\Rain\Router\Helper as RouterHelper; use ApplicationException; use Exception; diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 807c54185..28255f22d 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -783,9 +783,11 @@ class Form extends WidgetBase list($fieldName, $fieldContext) = $this->getFieldName($name); $field = new FormField($fieldName, $label); + if ($fieldContext) { $field->context = $fieldContext; } + $field->arrayName = $this->arrayName; $field->idPrefix = $this->getId(); @@ -793,25 +795,22 @@ class Form extends WidgetBase * Simple field type */ if (is_string($config)) { - if ($this->isFormWidget($config) !== false) { $field->displayAs('widget', ['widget' => $config]); } else { $field->displayAs($config); } - } /* * Defined field type */ else { - $fieldType = $config['type'] ?? null; if (!is_string($fieldType) && $fieldType !== null) { throw new ApplicationException(Lang::get( 'backend::lang.field.invalid_type', - ['type'=>gettype($fieldType)] + ['type' => gettype($fieldType)] )); } @@ -824,7 +823,6 @@ class Form extends WidgetBase } $field->displayAs($fieldType, $config); - } /* @@ -832,12 +830,37 @@ class Form extends WidgetBase */ $field->value = $this->getFieldValue($field); + /* + * Apply the field name to the validation engine + */ + $attrName = implode('.', HtmlHelper::nameToArray($field->fieldName)); + + if ($this->model && method_exists($this->model, 'setValidationAttributeName')) { + $this->model->setValidationAttributeName($attrName, $field->label); + } + /* * Check model if field is required */ if ($field->required === null && $this->model && method_exists($this->model, 'isAttributeRequired')) { - $fieldName = implode('.', HtmlHelper::nameToArray($field->fieldName)); - $field->required = $this->model->isAttributeRequired($fieldName); + // Check nested fields + if ($this->isNested) { + // Get the current attribute level + $nameArray = HtmlHelper::nameToArray($this->arrayName); + unset($nameArray[0]); + + // Convert any numeric indexes to wildcards + foreach ($nameArray as $i => $value) { + if (preg_match('/^[0-9]*$/', $value)) { + $nameArray[$i] = '*'; + } + } + + // Recombine names for full attribute name in rules array + $attrName = implode('.', $nameArray) . ".{$attrName}"; + } + + $field->required = $this->model->isAttributeRequired($attrName); } /*