Answer the question
In order to leave comments, you need to log in
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,
]);
}
}
->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,
]);
}
}
Answer the question
In order to leave comments, you need to log in
$resolver->setDefaults([
'data_class' => AdditionalCompany::class,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question