Symfony2 remove subform label - forms

I have created a subform like on Symfony2 documentation page:
http://symfony.com/doc/2.0/book/forms.html#form-rendering-template
This code:
public function buildForm(FormBuilder $builder, array $options)
{
// ...
$builder->add('category', new CategoryType());
}
It`s simply assign subform Category in form Task. My problem is to see subform name. I want to remove it but I do not now how to do it. I try this, but it doesn't work:
public function buildForm(FormBuilder $builder, array $options)
{
// ...
$builder->add('category', new CategoryType(), array('label' => '');
}
Do you have any idea how can I remove this SubForm label?

I have assigned false to the label, and it works. But I am not sure is this good solution. If you have any better idea share it please/
public function buildForm(FormBuilder $builder, array $options)
{
// ...
$builder->add('category', new CategoryType(), array('label' => false);
}

Related

Symfony3: How to translate FormErrors added in eventListeners?

Let say I have this form:
//...
class BananaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('flavor');
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
$form = $event->getForm();
if ($form->get('flavor')->getData() === null) {
$form->addError(new FormError('form.error.flavor_missing'));
}
}
}
}
Even though the message form.error.flavor_missing is defined both in messages.yml and validators.yml, it's not displayed.
Any idea how to translate it? I hope I won't have to inject the translator service in every form using this kind of code in order to solve this.
why not use Validation Constraints
use Symfony\Component\Validator\Constraints as Assert;
// ...
class BananaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('flavor', YourType::class, array(
'constraints' => array(
new Assert\NotNull(), // or any other
),
));
}
}
You can use a custom message
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('flavor', YourType::class, array(
'constraints' => array(
new Assert\NotNull(array(
'message' => 'form.error.flavor_missing'
)),
),
));
}
If you open your dev environment, you should see the missing string. Taking a look at them, you should see the domain and the expected messages file.

Access form data dynamically in symfony 4

I have created following simple form in symfony 4.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('email', EmailType::class)
->add('message',TextareaType::class)
;
}
I access these data from a controller method like following:
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
$formdata = $form->getData();
$concatenatedData = implode (",", array("Name: ".$formdata['name'], "E-mail: ".$formdata['email'], "Message: ".$formdata['message']));
Now i need to add more fields in the form. In that case, I need to loop through the $formdata and get those values in $concatenatedData. I am new in symfony. Can anyone give me any hints how to do that dynamically in symfony 4 ?
Many thanks in advance.
create a class that you need after defining in form class
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('price');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Content::class,
));
}
}
Follow
$content = new Content();
$form = $this->createForm(ContactType::class, $content);
$form->handleRequest($request);
$formdata = $form->getData();
$content->getName();
$content->getOther();
...

symfony,when the form does not have this field

class FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' =>'entity'
));
}
}
html form field:
<form>
<input name="a" value="a">
<input name="b" value="b">
</form
When I submit the form, the c,d is set to null.
When the form does not have this field, I do not want to update c and d :)
insted of this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
use this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c', TextType::class , array('mapped' => false))
->add('d', TextType::class , array('mapped' => false));
}
Update your buildForm function like this. In The Symfony FormType, by default all fields are required.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c',TextType::class, ['required' => false,'empty_data' => null])
->add('d',TextType::class, ['required' => false,'empty_data' => null] );
}
If you want to persist the entity in database c and d must be nullable as well

cannot get $this->generateUrl() work from inside AbstractType class

I cannot get $this->generateUrl() work, but it's work from my controller, or should I define 'setAction' using another way ?
class UserLoginType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->setAction($this->generateUrl('fos_user_security_check'))
->add('username', 'text')
->add('password', 'password')
->add('save', 'submit');
}
public function getName()
{
return 'user_login';
}
}
If you are building your form in a separate class and not in your controller, you should pass the action to the form type like this:
// In your controller
$form = $this->createForm(new FormType(), $object, array(
'action' => $this->generateUrl('target_route'),
'method' => 'POST',
));
The AbstractType does not contain any method generateUrl(), that's why you can't set the action in the type directly. You can find details here: http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form
Yes, in the Form class, function buildForm , put this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->setAction( $options["data"]["action"])
->add('name', TextType::class, array('label' => 'Nombre'))
;
}

How to access the entity inside the buildForm method of a form

I'm trying to get the entity inside the own form. I lazy remember a getData() method, but it doesn't exist inside the form, and I can't remember how to use it.
Is there any way to get the entity inside the buildform method not using an event listener?
I mean, something like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
/*some method to get the entity of the form such as getEntity????????*/
$builder->add('field');
}
I found it:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$entity = $builder->getData();
$builder->add('field');
}