Symfony 4 - 表单 - “此值无效”从 Angular 传递 Date 时

发布于 2025-01-14 19:43:59 字数 2914 浏览 0 评论 0原文

我正在尝试访问端点来更新 Auction 实体,该实体具有 start_dateend_date 属性,均为 DateTimeType< 类型/代码>。但是,当我从 Angular 前端将 Auction 实体传递到我的 API 时,我被告知 start_dateend_date 值是无效。

这是我的 AuctionType (表单)的当前状态:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('company', TextType::class)
            ->add('minimum_unit_price', NumberType::class)
            ->add('maximum_unit_amount', NumberType::class, ['required' => false])
            ->add('num_units', NumberType::class)
            ->add('end_date', DateTimeType::class, [
                'date_widget' => 'single_text',
                'date_format' => 'yyyy-MM-dd',
                ])
            ->add('start_date', DateTimeType::class, [
                'required' => false,
                'date_widget' => 'single_text',
                'date_format' => 'yyyy-MM-dd',
                ])
            ->add('description', TextType::class)
            ->add('slug', TextType::class, ['required' => false])
            ->add(
                'editors',
                EntityType::class,
                [
                    'class' => User::class,
                    'multiple' => true,
                    'choice_label' => 'email',
                    // This will need improvement later down the line - but fine for now
                    // TODO: This shouldn't include the own user
                    'choice_value' => function (?User $user) {
                        return $user ? $user->getEmail() : '';
                    },
                    'required' => false,
                ]
            );
    }

这是从前端传递的模型:

    const auctionUpdate: AuctionType = {
      company: this.auctionForm.controls.company.value,
      description: this.auctionForm.controls.description.value,
      editors: [],
      end_date: this.datePipe.transform(this.auction.end_date, 'yyyy-MM-dd'),
      maximum_unit_amount: this.auction.maximum_unit_amount,
      minimum_unit_price: this.auction.minimum_unit_price,
      name: this.auctionForm.controls.title.value,
      num_units: this.auction.num_units,
      slug: this.auction.slug,
      start_date: this.datePipe.transform(this.auction.start_date, 'yyyy-MM-dd'),
    };

注意: this.auction.start_datethis.auction.end_date 的类型为 Date。我尝试了两种不同的日期格式,例如 yyyy-MM-dd 和 yyyy-MM-dd HH:mm: SS (我们想要的格式)。

一般来说,我对 Symfony 和 PHP 比较陌生,所以这可能是基础的。我在这里缺少什么?

我的理解是,根据 Symfony 关于 DateTimeType 的文档,以字符串形式传递日期在 Symfony 中是有效的,并且我正在匹配数据库中存储的所有其他日期的日期格式。所以,我不太清楚为什么会被拒绝。

任何对此的见解将不胜感激!

PS:如果您需要查看更多上下文/代码来评估问题,请告诉我,我会添加它!

I am attempting to hit an endpoint to update an Auction entity, which has a start_date and end_date property, both of type DateTimeType. However, when I pass an Auction entity to my API from my Angular front-end, I am being told that the start_date and end_date values are not valid.

Here is the current state of my AuctionType (Form):

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class)
            ->add('company', TextType::class)
            ->add('minimum_unit_price', NumberType::class)
            ->add('maximum_unit_amount', NumberType::class, ['required' => false])
            ->add('num_units', NumberType::class)
            ->add('end_date', DateTimeType::class, [
                'date_widget' => 'single_text',
                'date_format' => 'yyyy-MM-dd',
                ])
            ->add('start_date', DateTimeType::class, [
                'required' => false,
                'date_widget' => 'single_text',
                'date_format' => 'yyyy-MM-dd',
                ])
            ->add('description', TextType::class)
            ->add('slug', TextType::class, ['required' => false])
            ->add(
                'editors',
                EntityType::class,
                [
                    'class' => User::class,
                    'multiple' => true,
                    'choice_label' => 'email',
                    // This will need improvement later down the line - but fine for now
                    // TODO: This shouldn't include the own user
                    'choice_value' => function (?User $user) {
                        return $user ? $user->getEmail() : '';
                    },
                    'required' => false,
                ]
            );
    }

Here is the model being passed from the front-end:

    const auctionUpdate: AuctionType = {
      company: this.auctionForm.controls.company.value,
      description: this.auctionForm.controls.description.value,
      editors: [],
      end_date: this.datePipe.transform(this.auction.end_date, 'yyyy-MM-dd'),
      maximum_unit_amount: this.auction.maximum_unit_amount,
      minimum_unit_price: this.auction.minimum_unit_price,
      name: this.auctionForm.controls.title.value,
      num_units: this.auction.num_units,
      slug: this.auction.slug,
      start_date: this.datePipe.transform(this.auction.start_date, 'yyyy-MM-dd'),
    };

Note: The this.auction.start_date and this.auction.end_date are of the type Date.I have tried two different date formats, such as yyyy-MM-dd and yyyy-MM-dd HH:mm:ss (our desired format).

I am relatively new to Symfony and PHP in general, so this may be basic. What am I missing here??

My understanding is that passing a date in the form of a string is valid in Symfony according to their documentation on the DateTimeType and I am matching the date format of all other dates stored in our database. So, I am not too sure why this is being rejected.

Any insight on this would be greatly appreciated!

PS: If you need more context/code to look at in order to assess the issue, please let me know and I will add it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文