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 :)
Related
I've been looking for a solution for hours. I'am working with Symfony 3.2.
I'am using Symfony Forms in order to display a data-table (with different filter chosen in the form) with Ajax => No submit button.
I can access to different result of data-table to view more information.
What i want is that when i leave the detail page by clicking on a button and come back on the research page, that i could keep in history all filters that i have chosen.
i wanted use session but actually, it seems has 0 impact. Below is some codes.
Controller:
public function indexAction(Request $request)
{
$form = $this->createSearchForm();
$request->getSession()->set('form_data', $form->getData());
$form->handleRequest($request);
$form->setData($request->getSession()->get('form_data'));
$this->datatable($form->getData());
return $this->render('backend/jobOffer/index.html.twig', [
'form' => $form->createView()
]);
}
FormType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('publicationState', ChoiceType::class, [
'label' => 'backend.job_offer.publication_state',
'required' => false,
'choices' => JobOffer::getPublicationStateChoices(),
'choice_translation_domain' => 'choices',
])
->add('job', Select2EntityType::class, [
'label' => 'backend.job',
'required' => false,
'class' => 'AppBundle:Job',
'text_property' => 'label',
'remote_route' => 'job_autocomplete',
])
->add('ids', HiddenType::class, [
'required' => false,
])
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
'method' => 'GET',
]);
}
I have a problem, there is a EntityType (or Select2EntityType) field named "job" in my form builder and i can't get the content of this field in my session. This field shows a autocomplete data-list after typing 2 letters, and we can choose one of job.
and also, when i refresh the page, i lose all filters, but i am supposed to store them in session ?
Thanks in advance for your help,
Yes you can use $form->getData() but to do that you need to do this according to the doc here with using if ($form->isSubmitted() && $form->isValid()) { among others.
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.
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'm trying to build multi-tenancy support in Symfony, doing this I created a group_id column in my User, on top of that, all entities also have this same column. That way users only have access to their group's data.
I've managed to cut through this whole thing like butter with data access and display, but then came the challenge of EntityTypes for the Symfony Forms.
The question is basically, how do I make the EntityType display only the data that that particular group has inputted. Sorting it by the group_id that both the user and contact has. Whats the best way to maybe pass this in so the user only has access to their data?
<?php
namespace ContactBundle\Form;
use ContactBundle\Entity\Contact;
use ContactBundle\Entity\Organization;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('first_name', TextType::Class, [
'attr' => ['class'=>'u-full-width'],
'label' => 'First Name',])
->add('last_name', TextType::Class, [
'attr' => ['class'=>'u-full-width'],
'label'=>'Last Name',
])
->add('email', TextType::Class, [
'attr' => ['class'=>'u-full-width'],
'label'=>'Email Address'
])
->add('organization_id', EntityType::Class, [
'attr' => ['class'=>'u-full-width'],
'required' => false,
'class'=>'ContactBundle:Organization',
'choice_label'=>'name',
'choice_value'=>'id',
'label'=>'Organization'
])
->add('phone', TextType::Class, [
'attr' => ['class'=>'u-full-width'],
'label'=>'Phone',
])
->add('role', TextType::Class, [
'attr' => ['class'=>'u-full-width'],
'label'=>'Role',
])
->add('submit', SubmitType::class, [
'label'=>'Submit',
'attr' => [
'class'=>'button-primary',
'style'=>'margin-top:30px;',]]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class'=>Contact::class,]);
}
}
?>
It is worth noting that I am using FOSUserBundle.
In Symfony it's very easy to inject whatever you need where you need it.
// ...
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ContactType extends AbstractType
{
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('organization_id', EntityType::Class, [
'attr' => ['class'=>'u-full-width'],
'required' => false,
'class'=>'ContactBundle:Organization',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.group', '?0')
->setParameters([$this->user->getGroup()]);
},
'choice_label'=>'name',
'choice_value'=>'id',
'label'=>'Organization'
])
// ...
If you are not using autowire and autoconfigure (Symfony 3.3+), register your form as a service manually and tag it with form.type:
# config/services.yaml
services:
AppBundle\Form\ContactType:
arguments: ['#security.token_storage']
tags: [form.type]
Related reads
https://symfony.com/doc/current/form/form_dependencies.html#define-your-form-as-a-service
https://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities
In controller when creating form
$form = $this->createForm(ContactType::class, $contact, ['group_id' => $groupId]);
In form configureOptions method
$resolver->setDefaults(['data_class'=>Contact::class, 'group_id' => null]);
In form buildForm you can get group_id by
$options['group_id']
I got it!
So what I ended up doing was getting rid of the whole idea of using a EntityType on the form side.
I then called the data from the controller and passed it in as an option.
Like so:
$contactManager = $this->get('contact.contact_manager');
$contact = new Contact($contactManager->nextId($group = $this->getUser()->getGroupId()), $group);
$form = $this->createForm(ContactType::class, $contact, ['organizations' => $this->get('contact.organization_manager')->findDataCollection($this->getUser()->getGroupId())]);
$form->handleRequest($request);
Turned my EntityType into a choice type and passed the array of organizations into the 'choices' field as an option. Kept everything else the same like so:
->add('organization_id', ChoiceType::Class, [
'attr' => ['class'=>'u-full-width'],
'required' => false,
'choices'=> $options['organizations'],
'choice_label'=>'name',
'choice_value'=>'id',
'label'=>'Organization'
])
Then, of course, set it within the options so it can expect a variable.
$resolver->setDefaults(['data_class'=>Contact::class, 'organizations' => null]);
I appreciate all of the help and ideas!! :)
You could do this in few different ways, one was to use combination of JavaScript and EntityType.
Basically just load all the entities and hide unwanted entities with JS depending on the previous list's selection. You can use data attribute to specify things needed to make that work.
I have a form which has a form (CustomerVatsType) for an entity (CustomerVats). This entity has a column (vats), which contains multiple vat rows. These rows are saved in json format. On this form, customer can choose a default vat, which will be saved in "default" index of rows saved in units column.
but problem is that radio input name is "form[vats][0][set_default]" due to structure of form. But for radio input to work correctly it needs to be same for all inputs (e.g. form[vats][set_default]). I can change name in twig file but then form class can not understand this.
What can be done for this situation. Does even Symfony support it. Here is my form class.
class VatsType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('vats', CollectionType::class, array(
'entry_type' => VatType::class,
'allow_add' => false,
'allow_delete' => false,
'prototype' => false,
'by_reference' => false,
)
)
->add('vatSumbit', SubmitType::class);
}
}
class VatType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('country_id', HiddenType::class, array('label' => false))
->add('vat_high', HiddenType::class, array('label' => false))
->add('vat_low', HiddenType::class, array('label' => false))
->add('vat_zero', HiddenType::class, array('label' => false))
->add('vat_none', HiddenType::class, array('label' => false))
->add('set_default', RadioType::class, array('label' => false))
->add('set_show', RadioType::class, array('label' => false));
}
}
I ended up using following fix for this problem. It seems bit dirty but made most sense to because I dont want to confuse other devs with some really difficult method.
<script type="text/javascript">
(function (document, window, $) {
$('[data-radio-field]').change(function () {
var field = $(this).data('radio-field');
$('[data-radio-field="' + field + '"]').not($(this)).prop('checked', false);
});
})(document, window, jQuery);
</script>
If anyone can suggest a clean method, they are welcome to answer it. I think it should be a common problem. Would like to see other approaches well.