如何使Symfony2形成可选字段,而不是保存



我的图像字段是可选的。当您上传映像时,它将将文件名保存在数据库中(使用学说使用事件)。

问题是当您编辑已经上传的表单并且不要添加图像时,它将图像字段变为null。

如果没有上传图像,是否有办法检查/删除字段值设置为空?

实体,表单代码如下:

class Product
{
    /**
     * @ORMColumn(type="string", nullable=true)
     *
     * @AssertImage
     */
    private $image;
}

形式

class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('image', FileType::class, [
            'required' => !$options['update'],
        ]);
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            $form = $event->getForm();
            $product = $event->getData();
            if (null == $form->get('image')->getData()) {
                // $form->remove('image');
            }
        });
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundleEntityProduct'
        ));
    }
    public function getBlockPrefix()
    {
        return 'appbundle_product';
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired([
            'update',
        ]);
    }
}

//在控制器中

$editForm = $this->createForm(
    'AppBundleFormProductType', 
    $product, 
    [
        'update' => true
    ]
);

您需要事件pre_submit,尝试以下操作:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('image', FileType::class, [
        'required' => !$options['update'],
    ]);
    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
        $form = $event->getForm();
        $product = $event->getData();
        if(empty($product['image'])){
            //No new image for existing field was given. Make field disabled so doctrine will not update with empty
            $config = $event->getForm()->get('image')->getConfig();
            $options = $config->getOptions();
            $event->getForm()->add(
                'image',
                $config->getType()->getName(),
                array_replace(
                    $options,
                    ['disabled' => true]
                )
            );
        }
    });
}

symfony表单具有submit()方法,其中有两个参数,$ suptiteddata和$ clearmissing

/**
 * Submits data to the form, transforms and validates it.
 *
 * @param null|string|array $submittedData The submitted data
 * @param bool              $clearMissing  Whether to set fields to NULL
 *                                         when they are missing in the
 *                                         submitted data.
 *
 * @return FormInterface The form instance
 *
 * @throws ExceptionAlreadySubmittedException If the form has already been submitted.
 */
public function submit($submittedData, $clearMissing = true);

$ clearmissing参数由默认设置为true。

如果您在控制器中执行此操作:$form->submit($submittedData, false);,您的图像不会设置为null。

https://symfony.com/doc/current/form/direct_submit.html#calling-form-submit-manalyally

类似的问题:symfony2-如何停止从邮政数据中不存在的无效字段

中停止form->

相关内容

  • 没有找到相关文章

最新更新