/** * Validate an associative array using the constraints defined in * getConstraints() and getOptionalConstraints(). * * All constraints from getConstraints() are used. * * Constraints from getOptionalConstraints() are only used if the field exists in $values. * * @param array $values Values to validate * @param AbstractEntity $contextEntity The existing entity to which the validated values * will be applied. Optional. * @return ConstraintViolationList */ public function validate(array $values, AbstractEntity $contextEntity = null) { $this->contextData = $values; $constraints = $this->getConstraints($values, $contextEntity); $arrayConstraint = new Assert\Collection(['fields' => $constraints, 'missingFieldsMessage' => self::MISSING]); return $this->validator->validateValue($values, $arrayConstraint); }
/** * @param string $dataClass Parent entity class name * @param File|Attachment $entity File entity * @param string $fieldName Field name where new file/image field was added * * @return \Symfony\Component\Validator\ConstraintViolationListInterface */ public function validate($dataClass, $entity, $fieldName = '') { /** @var Config $entityAttachmentConfig */ if ($fieldName === '') { $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass); $mimeTypes = $this->getMimeArray($entityAttachmentConfig->get('mimetypes')); if (!$mimeTypes) { $mimeTypes = array_merge($this->getMimeArray($this->config->get('oro_attachment.upload_file_mime_types')), $this->getMimeArray($this->config->get('oro_attachment.upload_image_mime_types'))); } } else { $entityAttachmentConfig = $this->attachmentConfigProvider->getConfig($dataClass, $fieldName); /** @var FieldConfigId $fieldConfigId */ $fieldConfigId = $entityAttachmentConfig->getId(); if ($fieldConfigId->getFieldType() === 'file') { $configValue = 'upload_file_mime_types'; } else { $configValue = 'upload_image_mime_types'; } $mimeTypes = $this->getMimeArray($this->config->get('oro_attachment.' . $configValue)); } $fileSize = $entityAttachmentConfig->get('maxsize') * 1024 * 1024; foreach ($mimeTypes as $id => $value) { $mimeTypes[$id] = trim($value); } return $this->validator->validateValue($entity->getFile(), [new FileConstraint(['maxSize' => $fileSize, 'mimeTypes' => $mimeTypes, 'mimeTypesMessage' => 'This file type is not allowed.'])]); }
/** * @expectedException \Symfony\Component\Validator\Exception\ValidatorException */ public function testValidateValueRejectsValid() { $entity = new Entity(); $metadata = new ClassMetadata(get_class($entity)); $this->metadataFactory->addMetadata($metadata); $this->validator->validateValue($entity, new Valid()); }
/** * @param string $email * * @throws \Symfony\Component\Validator\Exception\ValidatorException */ protected function validateAddress($email) { $emailConstraint = new EmailConstraints(); $emailConstraint->message = 'Invalid email address'; if ($email) { $errorList = $this->validator->validateValue($email, $emailConstraint); if ($errorList && $errorList->count() > 0) { throw new ValidatorException($errorList->get(0)->getMessage()); } } }
/** * This method checks whether all required parameters exist in the given * PaymentInstruction, and whether they are syntactically correct. * * This method is meant to perform a fast parameter validation; no connection * to any payment back-end system should be made at this stage. * * In case, this method is not implemented. The PaymentInstruction will * be considered to be valid. * * @param PaymentInstructionInterface $instruction * * @throws \JMS\Payment\CoreBundle\Plugin\Exception\InvalidPaymentInstructionException if the the PaymentInstruction is not valid */ public function checkPaymentInstruction(PaymentInstructionInterface $instruction) { // define form validators $constraints = new Assert\Collection(array('name' => array(new Assert\NotBlank(array('message' => 'Required'))), 'number' => array(new Assert\NotBlank(array('message' => 'Required')), new Assert\Length(array('min' => 12, 'max' => 19, 'minMessage' => 'Invalid card number 1', 'maxMessage' => 'Invalid card number 2')), new Assert\Luhn(array('message' => 'Invalid card number'))), 'exp_month' => array(new Assert\NotBlank(array('message' => 'Required')), new Assert\Range(array('min' => 1, 'max' => 12, 'minMessage' => 'Invalid code value', 'maxMessage' => 'Invalid code value'))), 'exp_year' => array(new Assert\NotBlank(array('message' => 'Required')), new Assert\Range(array('min' => date('Y'), 'max' => date('Y', strtotime('+20 years')), 'minMessage' => 'Invalid date', 'maxMessage' => 'Invalid date'))), 'cvc' => array(new Assert\NotBlank(array('message' => 'Required')), new Assert\Length(array('min' => 3, 'max' => 4, 'minMessage' => 'Invalid code value', 'maxMessage' => 'Invalid code value'))), 'address_line1' => array(new Assert\NotBlank(array('message' => 'Required'))), 'address_line2' => array(), 'address_city' => array(new Assert\NotBlank(array('message' => 'Required'))), 'address_state' => array(new Assert\NotBlank(array('message' => 'Required'))), 'address_country' => array(new Assert\NotBlank(array('message' => 'Required'))), 'address_zip' => array(new Assert\NotBlank(array('message' => 'Required'))))); // extract form values from extended data $dateToValidate = array(); foreach ($constraints->fields as $name => $constraint) { $dateToValidate[$name] = $instruction->getExtendedData()->get($name); } // validate input data $errors = $this->validator->validateValue($dateToValidate, $constraints); // transform validator errors into payment exceptions $errorBuilder = new ErrorBuilder(); foreach ($errors as $error) { // KLUDGE: remove [] around field name $field = substr($error->getPropertyPath(), 1, -1); $errorBuilder->addDataError('data_stripe_credit_card.' . $field, $error->getMessage()); } if ($errorBuilder->hasErrors()) { throw $errorBuilder->getException(); } }
/** * @param Request $request * @return array|RedirectResponse * * @Extra\Template("WeavingTheWebUserBundle:Settings:show.html.twig") */ public function saveAction(Request $request) { $currentUser = $this->getUser(); $form = $this->getSettingsForm($currentUser); $form->handleRequest($request); if ($request->getMethod() === 'POST') { if ($form->isValid()) { $currentPassword = $form->get('currentPassword')->getData(); $errorList = $this->validator->validateValue($currentPassword, new UserPassword()); if (count($errorList) === 0) { $plainPassword = $form->get('plainPassword')->getData(); if (strlen(trim($plainPassword)) === 0) { $currentUser->setPlainPassword($currentPassword); } $this->userManager->updateUser($currentUser); return new RedirectResponse($this->router->generate('weaving_the_web_user_show_settings')); } else { $currentPasswordError = $this->translator->trans('field_error_current_password', [], 'user'); $form->get('currentPassword')->addError(new FormError($currentPasswordError)); } } } return ['form' => $form->createView()]; }
/** * @param $data * @return \Symfony\Component\Validator\ConstraintViolationListInterface */ public function validate($data) { $constraints = [new Assert\NotBlank()]; return $this->validator->validateValue($data, $constraints); }
/** * Validate params. * * @param \Symfony\Component\Validator\Validator $validator * @param array $params * @param \Symfony\Component\HttpFoundation\ParameterBag $values * * @return \Symfony\Component\Validator\ConstraintViolationList */ protected function validateParams(Validator $validator, array $params, ParameterBag $values) { $violations = new ConstraintViolationList(); foreach ($params as $param) { if (empty($param['requirements'])) { continue; } $value = $values->get($param['name']); $paramViolations = $validator->validateValue($value, $param['requirements']); // add missing data foreach ($paramViolations as $violation) { $extendedViolation = new ConstraintViolation($violation->getMessage(), $violation->getMessageTemplate(), $violation->getMessageParameters(), $violation->getRoot(), $param['name'] . $violation->getPropertyPath(), $violation->getInvalidValue(), $violation->getMessagePluralization(), $violation->getCode()); $violations->add($extendedViolation); } } return $violations; }