/**
  * @param \eZ\Publish\Core\FieldType\Selection\Value $value
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
  * @param string $languageCode
  *
  * @return string
  */
 public function getFieldName(SPIValue $value, FieldDefinition $fieldDefinition, $languageCode)
 {
     if (empty($value->selection)) {
         return '';
     }
     $names = [];
     $fieldSettings = $fieldDefinition->getFieldSettings();
     foreach ($value->selection as $optionIndex) {
         if (isset($fieldSettings['options'][$optionIndex])) {
             $names[] = $fieldSettings['options'][$optionIndex];
         }
     }
     return implode(' ', $names);
 }
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\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 #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\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 #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\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;
 }
 /**
  * Returns default field options, created from given $fieldDefinition and $languageCode
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
  * @param string $languageCode
  * @param null|\eZ\Publish\API\Repository\Values\Content\Content $content
  *
  * @return array
  */
 protected function getDefaultFieldOptions(FieldDefinition $fieldDefinition, $languageCode, Content $content = null)
 {
     $options = array();
     $options["label"] = $fieldDefinition->getName($languageCode);
     $options["required"] = $fieldDefinition->isRequired;
     $options["ezforms"]["description"] = $fieldDefinition->getDescription($languageCode);
     $options["ezforms"]["language_code"] = $languageCode;
     $options["ezforms"]["fielddefinition"] = $fieldDefinition;
     if ($content !== null) {
         $options["ezforms"]["content"] = $content;
     }
     $options["constraints"] = array();
     if ($fieldDefinition->isRequired) {
         $options["constraints"][] = new Constraints\NotBlank();
     }
     return $options;
 }
