Sirius\Upload\Handler::addRule PHP Method

addRule() public method

Add validation rule (extension|size|width|height|ratio)
public addRule ( string $name, mixed $options = null, string $errorMessageTemplate = null, string $label = null ) : Handler
$name string
$options mixed
$errorMessageTemplate string
$label string
return Handler
    public function addRule($name, $options = null, $errorMessageTemplate = null, $label = null)
    {
        $predefinedRules = array(static::RULE_EXTENSION, static::RULE_IMAGE, static::RULE_SIZE, static::RULE_IMAGE_WIDTH, static::RULE_IMAGE_HEIGHT, static::RULE_IMAGE_RATIO);
        // convert to a name that is known by the default RuleFactory
        if (in_array($name, $predefinedRules)) {
            $name = 'upload' . $name;
        }
        $this->validator->add($name, $options, $errorMessageTemplate, $label);
        return $this;
    }

Usage Example

Ejemplo n.º 1
0
 function index_get()
 {
     $uploadHandler = new UploadHandler($this->directory);
     // set up the validation rules
     $uploadHandler->addRule('extension', ['allowed' => 'jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx'], '{label} should be a valid file (jpg, jpeg, png,pdf,doc,docx)', 'Valid File');
     $uploadHandler->addRule('size', ['max' => '20M'], '{label} should have less than {max}', 'Valid File');
     $uploadHandler->addRule('imageratio', ['ratio' => 1], '{label} should be a sqare image', 'Valid File');
     $files = $this->filesystem->listContents();
     $resource = new Collection($files, function (array $file) {
         return ['name' => ['name' => $file['path'], 'path' => $file['filename']], 'uri' => $this->directory . $file['path'], 'mime' => get_mime_by_extension($file['path'])];
     });
     $data = $this->fractal->createData($resource)->toArray();
     $this->response($data);
 }
All Usage Examples Of Sirius\Upload\Handler::addRule