/** * @return $this */ protected function email() { $recordExistsValidator = new UniqueObject(array('object_repository' => $this->sm->get('Doctrine\\ORM\\EntityManager')->getRepository('Test\\Entity\\Test'), 'object_manager' => $this->sm->get('Doctrine\\ORM\\EntityManager'), 'fields' => 'email', 'use_context' => true)); $recordExistsValidator->setMessage('This email already in use', UniqueObject::ERROR_OBJECT_NOT_UNIQUE); $this->add(array('name' => 'email', 'required' => true, 'validators' => array(array('name' => 'EmailAddress'), $recordExistsValidator), 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim')))); return $this; }
/** * @return $this */ protected function email() { $uniqueObjectValidator = new UniqueObject(['object_repository' => $this->sm->get('Doctrine\\ORM\\EntityManager')->getRepository('User\\Entity\\User'), 'fields' => ['email'], 'object_manager' => $this->sm->get('Doctrine\\ORM\\EntityManager'), 'use_context' => true]); $uniqueObjectValidator->setMessage('Email is already taken', UniqueObject::ERROR_OBJECT_NOT_UNIQUE); $this->add(array('name' => 'email', 'required' => true, 'validators' => array(array('name' => 'EmailAddress'), $uniqueObjectValidator), 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim')))); return $this; }
/** * @return $this */ protected function entity() { $recordUniqueValidator = new UniqueObject(['object_repository' => $this->sm->get('Doctrine\\ORM\\EntityManager')->getRepository('Comment\\Entity\\EntityType'), 'fields' => ['entity'], 'object_manager' => $this->sm->get('Doctrine\\ORM\\EntityManager')]); $recordUniqueValidator->setMessage('Entity type with this entity already exists'); $this->add(['name' => 'entity', 'required' => true, 'filters' => [['name' => 'StripTags'], ['name' => 'StringTrim']], 'validators' => [$recordUniqueValidator]]); return $this; }
/** * Returns false if there is another object with the same field values but other identifiers. * * @access public * @param mixed $value * @param array $context ,default is null * @return boolean is valid or not */ public function isValid($value, $context = null) { if (count($this->fields) > 1) { $value = $context; } return parent::isValid($value, $context); }
public function __construct(EntityManager $objectManager, $type) { // Firstname $firstname = new Input('prenom'); $firstname->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $firstname->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 50))); $this->add($firstname); // Lastname $lastname = new Input('nom'); $lastname->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $lastname->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 50))); $this->add($lastname); //Unique email validator $uniqueUserEmail = new UniqueObject(array('object_manager' => $objectManager, 'object_repository' => $objectManager->getRepository('Application\\Entity\\Utilisateur'), 'fields' => 'email', 'use_context' => true)); $uniqueUserEmail->setMessage('L\'email renseigné est déjà utilisé par un autre utilisateur.', 'objectNotUnique'); // Email $email = new Input('email'); $email->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $email->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 255)))->attach(new EmailAddress())->attach($uniqueUserEmail); $this->add($email); $editCallback = new Callback(function ($value) { if ($value == '') { return true; } /** @var StringLength $validator */ $validator = new StringLength(array('min' => 6, 'max' => 32)); return $validator->isValid($value); }); $editCallback->setMessage('Le mot de passe doit contenir entre 6 et 32 caractères.'); // Password $password = new Input('password'); $password->setRequired($type == UtilisateurForm::TYPE_ADD)->setContinueIfEmpty(true); $password->getFilterChain()->attach(new StringTrim()); $password->getValidatorChain()->attach($type == UtilisateurForm::TYPE_ADD ? new StringLength(array('min' => 6, 'max' => 32)) : $editCallback); $this->add($password); // Password Confirmation $passwordConfirmation = new Input('passwordConfirmation'); $passwordConfirmation->setRequired($type == UtilisateurForm::TYPE_ADD)->setContinueIfEmpty(true); $passwordConfirmation->getFilterChain()->attach(new StringTrim()); $passwordConfirmation->getValidatorChain()->attach($type == UtilisateurForm::TYPE_ADD ? new StringLength(array('min' => 6, 'max' => 32)) : $editCallback)->attach(new Identical('password')); $this->add($passwordConfirmation); $role = new Input('role'); $role->setRequired(false); $role->getFilterChain()->attach(new Digits()); $this->add($role); }
public function testCanFetchIdentifierFromObjectContext() { $context = new stdClass(); $context->id = 'identifier'; $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->at(0))->method('getIdentifierValues')->with($context)->will($this->returnValue(array('id' => 'identifier'))); $classMetadata->expects($this->at(1))->method('getIdentifierValues')->with($match)->will($this->returnValue(array('id' => 'identifier'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey', 'use_context' => true)); $this->assertTrue($validator->isValid('matchValue', $context)); }
public function testErrorMessageIsStringInsteadArray() { $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue(array('id'))); $classMetadata->expects($this->once())->method('getIdentifierValues')->with($match)->will($this->returnValue(array('id' => 'identifier'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey', 'use_context' => true)); $this->assertFalse($validator->isValid('matchValue', array('matchKey' => 'matchValue', 'id' => 'another identifier'))); $messageTemplates = $validator->getMessageTemplates(); $expectedMessage = str_replace('%value%', 'matchValue', $messageTemplates[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]); $messages = $validator->getMessages(); $receivedMessage = $messages[UniqueObject::ERROR_OBJECT_NOT_UNIQUE]; $this->assertTrue($expectedMessage == $receivedMessage); }
/** * @expectedException Zend\Validator\Exception\RuntimeException * @expectedExceptionMessage Expected context to contain id */ public function testThrowsAnExceptionOnMissingIdentifierInContext() { $match = new stdClass(); $classMetadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'); $classMetadata->expects($this->once())->method('getIdentifierFieldNames')->will($this->returnValue(array('id'))); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $objectManager->expects($this->any())->method('getClassMetadata')->with('stdClass')->will($this->returnValue($classMetadata)); $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->any())->method('getClassName')->will($this->returnValue('stdClass')); $repository->expects($this->once())->method('findOneBy')->with(array('matchKey' => 'matchValue'))->will($this->returnValue($match)); $validator = new UniqueObject(array('object_repository' => $repository, 'object_manager' => $objectManager, 'fields' => 'matchKey')); $validator->isValid('matchValue', array()); }