ich einen benutzerdefinierten Validator in src/VNN/PressboxBundle/Component/Validator/CurrentPasswordValidator.php
haben:Symfony2 benutzerdefinierten Validator Annotation nicht funktioniert
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\ConstraintValidator,
Symfony\Component\Validator\Constraint,
Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface,
Symfony\Component\Security\Core\SecurityContextInterface,
JMS\DiExtraBundle\Annotation\Validator,
JMS\DiExtraBundle\Annotation\InjectParams,
JMS\DiExtraBundle\Annotation\Inject;
/**
* @Validator("user.validator.current_password")
*/
class CurrentPasswordValidator extends ConstraintValidator
{
// ...
}
Und dann habe ich diese bei src/VNN/PressboxBundle/Component/Validator/Contraints/CurrentPassword.php
:
<?php
namespace VNN\PressboxBundle\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class CurrentPassword extends Constraint
{
public $message = "Your current password is not valid";
/**
* @return string
*/
public function validatedBy()
{
return 'user.validator.current_password';
}
}
Aus irgendeinem Grund, wenn ich versuche, eine Anmerkung hinzufügen das verwendet diesen Validator, erhalte ich einen Fehler. Hier ist meine Einheit/Anmerkung:
<?php
namespace VNN\PressboxBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinTable as JoinTable;
use Doctrine\ORM\Mapping\JoinColumn as JoinColumn;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\Email;
use VNN\PressboxBundle\Component\Validator\Constraints\CurrentPassword;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* VNN\PressboxBundle\Entity\User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User implements UserInterface, \Serializable
{
/**
* @var string $password
* @Assert\NotBlank()
* @CurrentPassword()
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
}
Der Fehler Ich erhalte ist:
AnnotationException: [Semantische Fehler] Die Anmerkung "@VNN \ PressboxBundle \ Component \ Validator \ Constraints \ CurrentPassword" in Eigenschaft VNN \ PressboxBundle \ Entity \ User :: $ Passwort existiert nicht, oder konnte nicht automatisch geladen werden.
Was mache ich falsch?
Danke für den Gedanken. Ich habe überprüft und 'VNNPressboxBundle' ist ordnungsgemäß registriert. –
@JasonSwett: Meine Antwort mit einigen anderen Möglichkeiten aktualisiert. – leek