How to enclose the validation message in a tag

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.

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. Thanks!

1 Like

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!

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:

Also have a look at the documentation of the module manager: Best Practices when Creating Modules - laminas-modulemanager - Laminas Docs

1 Like

Thnak you for your advice!

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