php - Custom Form Field Type in Symfony to display plain text (or even html)
Get the solution ↓↓↓Is it possible to add a new form field type in symfony that would simply render a configurable message? I tried to add a newAlertType
which should render a given message as a bootstrap 4 alert component. It actually works, but the problem is, that if I am using this form field type in an actual form, then the data mapper is searching for a property in the assigned data class. But it makes no sense to have a property in the data class, because this form type is not really expecting any user input. Is it possible to disable data mapping for a form type? I guess it is, because the\Symfony\Component\Form\Extension\Core\Type\ButtonType
does what I would like to achieve - but how?
Currently, my custom form field type is as follows:
<?php
// src/Form/Type/AlertType.php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AlertType extends AbstractType
{
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver->setDefaults(
[
'context' => 'danger',
'message' => '',
]
);
}
/**
* @inheritDoc
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['context'] = $options['context'];
$view->vars['message'] = $options['message'];
}
}
And the template is as follows:
{% block alert_row %}
<div class="alert alert-{{ context|escape('html_attr') }}" role="alert">
{{ message }}
</div>
{% endblock %}
And the form type (which uses the form field type) is:
<?php
namespace App\Form\Type;
use App\Setup\SetupFormData;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SetupFormType extends AbstractType
{
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
'setupPassword',
PasswordType::class
)
->add(
'warning',
AlertType::class,
['message' => 'HELLO', 'context' => 'primary']
)
->add(
'save',
SubmitType::class
);
}
/**
* @inheritDoc
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'data_class' => SetupFormData::class,
]
);
}
}
As long as theSetupFormData
has a property called$warning
, everything is working as desired. But I don't like to add this property to the data class.
Answer
Solution:
If you don't want that is mapped just add this'mapped' => false,
$builder
->add(
'setupPassword',
PasswordType::class
)
->add(
'warning',
AlertType::class,
['message' => 'HELLO', 'context' => 'primary'],
'mapped' => false,
)
->add(
'save',
SubmitType::class
);
Answer
Solution:
From Symfony 4+ you have help and help_html options in TextFormField and few extend form types.
Help Html. You can configure the help text as you want with additional html and css.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: foreign key constraint is incorrectly formed laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.