【问题标题】:symfony2 custom validator FatalErrorExceptionsymfony2 自定义验证器 FatalErrorException
【发布时间】:2015-03-11 09:09:36
【问题描述】:

我正在使用 Symfony2,我正在尝试为我的班级构建一个自定义验证器。

我按照指南找到了Here

我收到一个奇怪的错误:

FatalErrorException: Compile Error: Cannot redeclare class Acme\MyProject\Validator\Constraints\isUniqueNameInPlaces in /home/myfolder/src/Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php line 123

但是,文件 isUniqueNameInPlaces.php 只有 14 行长。 我确信我做了一些愚蠢的事情,但我不知道是什么。有人可以帮帮我吗?

这是我的 isUniqueNameInPlaces

  <?php

  // src/Acme/MyProject/Validator/Constraints/UniqueNameInPlaces.php
  namespace Acme\MyProject\Validator\Constraints;

  use Symfony\Component\Validator\Constraint;

  /**
  * @Annotation
  */
  class isUniqueNameInPlaces extends Constraint
  {
      public $message = 'Name is not unique';
  }

这是它的验证器

  <?php

  // src/Acme/MyProject/Validator/Constraints/isUniqueNameInPlacesValidator.php
  namespace Acme\MyProject\Validator\Constraints;

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

  class isUniqueNameInPlacesValidator extends ConstraintValidator
  {

      public function getTargets()
      {
          return self::CLASS_CONSTRAINT;
      }

      public function validate($protocol, Constraint $constraint)
      {
  //      my logic
      }
  }

【问题讨论】:

  • 你确定这两个文件的名字反映了类名吗?

标签: validation symfony


【解决方案1】:

您已经在别处有一个 isUn​​iqueNameInPlaces 类?因此错误无法重新声明类。
给你的 isUniqueNameInPlaces 类起一个其他名字,然后看看是否可以。

编辑:

你的文件不应该是
Acme/MyProject/Validator/Constraints/isUniqueNameInPlaces.php
而不是
Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php ?

【讨论】:

  • 评论您的课程,清空缓存并重试。会发生什么?
  • 如果不是更好,那就用硬方式缓存你:rm -rf app/cache/* 然后再试一次......
  • 注释约束类,我得到:RuntimeException: The autoloader expected class "Acme\MyProjectBundle\Validator\Constraints\isUniqueNameInPlaces" to be defined in file "/home/myfolder/src/Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
  • 您的文件不应该是 Acme/MyProject/Validator/Constraints/isUniqueNameInPlaces.php 而不是 Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php 吗?请参阅 MyProject 而不是 MyProjectBundle ?
  • 是的......你是对的!我知道这是一个愚蠢的错误...非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
相关资源
最近更新 更多