Exemple #6
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 #7
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;
 }
 /**
  * Renders the HTML for the settings for the given field definition
  * $definition
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $definition
  * @return string
  */
 public function renderFieldDefinitionSettings(FieldDefinition $definition)
 {
     if (!$this->template instanceof Twig_Template) {
         $tpl = reset($this->renderFieldDefinitionSettingsResources);
         $this->template = $this->environment->loadTemplate($tpl['template']);
     }
     $parameters = array('fielddefinition' => $definition, 'settings' => $definition->getFieldSettings());
     return $this->template->renderBlock($this->getRenderFieldDefinitionSettingsBlockName($definition), $parameters, $this->getBlockByFieldDefinition($definition));
 }
 /**
  * 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;
 }
Exemple #10
0
 /**
  * Validates field value against 'isMultiple' setting.
  *
  * Does not use validators.
  *
  * @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\Country\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;
     }
     $fieldSettings = $fieldDefinition->getFieldSettings();
     if ((!isset($fieldSettings["isMultiple"]) || $fieldSettings["isMultiple"] === false) && count($fieldValue->countries) > 1) {
         $validationErrors[] = new ValidationError("Field definition does not allow multiple countries to be selected.", null, array());
     }
     foreach ($fieldValue->countries as $alpha2 => $countryInfo) {
         if (!isset($this->countriesInfo[$alpha2])) {
             $validationErrors[] = new ValidationError("Country with Alpha2 code '%alpha2%' is not defined in FieldType settings.", null, array("alpha2" => $alpha2));
         }
     }
     return $validationErrors;
 }
Exemple #11
0
 /**
  * Validates field value against 'isMultiple' and 'options' settings.
  *
  * Does not use validators.
  *
  * @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\Selection\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;
     }
     $fieldSettings = $fieldDefinition->getFieldSettings();
     if ((!isset($fieldSettings['isMultiple']) || $fieldSettings['isMultiple'] === false) && count($fieldValue->selection) > 1) {
         $validationErrors[] = new ValidationError('Field definition does not allow multiple options to be selected.', null, array(), 'selection');
     }
     foreach ($fieldValue->selection as $optionIndex) {
         if (!isset($fieldSettings['options'][$optionIndex])) {
             $validationErrors[] = new ValidationError('Option with index %index% does not exist in the field definition.', null, array('%index%' => $optionIndex), 'selection');
         }
     }
     return $validationErrors;
 }
 /**
  * @param FieldDefinition $fieldDefinition
  * @param array $params
  * @param int $type Either self::VIEW or self::EDIT
  *
  * @return string
  */
 private function renderFieldDefinition(FieldDefinition $fieldDefinition, array $params, $type)
 {
     if (is_string($this->baseTemplate)) {
         $this->baseTemplate = $this->twig->loadTemplate($this->baseTemplate);
     }
     $params += ['fielddefinition' => $fieldDefinition, 'settings' => $fieldDefinition->getFieldSettings()];
     return $this->baseTemplate->renderBlock($this->getRenderFieldDefinitionBlockName($fieldDefinition->fieldTypeIdentifier, $type), $this->twig->mergeGlobals($params), $this->getBlocksByFieldDefinition($fieldDefinition, $type));
 }
 /**
  * Builds SPIFieldDefinition object using API FieldDefinitionUpdateStruct
  * and API FieldDefinition
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\ContentTypeFieldDefinitionValidationException if validator configuration or
  *         field setting do not validate
  *
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct
  * @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition
  *
  * @return \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition
  */
 protected function buildSPIFieldDefinitionUpdate(FieldDefinitionUpdateStruct $fieldDefinitionUpdateStruct, APIFieldDefinition $fieldDefinition)
 {
     /** @var $fieldType \eZ\Publish\SPI\FieldType\FieldType */
     $fieldType = $this->repository->getFieldTypeService()->buildFieldType($fieldDefinition->fieldTypeIdentifier);
     $validatorConfiguration = $fieldDefinitionUpdateStruct->validatorConfiguration === null ? $fieldDefinition->validatorConfiguration : $fieldDefinitionUpdateStruct->validatorConfiguration;
     $fieldSettings = $fieldDefinitionUpdateStruct->fieldSettings === null ? $fieldDefinition->fieldSettings : $fieldDefinitionUpdateStruct->fieldSettings;
     $validationErrors = array();
     if ($fieldDefinitionUpdateStruct->isSearchable && !$fieldType->isSearchable()) {
         $validationErrors[] = new ValidationError("FieldType '{$fieldDefinition->fieldTypeIdentifier}' is not searchable");
     }
     $validationErrors = array_merge($validationErrors, $fieldType->validateValidatorConfiguration($validatorConfiguration), $fieldType->validateFieldSettings($fieldSettings));
     if (!empty($validationErrors)) {
         throw new ContentTypeFieldDefinitionValidationException($validationErrors);
     }
     $spiFieldDefinition = new SPIFieldDefinition(array("id" => $fieldDefinition->id, "fieldType" => $fieldDefinition->fieldTypeIdentifier, "name" => $fieldDefinitionUpdateStruct->names === null ? $fieldDefinition->getNames() : $fieldDefinitionUpdateStruct->names, "description" => $fieldDefinitionUpdateStruct->descriptions === null ? $fieldDefinition->getDescriptions() : $fieldDefinitionUpdateStruct->descriptions, "identifier" => $fieldDefinitionUpdateStruct->identifier === null ? $fieldDefinition->identifier : $fieldDefinitionUpdateStruct->identifier, "fieldGroup" => $fieldDefinitionUpdateStruct->fieldGroup === null ? $fieldDefinition->fieldGroup : $fieldDefinitionUpdateStruct->fieldGroup, "position" => $fieldDefinitionUpdateStruct->position === null ? $fieldDefinition->position : $fieldDefinitionUpdateStruct->position, "isTranslatable" => $fieldDefinitionUpdateStruct->isTranslatable === null ? $fieldDefinition->isTranslatable : $fieldDefinitionUpdateStruct->isTranslatable, "isRequired" => $fieldDefinitionUpdateStruct->isRequired === null ? $fieldDefinition->isRequired : $fieldDefinitionUpdateStruct->isRequired, "isInfoCollector" => $fieldDefinitionUpdateStruct->isInfoCollector === null ? $fieldDefinition->isInfoCollector : $fieldDefinitionUpdateStruct->isInfoCollector, "isSearchable" => $fieldDefinitionUpdateStruct->isSearchable === null ? $fieldDefinition->isSearchable : $fieldDefinitionUpdateStruct->isSearchable));
     $spiFieldDefinition->fieldTypeConstraints->validators = $validatorConfiguration;
     $spiFieldDefinition->fieldTypeConstraints->fieldSettings = $fieldSettings;
     $spiFieldDefinition->defaultValue = $fieldType->toPersistenceValue($fieldType->acceptValue($fieldDefinitionUpdateStruct->defaultValue));
     return $spiFieldDefinition;
 }
Exemple #14
0
 /**
  * Validates a field based on the validators in the field definition
  *
  * Does not use validators.
  *
  * @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\ISBN\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;
     }
     $fieldSettings = $fieldDefinition->getFieldSettings();
     $isbnTestNumber = preg_replace("/[\\s|\\-]/", "", trim($fieldValue->isbn));
     // Check if value and settings are inline
     if ((!isset($fieldSettings["isISBN13"]) || $fieldSettings["isISBN13"] === false) && strlen($isbnTestNumber) !== 10) {
         $validationErrors[] = new ValidationError("ISBN-10 must be 10 character length", null, array());
     } else {
         if (strlen($isbnTestNumber) === 10) {
             if (!$this->validateISBNChecksum($isbnTestNumber)) {
                 $validationErrors[] = new ValidationError("ISBN value must be in a valid ISBN-10 format", null, array());
             }
         } else {
             if (!$this->validateISBN13Checksum($isbnTestNumber, $error)) {
                 $validationErrors[] = new ValidationError($error, null, array());
             }
         }
     }
     return $validationErrors;
 }