【问题标题】:Custom input type JSON validation自定义输入类型 JSON 验证
【发布时间】:2019-02-18 05:03:21
【问题描述】:

我正在构建一个自定义表单输入类型,我对如何在官方文档中实现它的验证有点困惑:https://symfony.com/doc/current/form/create_custom_field_type.html

我的字段类型基本上允许用户插入任意数量的值。它由 JavaScript 处理并呈现为隐藏输入,将值保存为 JSON 数组。

脚本运行良好,现在我需要做的就是确保输入值包含一个有效的单维 JSON 数组,以防用户使用浏览器的开发工具弄乱它,我希望它能够处理通过输入类来最大化字段的可重用性。

我怎样才能做到这一点?

这是我的输入类,但它几乎是空的:

<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

class ListType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([]);
    }

    public function getParent()
    {
        return HiddenType::class;
    }
}

谢谢

【问题讨论】:

  • 您将如何处理这些数据?我的问题是了解您的需求。你的实体是什么样子的
  • 为什么不直接使用现有的 HTML 输入元素将数据作为数组自动提交? stackoverflow.com/questions/20184670/…
  • @Mcsky 我有一个动态的输入类型结构,可以创建并链接到我网站的任何内容。到目前为止,输入值可以是文本输入、布尔值或 JSON 数组,我可能会添加更多。数据存储在我实体的文本字段中
  • @Chip Dean 我明天上班时会检查这个
  • 您想验证input value contains a valid single-dimension JSON array,但您说每个输入都可以包含文本输入、布尔值或JSON array。所以你至少可以有 2 个级别的数组。您可以为您的实体字段做自己的验证器。检查此symfony.com/doc/current/validation/custom_constraint.html。如果需要,我可以提供示例作为答案

标签: php symfony


【解决方案1】:

这就是最终使用自定义验证器的方式,欢迎使用更好的解决方案:

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

use App\Validator\Constraints\SingleDimensionJSON();

class ListType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'constraints' => [
                new SingleDimensionJSON()
            ]
        ]);
    }

    public function getParent()
    {
        return HiddenType::class;
    }
}

还有验证器类:

namespace App\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class SingleDimensionJSON extends ConstraintValidator
{
    public function validate($value, Constraint $constraint)
    {
        if ($value === null || $value === '') {
            return;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }

        $toArray = json_decode($value, true);

        if ($toArray === null) {
            $this->context
                ->buildViolation($constraint->message)
                ->setParameter('{{ string }}', $value)
                ->addViolation()
            ;
            return;
        }
        foreach ($toArray as $item) {
            if (is_array($item)) {
                throw new InvalidArgumentException('Multi dimensional array isn\'t allowed');
            }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2018-01-05
    • 2018-03-18
    • 1970-01-01
    • 2019-11-06
    • 2014-01-17
    相关资源
    最近更新 更多