Tricks

Render HTML (link,...) in a field label()

Sep 7, 2022
Martin Mildner
Form builder

Return an HtmlString object in order to add HTML to a field label.

Example:

use Illuminate\Support\HtmlString;
 
Checkbox::make('accept')
->label(fn () => new HtmlString('I accept the <a href="" target="_blank">terms and conditions</a>'))
->required(),
avatar

Nice!

avatar

If your server-side validation fails for some reason, you may see HTML in the validation error message:

The i accept the <a href="" target="_blank">terms and conditions</a> field is required.

Use validationAttribute() to set the label of the field for validation purposes:

Checkbox::make('accept')
->label(fn () => new HtmlString('I accept the <a href="" target="_blank">terms and conditions</a>'))
->validationAttribute("Terms and conditions")
->required(),