add extra html in addGroup Label

4 months ago

matc1
Member | 20
+
0
-

Hi i'm using Nette and makeBootstrap4,

i need to add some extra html text to addGroup label.

Something like

$text2 = “<br><b>BLABLABLA<b></br>”;

$form->addGroup(“LABEL1”.$text2); I notice that html is not evaluate and maybe it is correct,

can i add adding extra div o span in addGroup?.

I'm using Standalone Forms.

Excuse me but i'n not be able to do at now.

Thanks a lot.

Last edited by matc1 (2020-03-31 14:25)

4 months ago

dakur
Member | 44
+
0
-

Try passing Nette\Utils\Html object. It works for control labels and other stuff so it could work for group labels as well.

$form->addGroup(
  "LABEL1" .
  \Nette\Utils\Html::el('br') .
  \Nette\Utils\Html::el('b')->setText('BLABLABLA')
)

or shorter (but possibly less secure)

$form->addGroup(\Nette\Utils\Html::el()->setHtml('LABEL1<br><b>BLABLABLA</b>'))
// Be aware of **not** using this together with user input (e.g. Html::el()->setHtml('<b>' . $input . '</b>')
// as it is possible XSS issue

See Nette\Utils\Html docs for more information.

Also note that <br> is not a pair tag.

Last edited by dakur (2020-03-31 14:53)

4 months ago

matc1
Member | 20
+
0
-

dakur wrote:

Try passing Nette\Utils\Html object. It works for control labels and other stuff so it could work for group labels as well.

$form->addGroup(
  "LABEL1" .
  \Nette\Utils\Html::el('br') .
  \Nette\Utils\Html::el('b')->setText('BLABLABLA')
)

or shorter (but possibly less secure)

$form->addGroup(\Nette\Utils\Html::el()->setHtml('LABEL1<br><b>BLABLABLA</b>'))
// Be aware of **not** using this together with user input (e.g. Html::el()->setHtml('<b>' . $input . '</b>')
// as it is possible XSS issue

See Nette\Utils\Html docs for more information.

Also note that <br> is not a pair tag.

Thanks but i try all two solution and i notice that html is not evaluated like:

“LABEL1<br><b>BLABLABLA</b>”.

I notice that addGroup use <legend> to put label

<legend>LABEL1<br><b>BLABLABLA</b></legend>

Maybe i need to concatenate something after “</legend>”?

Thanks a lot

4 months ago

dakur
Member | 44
+
0
-

That's correct because $form->addGroup() implies that you want to make a container for more controls. This container can have a label, which is what you are setting with the parameter of addGroup(). And this label is rendered using as a <legend> tag in a <fieldset> wrapper. See example on MDN.

Or maybe addGroup() is not what you need. What are you trying to render? Are you just trying to style a word in the label, or make a multi-line label or something else?

Last edited by dakur (2020-03-31 16:46)

4 months ago

matc1
Member | 20
+
0
-

dakur wrote:

That's correct because $form->addGroup() implies that you want to make a container for more controls. This container can have a label, which is what you are setting with the parameter of addGroup(). And this label is rendered using as a <legend> tag in a <fieldset> wrapper. See example on MDN.

Or maybe addGroup() is not what you need. What are you trying to render? Are you just trying to style a word in the label, or make a multi-line label or something else?

Thanks, yes you're right, I'm trying to add an additional label (PIPPO+ html tag) after </legend> something like

<fieldset>
<legend>LABEL1</legend>
** PIPPO<br>…**

Excuse me but i'm using only Nette form standalone with bootstrap4 and i'm a novice.
Thanks a lot.

4 months ago

dakur
Member | 44
+
+1
-

It really depends on your context. If you plan to do this for every single group or your form is not long, than maybe manual rendering would be better for you. I personally render everything manually because I need custom HTML output all the time.

In such case you have to code whole HTML (which should not have to be long) except for labels and inputs – you use {label name /} and {input name} and Nette render inputs and its labels for you.

But if this is the only case you need to add something to the default output, maybe some hack could be done, but I can't really suggest one – I never needed this, sorry.

Last edited by dakur (2020-03-31 17:23)

4 months ago

matc1
Member | 20
+
0
-

dakur wrote:

It really depends on your context. If you plan to do this for every single group or your form is not long, than maybe manual rendering would be better for you. I personally render everything manually because I need custom HTML output all the time.

In such case you have to code whole HTML (which should not have to be long) except for labels and inputs – you use {label name /} and {input name} and Nette render inputs and its labels for you.

But if this is the only case you need to add something to the default output, maybe some hack could be done, but I can't really suggest one – I never needed this, sorry.

Thanks i will evaluate manual rendering.