Symfony2: The option "widget" does not exist - php

I'm trying to build a form using symfony2, but I keep getting the error message 'The option "widget" does not exist ' whenever I add the widget option to specify a form field type.
I'm following the example given on the documentation there http://symfony.com/doc/current/book/forms.html
here is my code that does not work.
class UserType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('Name')
->add('Login')
->add('Password')//, 'text', array('widget' => 'password'))
->add('ConfirmPassword')//, 'text', array('widget' => 'password', 'label' =>'Confirm Password'))
->add('Email', 'text', array('widget' => 'email'))
->add('ConfirmEmail')//,'text', array('widget' => 'email', 'label' =>'Confirm Email'))
//...
}
Anyone knows why ?
Thanks

I believe, the correct way to do what you want to achieve is the following:
->add('Name', 'text')
->add('Login', 'text')
->add('Password', 'password')
->add('ConfirmPassword', 'password', array('label' =>'Confirm Password'))
->add('Email', 'email')
->add('ConfirmEmail', 'email')
The first argument of add method is field's name (it has to be unique within form). The second is type and it is responsible for the form taken by widget while rendering. The list of built-in types here. The third argument is array of options. Each type has it's own set of possible options. Indeed, some of types have the widget option. For example date type has such option. But password and email types don't have such option.

Related

How to solve Symfony error Variable "expanded" does not exist?

Currently, I develop an app with PHP Symfony Framework. I've got a problem with Form Builder (I think).
I have two entities. Question and Choice.
Question and Choice are OneToMany Relationship Entity. One Question has many Choice.
Another two entities, Video and Category, the relationship is just the same with Question and Choice.
I create scaffolding crud for those entity with php bin/console make:crud.
Then I add the relationship symfony like in this guide from Symfony.
The logic is, I must select the Category first to create new Video. Same with the Choice, I must select the Question first to create new Choice data.
My problem appear when I open the Choice Create Form [/choice/new]. It says
Variable "expanded" does not exist.
Then the error details show on this lines
return $this->render('choice/new.html.twig', [
'choice' => $choice,
'form' => $form->createView(), // The highlighted error appear on this line
]);
But, It just happen in the Question-Choice, My Category-Video relationship is just fine. I tried to make Question-Choice as same as Category-Video (I changed the name of the entity for sure), I triple check it, but the error on Choice Create Form still occur.
This is my App\Form\ChoiceType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content')
->add('letter')
->add('image')
->add('question', EntityType::class, [
'class' => Question::class,
'choice_label' => 'content'
])
;
}
Notice the add('question')
and this is my App\Form\VideoType buildForm method
<?php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('url', FileType::class, [
'label' => 'Video File',
'required' => false,
])
->add('thumbnail', FileType::class, [
'required' => false,
])
->add('description')
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name'
])
;
}
Notice the add('category')
So, anyone know what is happening?
I renamed App\Form\ChoiceType to App\Form\TheChoiceType, make some adjusment for class name changing on the controller. Everything is work!
I don't believe this! The solution is to rename the form type.

How to use constraints in Symfony Forms making a field required?

Running Symfony 4 with Symfony Forms, I have defined a text field in a form builder:
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// .... other fields
$builder->add('referralCode', TextType::class, [
'required' => true,
'label' => 'Referral Code',
'constraints' => [new NotBlank()],
'attr' => [
'placeholder' => 'Enter a six figures Referral Code (e.g. "6EQE7M")'
]
]);
}
According to docs and tutorials, the NotBlank-constraint should be used here. However, it does not work. If I submit the form without any data typed into this text field, no error is shown. Instead a null value will be send into the property of the entity.
What else needs to be done here?

Symfony3.4 - Pre filled field on POST_SUBMIT Event

I have a form with a contact list. I want the field "first name" appear with the selected contact value after submit. My problem is that the field appear but I cant set the good data, the field always remains empty.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('contacts', ChoiceType::class, [
'label' => 'Contact',
'placeholder' => 'Choose a contact',
'choices' => $this->getContacts(),
'mapped' => false,
])
->setMethod('POST')
;
$builder->get('contacts')->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$contactId = $event->getData();
$parentForm = $event->getForm()->getParent();
$contactEntity = $exampleEm->getrepository(Contact::class)->find($contactId);
$firstName = $contactEntity->getFirstName();
// where can I set the 'contactFirstname' data ?
$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
]);
})
;
}
How to enter the right data so that the field appears pre-filled?
Edit :
I found a method, but it's not terrible:
$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
'empty_data' => $firstName,
]);
('data' => $firstNamedont work for me.)
$parentForm->get('contactFirstname')->setData($firstName); doesn't work either
Can't you simply set the 'data' option of your TextType field?
// ...
$contactEntity = $exampleEm->getrepository(Contact::class)->find($contactId);
$firstName = $contactEntity->getFirstName();
$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
'data' => $firstname //here?
]);
EDIT:
According to this post submitted on github, the form field needs to be submitted in order to have it's data changed.
In one of his solutions, he uses the "empty_data" as you did.
In the other one, he adds the field to the builder. Hides it with display: "none"; until the data is submitted.
The docs say
the data of an unmapped field can also be modified directly:
$form->get('agreeTerms')->setData(true);
So try this:
$parentForm
->add('contactFirstname', TextType::class, [
'label' => 'First name',
]);
$parentForm->get('contactFirstname')->setData($firstName);
Maybe using a setter before creating your form ?
https://symfony.com/doc/current/forms.html#building-the-form

Symfony validate email without assert

I have made a simple Symfony form with only an email field. This form is used to send a document to a user. The form is not linked to any entity. Therefore the formtype looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array(
'mapped' => false
))
->add('save', 'submit');
}
So the email field is not mapped and this works fine.
Now I want to validate the email address like I do with other forms where I have the following asserts:
#Assert\Email(message = "email.not_valid", checkMX = true)
As this formtype has no entity I'm wondering how I can add the checkMX to the email field?
https://symfony.com/doc/current/form/without_class.html#form-option-constraints
use Symfony\Component\Validator\Constraints\Email;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', 'email', array(
'mapped' => false,
'constraints' => array(
new Email(array('checkMX' => true)),
),
))
->add('save', 'submit');
}
EDIT checkMx -> checkMX

How to change input types in existing form using Symfony2?

I've got form class where I'm defining some inputs, something liek this:
class User extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('mail', 'text', array('label' => 'Mail'))
->add('password', 'text', array('label' => 'Hasło'))
->add('description', 'textarea', array('label' => 'Opis'));
}
}
I want to change mail and password input type to readonly and set them some values.
Now, I use form this way:
$form = $this->createForm(new User($this->get('database_connection')));
I tried many things, but Symfony2 has so many Form classes and I've lost in that.
I want to simply add some atributes to existing, added inputs.
I don't use Doctrine2 ORM, I use Doctrine DBAL, if it does matter.
Thanks in advance.
your can set default value with 'data' parameter and readonly with attr parameter
$builder
->add('mail', 'text', array('label' => 'Mail', 'data' => 'Default value'
attr => array('readonly=>'readonly')));

Categories