Symfony4-形式choice_value中的空值不起作用

时间:2019-05-07 17:13:35

标签: php forms entity-framework symfony4

当我使用choice_value函数以排除我的选择中的某些数据时,如果该字段为空,则将值设置为数据库,但不设置为“ NULL”。此值是我实体的第一个条目。当我列出所有数据时,如果该字段为空,则会在数据库中设置“ Null”值。

有什么想法吗?

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $this->currentId = $options['currentId'];
     $builder

         ->add('oldOwners', entityType::class, array(
             'class' => OldOwners::class,
             'required' => false,
             'empty_data' => null,
             'multiple' => false,
             'choice_value' => function (OldOwners $oldOwners = null) {
                 if ($oldOwners != null && $oldOwners != '') {
                     if ($this->currentId != 0 && $oldOwners->animal->getId() == $this->currentId)

                         return $oldOwners->getDateDisposal()->format('Y-m-d');
                 }

             },
             'choice_label' => function (OldOwners $oldOwners = null) {

                 if ($oldOwners != null && $oldOwners != '') {
                     if ($this->currentId != 0 && $oldOwners->animal->getId() == $this->currentId)

                         return $oldOwners->getDateDisposal()->format('Y-m-d');
                 }
             },

             'label' => "Date"));

 /**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Adopting::class,
        'currentId' => null,
    ]);
    $resolver->setRequired('currentId');
}

1 个答案:

答案 0 :(得分:0)

  

当我列出所有数据时,如果该字段为空,则会在数据库中设置“ Null”值。

这部分是因为您的'empty_data' => null

看看doc