基于getter而不是property来验证symfony集合

时间:2015-02-09 12:10:49

标签: php validation symfony properties

我有2个关于验证的问题。我在我的实体中使用了很多属性方法(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)

SpecimenValues的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'并使用twig来分割集合,但我更喜欢使用方法作为属性访问器。

谢谢!

1 个答案:

答案 0 :(得分:0)

我只是通过创建getter和properties属性来解决这个问题。属性本身在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;
}