Exemple #1
0
 /**
  * Validates a field based on the validators in the field definition.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\EmailAddress\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $errors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $errors;
     }
     $validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
     $constraints = isset($validatorConfiguration['EmailAddressValidator']) ? $validatorConfiguration['EmailAddressValidator'] : array();
     $validator = new EmailAddressValidator();
     $validator->initializeWithConstraints($constraints);
     if (!$validator->validate($fieldValue)) {
         return $validator->getMessage();
     }
     return array();
 }
Exemple #2
0
 /**
  * Validates a field based on the validators in the field definition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\TextLine\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $validationErrors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $validationErrors;
     }
     $validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
     $constraints = isset($validatorConfiguration['StringLengthValidator']) ? $validatorConfiguration['StringLengthValidator'] : array();
     if (isset($constraints['maxStringLength']) && $constraints['maxStringLength'] !== false && $constraints['maxStringLength'] !== 0 && strlen($fieldValue->text) > $constraints['maxStringLength']) {
         $validationErrors[] = new ValidationError("The string can not exceed %size% character.", "The string can not exceed %size% characters.", array("size" => $constraints['maxStringLength']), 'text');
     }
     if (isset($constraints['minStringLength']) && $constraints['minStringLength'] !== false && $constraints['minStringLength'] !== 0 && strlen($fieldValue->text) < $constraints['minStringLength']) {
         $validationErrors[] = new ValidationError("The string can not be shorter than %size% character.", "The string can not be shorter than %size% characters.", array("size" => $constraints['minStringLength']), 'text');
     }
     return $validationErrors;
 }
Exemple #3
0
 /**
  * Validates a field based on the validators in the field definition.
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\Float\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $validationErrors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $validationErrors;
     }
     $validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
     $constraints = isset($validatorConfiguration['FloatValueValidator']) ? $validatorConfiguration['FloatValueValidator'] : array();
     $validationErrors = array();
     if (isset($constraints['maxFloatValue']) && $constraints['maxFloatValue'] !== null && $fieldValue->value > $constraints['maxFloatValue']) {
         $validationErrors[] = new ValidationError('The value can not be higher than %size%.', null, array('%size%' => $constraints['maxFloatValue']), 'value');
     }
     if (isset($constraints['minFloatValue']) && $constraints['minFloatValue'] !== null && $fieldValue->value < $constraints['minFloatValue']) {
         $validationErrors[] = new ValidationError('The value can not be lower than %size%.', null, array('%size%' => $constraints['minFloatValue']), 'value');
     }
     return $validationErrors;
 }
Exemple #4
0
 /**
  * Validates a field based on the validators in the field definition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\Integer\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $validationErrors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $validationErrors;
     }
     $validatorConfiguration = $fieldDefinition->getValidatorConfiguration();
     $constraints = isset($validatorConfiguration['IntegerValueValidator']) ? $validatorConfiguration['IntegerValueValidator'] : array();
     $validationErrors = array();
     // 0 and False are unlimited value for maxIntegerValue
     if (isset($constraints['maxIntegerValue']) && $constraints['maxIntegerValue'] !== 0 && $constraints['maxIntegerValue'] !== false && $fieldValue->value > $constraints['maxIntegerValue']) {
         $validationErrors[] = new ValidationError("The value can not be higher than %size%.", null, array("size" => $constraints['maxIntegerValue']));
     }
     if (isset($constraints['minIntegerValue']) && $constraints['minIntegerValue'] !== false && $fieldValue->value < $constraints['minIntegerValue']) {
         $validationErrors[] = new ValidationError("The value can not be lower than %size%.", null, array("size" => $constraints['minIntegerValue']));
     }
     return $validationErrors;
 }
Exemple #5
0
 /**
  * Validates a field based on the validators in the field definition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\BinaryBase\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $errors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $errors;
     }
     foreach ((array) $fieldDefinition->getValidatorConfiguration() as $validatorIdentifier => $parameters) {
         switch ($validatorIdentifier) {
             // @todo There is a risk if we rely on a user built Value, since the FileSize
             // property can be set manually, making this validation pointless
             case 'FileSizeValidator':
                 if (!isset($parameters['maxFileSize']) || $parameters['maxFileSize'] == false) {
                     // No file size limit
                     break;
                 }
                 // Database stores maxFileSize in MB
                 if ($parameters['maxFileSize'] * 1024 * 1024 < $fieldValue->fileSize) {
                     $errors[] = new ValidationError("The file size cannot exceed %size% byte.", "The file size cannot exceed %size% bytes.", array("size" => $parameters['maxFileSize']));
                 }
                 break;
         }
     }
     return $errors;
 }
 /**
  * Validates a field based on the validators in the field definition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
  * @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue The field value for which an action is performed
  *
  * @return \eZ\Publish\SPI\FieldType\ValidationError[]
  */
 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
 {
     $errors = array();
     if ($this->isEmptyValue($fieldValue)) {
         return $errors;
     }
     if (isset($fieldValue->inputUri) && !getimagesize($fieldValue->inputUri)) {
         $errors[] = new ValidationError("A valid image file is required.");
     }
     // BC: Check if file is a valid image if the value of 'id' matches a local file
     if (isset($fieldValue->id) && file_exists($fieldValue->id) && !getimagesize($fieldValue->id)) {
         $errors[] = new ValidationError("A valid image file is required.");
     }
     foreach ((array) $fieldDefinition->getValidatorConfiguration() as $validatorIdentifier => $parameters) {
         switch ($validatorIdentifier) {
             case 'FileSizeValidator':
                 if (!isset($parameters['maxFileSize']) || $parameters['maxFileSize'] == false) {
                     // No file size limit
                     break;
                 }
                 // Database stores maxFileSize in MB
                 if ($parameters['maxFileSize'] * 1024 * 1024 < $fieldValue->fileSize) {
                     $errors[] = new ValidationError("The file size cannot exceed %size% byte.", "The file size cannot exceed %size% bytes.", array("size" => $parameters['maxFileSize']));
                 }
                 break;
         }
     }
     return $errors;
 }