是否有形式以形式SYMFONY 2.5检查价值

时间:2017-06-01 09:35:38

标签: php symfony

我有一个嵌入式表单(oneTo Many关系)。

1 - 实体请求

2 - 请求的实体产品

我正在寻找适应形式。

我希望表单可以在检查此对象的价格时动态修改该值。

最好是检查表单中的值并进行比较,但我不知道哪种方法可以解决我的问题。

我想在表格" ProductRequestType"中进行检查。

我的表单ProductRequestType的例子:

<?php

namespace \testBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager;

class testRequestProductType  extends AbstractType {
    /** @var \Doctrine\ORM\EntityManager */
    private $em;

    /**
    * Constructor
    *
    * @param Doctrine $doctrine
    */
    public function __construct($em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $disabled_exchange = $options['attr']['disabled_exchange'];
        $customer_id = $options['attr']['customer_id'];

        $builder->add('id', 'hidden');
        $builder->add('part_number');
        $builder->add('_number');
        $builder->add('source_quantity');
        $builder->add('quantity');
        $builder->add('serial');
        $builder->add('buy_date', 'date', array('required' => false,'widget' => 'single_text', 'fotestt' =>'dd/MM/yy'));
        $builder->add('return_date', 'date', array('required' => false,'widget' => 'single_text', 'fotestt' =>'dd/MM/yy'));
        $builder->add('is_new', null, array('required' => false));
        $builder->add('test_comment');
        $builder->add('available_stock', 'integer', array('mapped' => false));
        $builder->add('buying_price', 'integer', array('mapped' => false));
        $builder->add('test_reason', 'entity',
            array(
                'class' => 'testBundle:testReason',
                'property' => 'name',
                'expanded' => false,
                'multiple' => false
            )
        );

        //I would like to get the price and if the price < 100 the variable $disabled_exchange = true

        if($disabled_exchange){
            $builder->add('test_action', 'entity',
                array(
                    'class' => 'testBundle:testAction',
                    'property' => 'label',
                    'expanded' => false,
                    'multiple' => false,
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('u')
                            ->where('u.name != :action')
                            ->setParameter('action', 'exchange');
                    }
                )
            );
        }else{
            $builder->add('test_action', 'entity',
                array(
                    'class' => 'testBundle:testAction',
                    'property' => 'label',
                    'expanded' => false,
                    'multiple' => false
                )
            );
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '\testBundle\Entity\testRequestProduct',
            'disabled_exchange' => false
        ));
    }

    public function getName()
    {
        return 'test_request_product';
    }

}

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

您可以使用表单事件侦听器

来完成此操作

symfony event doc