From a70260ca41ea6e209f35f2f11a272a0a48b935ce Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Thu, 5 Dec 2019 23:42:51 +0800 Subject: [PATCH] New method `addAlert` on OptionForm --- app/Services/OptionForm.php | 21 ++++++++++++++++++++- resources/views/forms/form.twig | 15 +++++++++++++++ tests/ServicesTest/OptionFormTest.php | 17 ++++++++++++++++- 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index 6427a9fb..e0b64dec 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -27,6 +27,7 @@ class OptionForm protected $buttons = []; protected $messages = []; + protected $alerts = []; protected $hookBefore; protected $hookAfter; @@ -175,6 +176,24 @@ class OptionForm return $this; } + /** + * Add an alert to the top of option form. + * + * @param string $msg + * @param string $style + * @return $this + */ + public function addAlert($msg = self::AUTO_DETECT, $style = 'info') + { + if ($msg == self::AUTO_DETECT) { + $msg = trans("options.$this->id.alert"); + } + + $this->alerts[] = ['content' => $msg, 'type' => $style]; + + return $this; + } + /** * Add callback which will be executed before handling options. * @@ -266,7 +285,7 @@ class OptionForm call_user_func($this->hookAfter, $this); } - $this->addMessage(trans('options.option-saved'), 'success'); + $this->addAlert(trans('options.option-saved'), 'success'); } return $this; diff --git a/resources/views/forms/form.twig b/resources/views/forms/form.twig index 6771b397..e9846b9e 100644 --- a/resources/views/forms/form.twig +++ b/resources/views/forms/form.twig @@ -12,6 +12,21 @@ {% endfor %} + {% for alert in alerts %} +
+ {% if alert.type == 'success' %} + + {% elseif alert.type == 'info' %} + + {% elseif alert.type == 'warning' %} + + {% elseif alert.type == 'danger' %} + + {% endif %} + {{ alert.content }} +
+ {% endfor %} + {% if renderWithoutTable %} {% for item in items %} {{ item.render()|raw }} diff --git a/tests/ServicesTest/OptionFormTest.php b/tests/ServicesTest/OptionFormTest.php index 3861b0aa..bd919183 100644 --- a/tests/ServicesTest/OptionFormTest.php +++ b/tests/ServicesTest/OptionFormTest.php @@ -113,6 +113,21 @@ class OptionFormTest extends TestCase $this->assertEquals('greeting', trim($crawler->filter('.callout-warning')->text())); } + public function testAddAlert() + { + $form = new OptionForm('test', 'test'); + $returned = $form->addAlert(); + $form->addAlert('greeting', 'warning'); + $this->assertSame($form, $returned); + + $crawler = new Crawler($form->render()); + $this->assertEquals( + trans('options.test.alert'), + trim($crawler->filter('.alert-info')->text()) + ); + $this->assertEquals('greeting', trim($crawler->filter('.alert-warning')->text())); + } + public function testHookBefore() { $called = false; @@ -240,7 +255,7 @@ class OptionFormTest extends TestCase $crawler = new Crawler($form->render()); $this->assertEquals( trans('options.option-saved'), - trim($crawler->filter('.callout-success')->text()) + trim($crawler->filter('.alert-success')->text()) ); $this->assertEquals('formatted value', option('t')); }