Skip to content

Commit

Permalink
Open first accordion if validation exception of control inside
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrecek234 committed Aug 28, 2022
1 parent a120f0f commit 621d33f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions demos/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);

Expand Down
7 changes: 7 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 621d33f

Please sign in to comment.