I'm using the following simple controller function to create a form:
$json = json_decode(file_get_contents('../data/sample.json'));
public function index(Request $request): Response
{
$form = $this->createFormBuilder();
$form->add('city', ChoiceType::class, [
'choices' => $json[0]->options,
'label' => $json[0]->label,
]);
$form->add('submit', SubmitType::class, [
'attr' => [
'class' => 'btn btn-primary'
],
]);
$form->add('next', SubmitType::class, [
'attr' => [
'class' => 'btn btn-warning',
'disabled' => true
]
]);
$form = $form->getForm();
.
.
.
return $this->render('quiz/index.html.twig', [
'form' => $form->createView(),
]);
}
The data is read from the following same json file :
[
{
"options" : ["Lisbon", "London", "Los Angels"],
"label" : "Please select your <u>city</u> of residence"
}
]
Each of the labels has some HTML tag(s) in them but all of those tags are not being processed. So the example above displays in my browser exactly as seen in the code.
In my twig template, I have tried each one of the following but individually:
{{ form(form) }}
{{ form(form) | raw }}
{{ form(form) | convert_encoding('UTF-8', 'iso-2022-jp') }}
{{ form(form) | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% autoescape false %}
{{ form(form) | convert_encoding('UTF-8', 'HTML-ENTITIES') }}
{% endautoescape %}
{{ form(form) | striptags('<u><input><p><select><option><br><b><button>') }}
{{ form(form) | striptags('<u><input><p><select><option><br><b><button>') | raw }}
Despite all those suggestions I found online, my form labels still display unprocessed HTML tags.
An potential added complication is that I print the entire form as one whole using {{ form(form) }}
. However because the form is dynamically assemble from JSON data, it's not really possible to print the form in segments because the JSON data can be added to or removed from at any time.