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.
Related
I could not really find my specific case on the internet and therefore decided on writing my own question.
I have a form to create a Member object. This member object has a reference to an application object. Both are saved in 2 different databases.
Creating a Member object is no issue only when I edit and fill out the form, do I encounter an error ->
Entity of type "Application"
passed to the choice field must be managed. Maybe you forget to
persist it in the entity manager
Here is my Form Code
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('membberName', null, array('label' => false))
->add('memberDescription', TextareaType::class, array('label' => false))
->add('memberVisible', null, array('label' => false))
->add('memberApp', EntityType::class, [
'label' => false,
'class' => Application::class,
'choice_label' => function (Application $application) {
return sprintf('(%d) %s', $application->getAppId(), $application->getAppurlUrl());
},
'choices' => $this->applicationRepository->getAll(),
])
->add('Save', SubmitType::class, [
'attr' => ['class' => 'create-button']
]);
}
I found a lot of cases about this issue but none of them could help me.
I only encounter this issue if I load an entity that is related to another one outside their own database.
To summarize: calling the create view page and pressing on submit works.
Calling the edit view causes the above mentioned issue.
Do I have to define or configure anything so my form can load correctly?
Try setting up custom em for your entity form field memberApp in options to your second database entity manager.
like described in docs: https://symfony.com/doc/current/reference/forms/types/entity.html#em
em type: string | Doctrine\Common\Persistence\ObjectManager default:
the default entity manager
If specified, this entity manager will be used to load the choices
instead of the default entity manager.
Another way, probably would be to set ['mapped' => false] for this field and handling flush manually with correct database's em in controller or service
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?
I have an entity Questions and an entity Answers. The answers are referring to a question and have a field Correct (which is just a Boolean true/false). Each question has 4 answers and of those 4 answers, 1 is correct.
Now I'm implementing a backend form where questions can be added or edited. I use Symfony forms to do this. My formtype builder looks like this:
QuestionType.php
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('description', TextareaType::class, [
'label' => 'admin.question.form.description',
'required' => false,
])
->add('question', TextType::class, [
'label' => 'admin.question.form.question',
])
->add('answers', CollectionType::class,[
'label' => 'admin.question.form.answers.answers',
'entry_type' => AnswerType::class,
]);
}
So I'm using a collection for the answers, which will be 4 in total. This collection is creating AnswerType forms.
AnswerType.php
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('answer', TextType::class, [
'label' => 'admin.question.form.answers.answer',
])
->add('correct', RadioType::class,[
'label' => 'admin.question.form.answers.correct',
]);
}
Which is just 2 form elements, an input field for the answer and a radiobutton to indicate whether the answer is true or false.
I want to render those 4 answer fields each with a radiobutton at the end to indicate the correct answer. Unfortunately those radiobuttons are not linked, and each belongs to it's own collection. Each radiobutton is selectable, so every answer can be selected as correct. Which seems logical because there is no way to tell Symfony to group them.
Each has it's own name, and does not belong to each other. How can these checkboxes be linked, or what is a better way to tackle this problem?
Set same CSS class name to radio buttons in same question and imlement check/uncheck logic when user clicks on a radio button via JS.
I'm looking for best (or just working) way to solve following problem.
I have like standard UserType form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'username',
Type\TextType::class
)
->add(
'email',
Type\EmailType::class
)
->add(
'plainPassword',
Security\UserRepeatedPasswordType::class
)
->add(
'roles',
Type\ChoiceType::class,
[
'multiple' => true,
'expanded' => true,
'choices' => $this->getRoleChoices()
]
);
}
What is nonstandard is that UserRepeatedPasswordType, it looks like this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'password',
Type\RepeatedType::class,
[
'type' => Type\PasswordType::class,
'required' => true,
'first_options' => [
'label' => 'Password'
],
'second_options' => [
'label' => 'Repeat Password'
],
]
);
}
And I created it because those two fields are also used in passwordReset form and userSettings form. And now I have two problems:
1.) When I use it this way, value from UserRepeatedPasswordType is not correctly mapped for my User Entity - there is an error that string is expected (duh ;) but it got array. I tried using View and Model transformer but no proper results (but I don't have much experience with those, so that maybe the case). I also tried to experiment with getParent(), and pass there UserType but it goes to some endless loop and I got 500. If I just copy paste field from UserRepeatedPasswordType to UserType it works correctly.
2.) If this is solved (or even by copy paste, if can't be done other way), there is another related (I believe) problem:
I have this ChangePasswordType form, which is used to reset your password.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'confirmationToken',
Type\HiddenType::class,
[
'required' => true,
'constraints' => [
new NotBlank(),
]
]
)
->add(
'plainPassword',
Type\RepeatedType::class,
[
'type' => Type\PasswordType::class,
'required' => true,
'first_options' => [
'label' => 'Password'
],
'second_options' => [
'label' => 'Repeat Password'
],
]
)
->add(
'changePassword',
Type\SubmitType::class
);
}
And it works fine as it is but I want to do two things with it - first, solving my first problem and use UserRepeatedPasswordType in it, second - I have some Assert\Length done in User Entity on $plainPassword and it workes correctly when I submit new user via UserType form. But I want that validation somewhat mapped to ChangePasswordType or ideally to UserRepeatedPasswordType - just to have all rules in one place. Can this even be done? Thanks for any solutions / hints / advices.
Ok, dunno if anyone is interested but that is how I completed this. If anyone have better answer, just give me a sign (mostly to the first one) ;)
1.) As i thought, solved by ViewTransformer but in parent form (In UserType not in UserRepeatedPasswordType
$builder->get('plainPassword')
->addViewTransformer(new CallbackTransformer(
function ($singleAsArray) {
return $singleAsArray;
},
function ($arrayAsSingle) {
return $arrayAsSingle['password'] ?? '';
}
));
2.) That was actually quite easy. All you have to do is to map that form to UserEntity that same way as UserType and made custom validation groups just to have everything nice and under control :)
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')));