【问题标题】:Symfony2 validate text input when containing something onlySymfony2 在仅包含某些内容时验证文本输入
【发布时间】:2016-01-03 04:12:28
【问题描述】:

我想验证一个包含下拉菜单和文本输入字段的表单。

用户可以从下拉菜单中选择一个项目。如果他想创建一个新项目,他可以使用下拉菜单旁边的文本输入字段。

这是我的上传类型:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->setAction('upload')
        ->setMethod('POST')

        // project name dropdown menu
        ->add('projectname', 'choice' , array(
            'label' => 'upload_project_label',
            'choices'  => $this->projects,
            'attr' => array(
                'class' => 'form-control some',
                'required' => 'true'
            )
        ))

        // newprojectname text input
        ->add('newprojectname', 'text', array(
                'label' => false,
                'attr' => array(
                    'class' => 'form-control',
                    'required' => false,
                    'placeholder' => 'upload_newprojectname_placeholder'
                )
            )
        )
...

这是来自我的上传实体的 sn-p:

/**
 * @ORM\Column(type="text")
 *
 * @var string $projectname
 * @Assert\NotBlank()
 */
protected $projectname;

/**
 * @ORM\Column(type="text")
 *
 * @var string $newprojectname
 * @Assert\Length(
 *     min = 3,
 *     max = 7,
 *     minMessage = "min message",
 *     maxMessage = "max message"
 * )
 */
protected $newprojectname;

我的问题是是否有可能通过查询来检查字段 newproject 是否已设置(即输入了一个字符串)?如果是这样,让 Assert 注释完成它的工作。

【问题讨论】:

  • 我不明白...你要检查该字段是否不为空,然后呢?
  • 您可以创建一个Callback 断言。
  • @pabgaran - 我想检查是否在该字段中输入了某些内容,如果是,请使用 Assert 注释检查输入的值。够清楚吗?

标签: validation symfony annotations assert


【解决方案1】:

这可以通过多种方式完成,所有这些都可能满足您的要求。

  1. 使用custom callback - 这是最快最直接的方法
  2. 使用expression validator - 很多人在 PHP 中嵌入元语言时遇到问题,这是完全有效的,但这是另一种快速的做事方式
  3. 使用group sequences,特别是group sequence provider 功能

您选择哪一个取决于您,但如果您的验证约束变得更加复杂,回调是一个快速且简单的起点。

【讨论】:

  • 感谢您的回答。我通过使用@Frasci 提到的自定义回调找到了解决方案。将在下面发布代码。
【解决方案2】:

这是作为自定义回调验证的建议解决方案的代码块。

我必须在我的上传实体中添加另一个函数,如下所示:

use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
 * Function holds custom validation for fields in import/upload form
 * @Assert\Callback
 * @param ExecutionContextInterface $context
 */
public function validate(ExecutionContextInterface $context)
{
    // check new project name
    $newProjectName = $this->getNewprojectname();
    if(!empty($newProjectName)) {

        // letters only
        $pattern = '/[a-zA-Z]/';
        if($this->isPatternWrong($newProjectName, $pattern)) {
            $context
                ->buildViolation('Please use letters only.')
                ->atPath('newprojectname')
                ->addViolation();
        }

        // text max. 7 digits
        $maxlength = 7;
        if($this->isStringTooLong($newProjectName, $maxlength)) {
            $context
                ->buildViolation('Max. length 7 digits.')
                ->atPath('newprojectname')
                ->addViolation();
        }
    }
}

private function isPatternWrong($string, $pattern)
{
    $result = preg_match($pattern, $string);
    if($result === 0) {
        return true;
    }
    return false;
}

private function isStringTooLong($string, $length)
{
    if(strlen($string) > $length) {
        return true;
    }
    return false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2017-06-07
    • 1970-01-01
    相关资源
    最近更新 更多