diff --git a/demos/form/form.php b/demos/form/form.php index 25c27402d0..02954aa762 100644 --- a/demos/form/form.php +++ b/demos/form/form.php @@ -38,6 +38,7 @@ Header::addTo($tab, ['Very simple form']); $form = Form::addTo($tab); + $form->addControl('email'); $form->onSubmit(function (Form $form) { // implement subscribe here @@ -56,13 +57,16 @@ $group->addControl('surname'); $group->addControl('gender', [Form\Control\Dropdown::class, 'values' => ['Female', 'Male']]); +$accordionLayout = $form->layout->addSubLayout([\Atk4\Ui\Form\Layout\Section\Accordion::class]); +$details = $accordionLayout->addSection('Validating Field in Accordion'); + // testing 0 value $values = [0 => 'noob', 1 => 'pro', 2 => 'dev']; $form->addControl('description', [Form\Control\Textarea::class])->set(0); $form->addControl('no_description', [Form\Control\Textarea::class])->set(null); $form->addControl('status_optional', [Form\Control\Dropdown::class, 'values' => $values]); -$form->addControl('status_string_not-nullable', [Form\Control\Dropdown::class], ['type' => 'string', 'values' => $values, 'nullable' => false]); -$form->addControl('status_integer_not-nullable', [Form\Control\Dropdown::class], ['type' => 'integer', 'values' => $values, 'nullable' => false]); +$details->addControl('status_string_not-nullable', [Form\Control\Dropdown::class], ['type' => 'string', 'values' => $values, 'nullable' => false]); +$details->addControl('status_integer_not-nullable', [Form\Control\Dropdown::class], ['type' => 'integer', 'values' => $values, 'nullable' => false]); $form->addControl('status_string_required', [Form\Control\Dropdown::class], ['type' => 'string', 'values' => $values, 'required' => true]); $form->addControl('status_integer_required', [Form\Control\Dropdown::class], ['type' => 'integer', 'values' => $values, 'required' => true]); diff --git a/src/Form.php b/src/Form.php index 248dc88618..f4b84087aa 100644 --- a/src/Form.php +++ b/src/Form.php @@ -258,7 +258,14 @@ public function onSubmit(\Closure $callback) return $response; } catch (ValidationException $e) { $response = []; + $openFirstSectionOnError = false; + foreach ($e->errors as $field => $error) { + if (!$openFirstSectionOnError && $this->getControl($field)->getOwner()->getOwner() instanceof \Atk4\Ui\AccordionSection) { + $response[] = $this->getControl($field)->getOwner()->getOwner()->getOwner()->jsOpen($this->getControl($field)->getOwner()->getOwner()); + $openFirstSectionOnError = true; + } + $response[] = $this->error($field, $error); }