基于getter而不是属性验证symfony集合


Validating symfony collection based on getters instead of properties

我有两个关于验证的问题。我在实体中大量使用属性方法(getter)(更好的代码imho)。这就是这样一个实体:

    class Spec2Events implements ValueAssignable
    {
        private $values;
       /**
        * @return 'Doctrine'Common'Collections'Collection
        */
        public function getValues()
        {
           return $this->values;
        }
        /**
         * @return 'Doctrine'Common'Collections'Collection
         */
        public function getCauseOfDeathValues()
        {
            $codPms=array();
            array_push($codPms,'Cause of death::Natural');
            array_push($codPms,'Cause of death::Bycatch');
            array_push($codPms,'Cause of death::Ship strike');
            array_push($codPms,'Cause of death::Predation');
            array_push($codPms,'Cause of death::Other');
            array_push($codPms,'Cause of death::Unknown');
            return $this->getValues()->filter(
                function($entry) use ($codPms) {
                    return in_array($entry->getPmdSeqno()->getName(), $codPms);
                }
            );
        }
   }

$values在本例中是SpecimenValues(实现EntityValues)的集合。ValueAssignables有一个EntityValues集合。

EntityValuesType类是实现EntityValues的任何类的形式。此表单包含一些文本或选择子项。

EntityValuesType表单的调用方式如下:

$builder->add('causeOfDeathValues', 'collection', array('type' => new EntityValuesType($this->doctrine),
    'options' => array('data_class' => 'AppBundle'Entity'SpecimenValues'),
    'allow_delete' => true,
    'delete_empty' => true
)); //in order to check if using a class getter as a property works (fails)
$builder->add('values', 'collection', array('type' => new EntityValuesType($this->doctrine),
    'options' => array('data_class' => 'AppBundle'Entity'SpecimenValues'),
    'allow_delete' => true,
    'delete_empty' => true
)); //in order to check if using a class member as a property works (works)

SpecimenValue的Validation.yml如下所示:

AppBundle'Entity'SpecimenValues:
    properties:
        pmdSeqno:
               - NotBlank: ~
               - NotNull: ~
        s2eScnSeqno:
               - NotBlank: ~
               - NotNull: ~
        description:
                - Length:
                     min: 0
                     max: 250
        value:
                - NotBlank: ~
                - NotNull: ~
                - Length:
                     min: 1
                     max: 50
        valueFlag:
                - Length:
                     min: 0
                     max: 50

控制器看起来像这样:

    public function newAction()
    {
        $observation = $this->prepareObservation();
        $form = $this->createForm(new ObservationsType($this->getDoctrine()), $observation);
        return $this->render('AppBundle:Page:add-observations-specimens.html.twig', array(
            'form' => $form->createView()
        ));
    }
    private function prepareObservation(){
        $observation = new Observations();
        $event = new EventStates();
        $observation->setEseSeqno($event);
        $s2e = new Spec2Events();
        $event->setSpec2Events($s2e);
        $this->instantiateSpecimenValues('Cause of death::Natural', $s2e, false);
        $this->instantiateSpecimenValues('Cause of death::Bycatch', $s2e, false);
        $this->instantiateSpecimenValues('Cause of death::Ship strike', $s2e, false);
        $this->instantiateSpecimenValues('Cause of death::Predation', $s2e, false);
        $this->instantiateSpecimenValues('Cause of death::Other', $s2e, false);
        $this->instantiateSpecimenValues('Cause of death::Unknown', $s2e, false);
//...
        return $observation;
    }
    private function instantiateSpecimenValues($pmName, &$s2e, $mustBeFlagged)
    {
        $em = $this->getDoctrine()->getManager();
        $pm = $em->getRepository("AppBundle:ParameterMethods")->getParameterMethodByName($pmName);
        $sv = new SpecimenValues();
        $sv->setPmdSeqno($pm);
        $sv->setS2eScnSeqno($s2e);
        $sv->setValueFlagRequired($mustBeFlagged);
        return $sv;
    }

现在,我的问题是验证器没有阻止空值(没有显示表单错误消息)。

如果我在FormEvents:PRE_SET_DATA中以编程方式添加验证约束,如下所示:

$options2['constraints'] = array(new 'Symfony'Component'Validator'Constraints'NotNull());

它是有效的,但是放置在.yml文件中的约束被忽略了。有可能将"以编程方式"执行AND与validation.yml结合起来吗?在任何情况下,我都会写一个回调来添加到.yml中,所以我更喜欢validation.yml.

使用名为"values"的表单子级(对应于纯类成员变量)可以正常工作:所有必需的空字段都会得到一条消息。所有其他验证工作正常。

什么可以解决这个问题?我也可以使用"values"和使用trick来拆分集合,但我更喜欢使用方法作为属性访问器。

谢谢!

我只需将字段创建为getter和属性就可以解决这个问题。属性本身是在setter中设置的。这是必要的,否则永远不会调用验证器。

因此:

/**
 * @var 'Doctrine'Common'Collections'Collection
 * @ORM'OneToMany(targetEntity="AppBundle'Entity'SpecimenValues", mappedBy="s2eScnSeqno")
 */
private $values;
private $causeOfDeathValues;
/**
 * @param 'Doctrine'Common'Collections'Collection $values
 * @return Spec2Events
 */
public function setCauseOfDeathValues('Doctrine'Common'Collections'Collection $values)
{
    $this->causeOfDeathValues=$values;
    $this->values= new 'Doctrine'Common'Collections'ArrayCollection(
        array_merge($this->getValues()->toArray(), $values->toArray())
    );
    return $this;
}