/** * @return $this */ protected function username() { $recordExistsValidator = new NoObjectExists(array('object_repository' => $this->sm->get('Doctrine\\ORM\\EntityManager')->getRepository('User\\Entity\\User'), 'fields' => 'username')); $recordExistsValidator->setMessage('User with this email already exists', NoObjectExists::ERROR_OBJECT_FOUND); $this->add(array('name' => 'username', 'required' => true, 'validators' => array($recordExistsValidator, array('name' => 'StringLength', 'options' => array('min' => 3, 'max' => 100))), 'filters' => array(array('name' => 'StripTags'), array('name' => 'StringTrim')))); return $this; }
public function testCannotValidateWithAvailableObjectInRepository() { $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->once())->method('findOneBy')->will($this->returnValue(new stdClass())); $validator = new NoObjectExists(array('object_repository' => $repository, 'fields' => 'matchKey')); $this->assertFalse($validator->isValid('matchValue')); }
public function testErrorMessageIsStringInsteadArray() { $repository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $repository->expects($this->once())->method('findOneBy')->will($this->returnValue(new stdClass())); $validator = new NoObjectExists(array('object_repository' => $repository, 'fields' => 'matchKey')); $this->assertFalse($validator->isValid('matchValue')); $messageTemplates = $validator->getMessageTemplates(); $expectedMessage = str_replace('%value%', 'matchValue', $messageTemplates[NoObjectExists::ERROR_OBJECT_FOUND]); $messages = $validator->getMessages(); $receivedMessage = $messages[NoObjectExists::ERROR_OBJECT_FOUND]; $this->assertTrue($expectedMessage == $receivedMessage); }
/** * @return ViewModel */ public function createAction() { $entityManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $repository = $entityManager->getRepository('Categories\\Entity\\Categories'); /** @var \Categories\Service\Categories $categoriesService */ $categoriesService = $this->getServiceLocator()->get('Categories\\Service\\Categories'); $form = $this->getCreateForm(); $parentId = !$this->params()->fromRoute('parentId') ? null : $this->params()->fromRoute('parentId'); $urlHelper = $this->getServiceLocator()->get('viewhelpermanager')->get('url'); $form->setAttribute('action', $urlHelper('categories/create', ['parentId' => $parentId])); if ($this->getRequest()->isPost()) { $form->setInputFilter(new Filter\CreateInputFilter($this->getServiceLocator())); $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $aliasValid = new Validator\NoObjectExists(['object_repository' => $repository, 'fields' => ['alias', 'parentId']]); if ($aliasValid->isValid(['alias' => $form->get('alias')->getValue(), 'parentId' => $parentId])) { $category = $this->getEntity(); $category->setParentId(!$parentId ? null : $repository->find($parentId)); $category->setOrder($this->getMaxOrder($parentId)); $hydrator = new DoctrineHydrator($entityManager); $hydrator->hydrate($form->getData(), $category); $entityManager->persist($category); $entityManager->flush(); //Add image from session if ($categoriesService->ifImagesExist()) { $imageService = $this->getServiceLocator()->get('Media\\Service\\File'); foreach ($categoriesService->getSession()->ids as $imageId) { $imageService->associateFileWithObject($this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager')->getRepository('Media\\Entity\\File')->find($imageId), $category); } $categoriesService->clearImages(); } $this->flashMessenger()->addSuccessMessage('Category has been successfully created!'); if (!$this->getRequest()->isXmlHttpRequest()) { return $this->redirect()->toRoute('categories/default', array('controller' => 'management', 'action' => 'index')); } else { return; } } else { $form->get('alias')->setMessages(array('errorMessageKey' => 'Alias must be unique in it\'s category!')); } } } else { $categoriesService->clearImages(); } $imageService = new File($this->getServiceLocator()); $viewModel = new ViewModel(); $viewModel->setVariables(['form' => $form, 'fileUpload' => ['imageService' => $imageService, 'module' => 'image-categories', 'type' => \Media\Entity\File::IMAGE_FILETYPE, 'id' => null]]); $viewModel->setTerminal($this->getRequest()->isXmlHttpRequest()); return $viewModel; }
public function __construct(array $options) { parent::__construct($options); if (isset($options['additionalFields'])) { $this->additionalFields = $options['additionalFields']; } }
public function __construct(array $options) { parent::__construct($options); if (!isset($options['id'])) { throw new InvalidArgumentException('Chave "id" deve ser especificada na procura por outras entidades'); } if (isset($options['additionalFields'])) { $this->additionalFields = $options['additionalFields']; } $this->id = $options['id']; }
public function __construct(EntityManager $em) { $input = new Input('prenom'); $validator = new NotEmpty(); $validator->setMessage('Le prénom est obligatoire', NotEmpty::IS_EMPTY); $input->getValidatorChain()->attach($validator); $filter = new StringTrim(); $input->getFilterChain()->attach($filter); $this->add($input); $input = new Input('email'); $input->setRequired(false); // TODO ne fonctionne pas pour l'UPDATE $validator = new NoObjectExists(array('object_repository' => $em->getRepository('AddressBook\\Entity\\Contact'), 'fields' => 'email')); $validator->setMessage("Un utilisateur utilise déjà cet email", NoObjectExists::ERROR_OBJECT_FOUND); $input->getValidatorChain()->attach($validator); if (class_exists('Zend\\Filter\\ToNull')) { $filter = new \Zend\Filter\ToNull(); } else { if (class_exists('Zend\\Filter\\Null')) { $filter = new \Zend\Filter\Null(); } } $input->getFilterChain()->attach($filter); $this->add($input); $input = new Input('societe'); $input->setRequired(false); if (class_exists('Zend\\Filter\\ToNull')) { $filter = new \Zend\Filter\ToNull(); } else { if (class_exists('Zend\\Filter\\Null')) { $filter = new \Zend\Filter\Null(); } } $input->getFilterChain()->attach($filter); $this->add($input); }
/** * @param array $options * @throws \Zend\Validator\Exception\InvalidArgumentException */ public function __construct(array $options) { parent::__construct($options); if (!isset($options['join']) || !is_scalar($options['join'])) { if (!isset($options['join'])) { $provided = 'nothing'; } else { if (is_object($options['join'])) { $provided = get_class($options['join']); } else { $provided = getType($options['join']); } } throw new Exception\InvalidArgumentException(sprintf('Option "join" is required and must be the field name' . ' of the association to join to, %s given', $provided)); } $this->join = $options['join']; }
/** * @param array $options * @throws \Zend\Validator\Exception\InvalidArgumentException */ public function __construct(array $options) { if (!isset($options['exclude']) || !is_scalar($options['exclude'])) { if (!isset($options['exclude'])) { $provided = 'nothing'; } else { if (is_object($options['exclude'])) { $provided = get_class($options['exclude']); } else { $provided = getType($options['exclude']); } } throw new Exception\InvalidArgumentException(sprintf('Option "exclude" is required and must be the primary key' . ' value of the object to exclude, %s given', $provided)); } $this->exclude = $options['exclude']; parent::__construct($options); }