验证后端Octobercms

时间:2016-05-03 14:35:48

标签: laravel validation file-upload octobercms

即时创建一个插件,其中我在后端使用fileupload字段类型。我有一个realation $ attachOne,我需要验证图像尺寸(高度和宽度),这是一种方法吗?

2 个答案:

答案 0 :(得分:4)

您需要向模型添加验证逻辑。在关系被称为somerelation的地方,在模型类中定义这样的方法:

public function beforeValidate()
{
    $file = $this->somerelation()->withDeferred($this->sessionKey)->first();
    $filename = $file->getLocalPath();
    list($width, $height) = getimagesize($filename);

    if ($width < 800) {
        throw new ValidationException(['somerelation' => 'Width must be greater than 800']);
    }
}

要使此方法覆盖起作用,请确保模型已使用October\Rain\Database\Traits\Validation特征。因为它只发生在后端,快速检查App::runningInBackend()应该可以解决问题。

答案 1 :(得分:0)

使用它会给出图像的宽度和高度。

list($width, $height) = getimagesize($filename);