eZ\Publish\Core\FieldType\Validator\FloatValueValidator::validate PHP Method

validate() public method

Will return true when all constraints are matched. If one or more constraints fail, the method will return false. When a check against a constant has failed, an entry will be added to the $errors array.
public validate ( Value $value ) : boolean
$value eZ\Publish\Core\FieldType\Value
return boolean
    public function validate(BaseValue $value)
    {
        $isValid = true;
        if ($this->constraints['maxFloatValue'] !== null && $value->value > $this->constraints['maxFloatValue']) {
            $this->errors[] = new ValidationError('The value can not be higher than %size%.', null, array('%size%' => $this->constraints['maxFloatValue']));
            $isValid = false;
        }
        if ($this->constraints['minFloatValue'] !== null && $value->value < $this->constraints['minFloatValue']) {
            $this->errors[] = new ValidationError('The value can not be lower than %size%.', null, array('%size%' => $this->constraints['minFloatValue']));
            $isValid = false;
        }
        return $isValid;
    }

Usage Example

 /**
  * Tests validating a wrong value.
  *
  * @dataProvider providerForValidateKO
  * @covers \eZ\Publish\Core\FieldType\Validator\FloatValueValidator::validate
  */
 public function testValidateWrongValues($value, $message, $values)
 {
     $validator = new FloatValueValidator();
     $validator->minFloatValue = $this->getMinFloatValue();
     $validator->maxFloatValue = $this->getMaxFloatValue();
     $this->assertFalse($validator->validate(new FloatValue($value)));
     $messages = $validator->getMessage();
     $this->assertCount(1, $messages);
     $this->assertInstanceOf('eZ\\Publish\\SPI\\FieldType\\ValidationError', $messages[0]);
     $this->assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Translation\\Message', $messages[0]->getTranslatableMessage());
     $this->assertEquals($message, $messages[0]->getTranslatableMessage()->message);
     $this->assertEquals($values, $messages[0]->getTranslatableMessage()->values);
 }