繁体   English   中英

Symfony4 - 表单choice_value中的空值不起作用

[英]Symfony4 - empty value in form choice_value is not working

当我使用choice_value函数以排除select中的某些数据时,如果该字段为空,则将值设置为database但不设置为“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');
}

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

对于这部分,这是因为你的'empty_data' => null

看看doc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM