K
K
Konstantin Zhikhor2019-07-05 11:53:05
symfony
Konstantin Zhikhor, 2019-07-05 11:53:05

How to create virtual fields correctly?

Tell me please. I do not get you.
I created a virtual field

namespace App\Form;

use App\Entity\AdditionalCompany;
use App\Entity\AdditionalProduct;
use App\Repository\AdditionalProductRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('products', EntityType::class,[
                'class'=>AdditionalProduct::class,
                'query_builder'=>function(AdditionalProductRepository $additionalProductRepository)
                {
                    return $additionalProductRepository->findAllAdditionalProductsByIds([0]);
                },
                'choice_label'=>function(AdditionalProduct $additionalProduct){
                    $endStrArr = [60=>'(0,2 % в мес)', 56=>' (0,416% в мес)', 1=>'(0,8% в мес)', 7=>'(1% в мес)', 63=>'(500Р)'];
                    $endStr = $endStrArr[$additionalProduct->getId()]?? '';
                    return 'Активировать '.$additionalProduct->getName().' '.$endStr;
                },
                'choice_attr' => function() {
                    return ['class' => 'checkbox_additional'];
                },
                'multiple' => true,
                'expanded' => true,
                'mapped' => false,
                'label'=>false,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'inherit_data' => true,
        ]);
    }
}

Connected it to the main CompanyType fields and after that everything works. products are added. but i still have two fields
->add('additionalCompanies', ProductsType::class)
namespace App\Form;

use App\Entity\AdditionalCompany;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AdditionalCompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('is_default')
            ->add('is_forced')
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'inherit_data' => true,
        ]);
    }
}

and I also need to connect it, but it refers to the company entity, and I need to the AdditionalCompany entity

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antony Tkachenko, 2019-07-05
@LemonFox

$resolver->setDefaults([
            'data_class' => AdditionalCompany::class,
        ]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question