Reputation: 225
Is it possible to add a new form field type in symfony that would simply render a configurable message? I tried to add a new AlertType
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 the SetupFormData
has a property called $warning
, everything is working as desired. But I don't like to add this property to the data class.
Upvotes: 1
Views: 1391
Reputation: 56
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.
Upvotes: 0
Reputation: 51
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
);
Upvotes: 1