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
示例#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);
 }
示例#2
0
 public function register(Silex\Application $app)
 {
     // This exposes the main upload object as a service
     $app['upload'] = $app->share(function ($app) {
         $allowedExensions = $app['config']->get('general/accept_file_types');
         $uploadHandler = new UploadHandler($app['upload.container']);
         $uploadHandler->setPrefix($app['upload.prefix']);
         $uploadHandler->setOverwrite($app['upload.overwrite']);
         $uploadHandler->addRule('extension', array('allowed' => $allowedExensions));
         return $uploadHandler;
     });
     // This exposes the file container as a configurabole object please refer to:
     // Sirius\Upload\Container\ContainerInterface
     // Any compatible file handler can be used.
     $app['upload.container'] = $app->share(function ($app) {
         $base = $app['resources']->getPath($app['upload.namespace']);
         if (!is_writable($base)) {
             throw new \RuntimeException("Unable to write to upload destination. Check permissions on {$base}", 1);
         }
         $container = new FlysystemContainer($app['filesystem']->getManager($app['upload.namespace']));
         return $container;
     });
     // This allows multiple upload locations, all prefixed with a namespace. The default is /files
     // Note, if you want to provide an alternative namespace, you must set a path on the $app['resources']
     // service
     $app['upload.namespace'] = 'files';
     // This gets prepended to all file saves, can be reset to "" or add your own closure for more complex ones.
     $app['upload.prefix'] = date('Y-m') . '/';
     $app['upload.overwrite'] = false;
 }
 /**
  * Handle uploaded files
  *
  * @param Request $request
  * @return Response
  */
 public function filesUpload(Request $request)
 {
     $path = $request->input('path');
     $field = $request->input('field');
     $allowed = Mediabrowser::allowedExtensions($field);
     $uploadHandler = new UploadHandler(public_path($path));
     $uploadHandler->addRule('extension', ['allowed' => $allowed]);
     $result = $uploadHandler->process($_FILES['files'], array(UploadHandler::OPTION_AUTOCONFIRM => false));
     if ($result->isValid()) {
         $result->confirm();
         return Response::json([]);
     } else {
         $result->clear();
         return Response::json($result->getMessages());
     }
 }
示例#4
0
文件: File.php 项目: siriusphp/input
 /**
  * Attaches the upload handlers to the input object
  *
  * @param InputFilter $inputFilter
  */
 protected function prepareUploadHandlers(InputFilter $inputFilter)
 {
     $uploadValidator = new \Sirius\Validation\ValueValidator($inputFilter->getValidator()->getRuleFactory(), $inputFilter->getValidator()->getErroMessagePrototype(), $this->getLabel());
     // create the upload handler
     $uploadHandler = new Handler($this->getUploadContainer(), $this->getUploadOptions(), $uploadValidator);
     if (is_array($this->getUploadRules())) {
         foreach ($this->getUploadRules() as $rule) {
             if (!is_array($rule)) {
                 $rule = array($rule);
             }
             $name = $rule[0];
             $options = isset($rule[1]) ? $rule[1] : null;
             $message = isset($rule[2]) ? $rule[2] : null;
             $label = $this->getLabel();
             $uploadHandler->addRule($name, $options, $message, $label);
         }
     }
     $inputFilter->setUploadHandler($inputFilter->getUploadPrefix() . $this->getName(), $uploadHandler);
 }
示例#5
0
 public function register(Application $app)
 {
     // This exposes the main upload object as a service
     $app['upload'] = $app->share(function () use($app) {
         $allowedExensions = $app['config']->get('general/accept_file_types');
         $uploadHandler = new UploadHandler($app['upload.container']);
         $uploadHandler->setPrefix($app['upload.prefix']);
         $uploadHandler->setOverwrite($app['upload.overwrite']);
         $uploadHandler->addRule('extension', ['allowed' => $allowedExensions]);
         $pattern = $app['config']->get('general/upload/pattern', '[^A-Za-z0-9\\.]+');
         $replacement = $app['config']->get('general/upload/replacement', '-');
         $lowercase = $app['config']->get('general/upload/lowercase', true);
         $uploadHandler->setSanitizerCallback(function ($filename) use($pattern, $replacement, $lowercase) {
             if ($lowercase) {
                 return preg_replace("/{$pattern}/", $replacement, strtolower($filename));
             }
             return preg_replace("/{$pattern}/", $replacement, $filename);
         });
         return $uploadHandler;
     });
     // This exposes the file container as a configurable object please refer to:
     // Sirius\Upload\Container\ContainerInterface
     // Any compatible file handler can be used.
     $app['upload.container'] = $app->share(function () use($app) {
         $base = $app['resources']->getPath($app['upload.namespace']);
         if (!is_writable($base)) {
             throw new \RuntimeException("Unable to write to upload destination. Check permissions on {$base}", 1);
         }
         $container = new UploadContainer($app['filesystem']->getFilesystem($app['upload.namespace']));
         return $container;
     });
     // This allows multiple upload locations, all prefixed with a namespace. The default is /files
     // Note, if you want to provide an alternative namespace, you must set a path on the $app['resources']
     // service
     $app['upload.namespace'] = 'files';
     // This gets prepended to all file saves, can be reset to "" or add your own closure for more complex ones.
     $app['upload.prefix'] = date('Y-m') . '/';
     $app['upload.overwrite'] = false;
 }