I have the current situation
<?php
namespace MyBundle\Form\Type;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
class MyFormType extends AbstractType implements ContainerAwareInterface
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$translator = $this->container->get('translator');
$builder->add('my-field', 'text', [
'constraints' => [
new Assert\NotBlank([
'message' => $translator->trans('%field% should not be blank.', ['%field%' => $translator->trans('MyFieldName')]),
]),
],
]);
}
public function getName()
{
return 'my_form';
}
}
This example already works, I am trying to refactor it so I don't have to include the container (or the translator) in it.
The challenge lies in keeping
- '%field% should not be blank.' and
- 'MyFieldName'
as the only two translatable strings, 'cause it's likely that MyFieldName is going to be translated already (like for labels) leaving '%field% should not be blank.' as a generic message valid for any field in the site.
Past into constraints 'attr' => array( 'placeholder' => 'Message', )