# How to enclose the validation message in a tag

**URL:** <https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285>\
**Category:** Components & MVC\
**Created:** [May 2, 2021, 2:36pm UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285 "2021-05-02T14:36:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smj320](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/smj320/32/914_2.png) [@smj320](https://discourse.laminas.dev/u/smj320)\
**Post date:** [May 2, 2021, 2:36pm UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285/1 "2021-05-02T14:36:20Z")

</div>

Hello.

Normally, the validation message seems to be

`<ul><li>message</li></ul>.`

I would like to change this to

`<div class="alert-danger"><ul><li>message</li></ul></div>.`

Is there any way to enclose a validation message in a

`<div class="foo">`

tag?

I found a way;

`<div class="alert-danger">`  
`<?= $this->formRow($form->get('email')) ?>`  
`</div>`

but it not smart, I think.

---

<div class="post-metadata">

**Author:** ![ALTAMASH80](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/altamash80/32/895_2.png) [@ALTAMASH80](https://discourse.laminas.dev/u/ALTAMASH80)\
**Post date:** [May 4, 2021, 3:22pm UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285/3 "2021-05-04T15:22:09Z")

</div>

Hi, as long as it fits your design you’re good. But is there a better way? Laminas Docs have shown one way of doing it [here](https://docs.laminas.dev/laminas-form/helper/form-element-errors/). Thanks!

---

<div class="post-metadata">

**Author:** ![smj320](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/smj320/32/914_2.png) [@smj320](https://discourse.laminas.dev/u/smj320)\
**Post date:** [May 6, 2021, 3:12pm UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285/4 "2021-05-06T15:12:21Z")

</div>

I took your advice and found the following method.

In Module.php

```
public function onBootstrap(MvcEvent $e)
{
...
	//Get View class
	$app = $e->getApplication();
	$view = $app->getServiceManager()->get(PhpRenderer::class);

	//set 'class' to <ul> in FormElementErrors
	$pg = $view->plugin("form_element_errors");
	$pg->setAttributes(['class'=>'alert-danger']);
}

```

so, I can enclose messages` <ul class="alert-danger">,` and it is what I want to do.

Thank you for your advice!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [May 6, 2021, 5:49pm UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285/5 "2021-05-06T17:49:07Z")

</div>

> [@smj320](#):
>
> …found the following method.

Do not use the `onBootstrap` method for this because the method is always executed. This means that the view helper FormElementErrors is always called, even if it is not needed!

Create a factory for the view helper and make all settings there. An example of how to use the configuration of your application can be found in laminas-view:

> <https://github.com/laminas/laminas-view/blob/c34b15f42cac410f35010163de169ab71c39bf73/src/Helper/Service/FlashMessengerFactory.php#L26-L53>

Also have a look at the documentation of the module manager: [Best Practices when Creating Modules - laminas-modulemanager - Laminas Docs](https://docs.laminas.dev/laminas-modulemanager/best-practices/#keep-the-init-and-onbootstrap-methods-lightweight)

---

<div class="post-metadata">

**Author:** ![smj320](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/smj320/32/914_2.png) [@smj320](https://discourse.laminas.dev/u/smj320)\
**Post date:** [May 7, 2021, 6:09am UTC](https://discourse.laminas.dev/t/how-to-enclose-the-validation-message-in-a-tag/2285/6 "2021-05-07T06:09:22Z")

</div>

Thnak you for your advice!

This advice is very helpful because I was confused about the order in which to perform the initialization.
