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

validate() public method

Checks if $value->file has the appropriate size.
public validate ( Value $value ) : boolean
$value eZ\Publish\Core\FieldType\Value
return boolean
    public function validate(BaseValue $value)
    {
        $isValid = true;
        if ($this->constraints['maxFileSize'] !== false && $value->file->size > $this->constraints['maxFileSize']) {
            $this->errors[] = new ValidationError('The file size cannot exceed %size% byte.', 'The file size cannot exceed %size% bytes.', array('%size%' => $this->constraints['maxFileSize']));
            $isValid = false;
        }
        return $isValid;
    }

Usage Example

 /**
  * Tests validating a wrong value.
  *
  * @dataProvider providerForValidateKO
  * @covers \eZ\Publish\Core\FieldType\Validator\FileSizeValidator::validate
  */
 public function testValidateWrongValues($size, $message, $values)
 {
     $this->markTestSkipped('BinaryFile field type does not use this validator anymore.');
     $validator = new FileSizeValidator();
     $validator->maxFileSize = $this->getMaxFileSize();
     $this->assertFalse($validator->validate($this->getBinaryFileValue($size)));
     $messages = $validator->getMessage();
     $this->assertCount(1, $messages);
     $this->assertInstanceOf('eZ\\Publish\\SPI\\FieldType\\ValidationError', $messages[0]);
     $this->assertInstanceOf('eZ\\Publish\\API\\Repository\\Values\\Translation\\Plural', $messages[0]->getTranslatableMessage());
     $this->assertEquals($message[0], $messages[0]->getTranslatableMessage()->singular);
     $this->assertEquals($message[1], $messages[0]->getTranslatableMessage()->plural);
     $this->assertEquals($values, $messages[0]->getTranslatableMessage()->values);
 }