Home > Mobile >  Symfony and custom form type and input name
Symfony and custom form type and input name

Time:05-25

I have a form with custom type.

My custom type is like this :

<?php

namespace CpayCore\AppBundle\Form;

use ActorChecker\ActorChecker;
use CpayEntity\EntityBundle\Model\PaymentMethodEnum;
use CpayEntity\EntityBundle\Repository\ActorWalletRepository;
use CpayTools\ElasticsearchBundle\Form\Extension\ElasticConditionTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

class PaymentMethodType extends AppAbstractType
{


    public function buildForm(FormBuilderInterface $builder, array $options): void
    {

        $builder->add('test', ChoiceType::class, [
            self::CHOICES => PaymentMethodEnum::getAllTypes(),
            self::CHOICE_LABEL => function ($value) {
                if ($value === PaymentMethodEnum::SCT_TRANSACTION) {
                    return 'payment_request.payment_method.' . PaymentMethodEnum::WIRE_TRANSFER;
                }

                return 'payment_request.payment_method.' . $value;
            },
            self::PLACEHOLDER => self::NONE_SELECTED,
            self::LABEL => 'payment_request.payment_method',
            self::LABEL_ATTR => [
                self::E_CLASS => self::CONTROL_LABEL,
            ],
            self::TRANSLATION_DOMAIN => self::APPBUNDLE,
            self::MULTIPLE => true,
            self::EXPANDED => true,
            self::ATTR => [
                self::E_CLASS => 'checkbox-col-3',
            ],
        ]);

     

    }

}

In my form type I call my custom type like this :

 $builder->add('payment', PaymentMethodType::class, [           
        ])

How can I define de name of my select in template render, I won't have [payment][test] but only [test] ?

Thanks

PS : sorry for my very bad english

CodePudding user response:

Since you are only modifying the options array of the custom form type (and you only have one child), you would be better to simply extend the existing ChoiceType.

class PaymentMethodType extends AppAbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            self::CHOICES => PaymentMethodEnum::getAllTypes(),
            self::CHOICE_LABEL => function ($value) {
                if ($value === PaymentMethodEnum::SCT_TRANSACTION) {
                    return 'payment_request.payment_method.' . PaymentMethodEnum::WIRE_TRANSFER;
                }

                return 'payment_request.payment_method.' . $value;
            },
            self::PLACEHOLDER => self::NONE_SELECTED,
            self::LABEL => 'payment_request.payment_method',
            self::LABEL_ATTR => [
                self::E_CLASS => self::CONTROL_LABEL,
            ],
            self::TRANSLATION_DOMAIN => self::APPBUNDLE,
            self::MULTIPLE => true,
            self::EXPANDED => true,
            self::ATTR => [
                self::E_CLASS => 'checkbox-col-3',
            ],
        ]);
    }

    public function getParent(): string
    {
        return ChoiceType::class;
    }
}

Then, when you do

 $builder->add('payment', PaymentMethodType::class, [           
        ])

your form will have only myform[payment] (but no test)

Here is the Symfony Documentation on the subject

CodePudding user response:

You can set the "name" empty of your form like this:

class PaymentMethodType extends ... 
{
    ... 

    public function getName(): string
    {
        return '';
    }
}
  • Related