首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Phalcon 4 messageEmpty文件验证

Phalcon 4 messageEmpty文件验证
EN

Stack Overflow用户
提问于 2020-03-24 17:12:13
回答 1查看 297关注 0票数 0

有人知道如何在Phalcon 4上修改文件验证器中的messageEmpty吗?我试过使用与Phalcon 3相同的方法,但它不起作用。

,这是我的Validator:

代码语言:javascript
运行
复制
$field->addValidator(new \Phalcon\Validation\Validator\File([
    "allowedTypes" => [
        "text/plain"
    ],
    "messageEmpty" => 'Message empty',
    "messageType" => _('Please upload a txt file')
]));

$field->setLabel(_('Upload List'));

这是运行$ This ->form->isValid($_FILES)后收到的消息

代码语言:javascript
运行
复制
object(Phalcon\Messages\Messages)#192 (2) {
  ["position":protected]=>
  int(0)
  ["messages":protected]=>
  array(1) {
    [0]=>
    object(Phalcon\Messages\Message)#191 (5) {
      ["code":protected]=>
      int(0)
      ["field":protected]=>
      string(8) "robinson"
      ["message":protected]=>
      string(32) "Field robinson must not be empty"
      ["type":protected]=>
      string(42) "Phalcon\Validation\Validator\File\MimeType"
      ["metaData":protected]=>
      array(0) {
      }
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-25 14:43:17

最后,我创建了一个自定义验证器:

代码语言:javascript
运行
复制
/**
 * Class FileValidator 
 * @package Api\Forms\Core\Validators\File
 * @author Iulian Gafiu <julian@clickmedia.es>
 */
class FileValidator extends AbstractValidator {

    /**
     * @var array
     */
    protected $params;

    /**
     * IsEmpty constructor.
     * @param array $options
     */
    public function __construct(array $options = []){
        $this->params = $options;
        parent::__construct($options);
    }

    /**
     * @inheritDoc
     */
    public function validate(\Phalcon\Validation $validation, $field): bool{

        if($_FILES[$field]['size'] <= 0){

            if(!isset($this->params['messageEmpty']) || empty($this->params['messageEmpty'])){
                $this->params['messageEmpty'] = sprintf(_('%s cannot be empty'), $field);
            }

            $validation->appendMessage(
                new Message($this->params['messageEmpty'], $field, 'FileEmpty')
            );

            return false;

        }else{


            if(isset($this->params['maxSize']) && !empty($this->params['maxSize'])){
                if($this->human2byte(strtolower($this->params['maxSize'])) < $_FILES[$field]['size']){
                    $validation->appendMessage(
                        new Message($this->params['messageMaxSize'], $field, 'MaxSize')
                    );
                    return false;
                }
            }

            if(isset($this->params['allowedTypes']) && !empty($this->params['allowedTypes'])){
                if(!in_array($_FILES[$field]['type'], $this->params['allowedTypes'])){
                    $validation->appendMessage(
                        new Message($this->params['messageType'], $field, 'allowedTypes')
                    );
                    return false;
                }
            }


        }

        return true;
    }

    /**
     * @param $value
     * @return string|string[]|null
     * @author Eugene Kuzmenko
     * @see https://stackoverflow.com/questions/11807115/php-convert-kb-mb-gb-tb-etc-to-bytes
     */
    public function human2byte($value) {
        return preg_replace_callback('/^\s*(\d+)\s*(?:([kmgt]?)b?)?\s*$/i', function ($m) {
            switch (strtolower($m[2])) {
                case 't': $m[1] *= 1024;
                case 'g': $m[1] *= 1024;
                case 'm': $m[1] *= 1024;
                case 'k': $m[1] *= 1024;
            }
            return $m[1];
        }, $value);
    }

}

然后我在表格上用它:

代码语言:javascript
运行
复制
$field->addValidator(new FileValidator([
    'maxSize' => '112M',
    'allowedTypes' => [
        "text/plain"
    ],
    'messageEmpty' => _('Please upload a file'),
    'messageMaxSize' => _('File size exceeds the max permitted'),
     'messageType' => _('Please upload a formated txt file')
]));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60835959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档