Laravel中子字符串的唯一验证


Unique validation of a substring in Laravel

我将filenames + their extensions存储在files表的filename列下。我的问题是,由于只有名称出现在$request对象中,而没有相应的扩展名,如果不首先修改输入数据,我就无法使用唯一验证规则验证文件名。示例:

// . . .
$this->validate($request, [
    //   Suppose the name of uploaded file is 'file'.
    // The below rule will NEVER fail, because in the 
    // database, similar file will be stored as 'file.txt',
    // thus 'file' != 'file.txt'
    'filename' => 'unique:files'
]);
// . . .

有没有一种方法可以忽略数据库中的后缀(扩展名)来验证文件名?

您可以尝试覆盖Request类中的all()方法,并在验证前而不是验证后追加扩展。这将类似于

public function all() {
    $data = parent::all();           // Get all the data in your request
    $data['filename'] .=  '.txt';    // Concatenate the file extension
    return $data;           // DONT FORGET TO RETURN THE CHANGED DATA
}

现在,您的规则将正常工作,因为它将搜索扩展名为的文件提醒:您需要停止在控制器中或您使用的任何位置添加扩展名,否则您将使用filename.txt.txt并返回到原点1。

就我个人而言,每当我喜欢all()方法时,我都会发现它有点乱,所以我有以下特点

trait SanitizeRequest {
    protected $sanitized = false;
    public function all() {
        return $this->sanitize(parent::all());
    }
    protected function sanitize(array $inputs) {
        if ($this->sanitized) return $inputs;
        foreach ($inputs as $field => $value) {
            if (method_exists($this, $field))
                $inputs[$field] = $this->$field($value);
        }
        $this->replace($inputs);
        $this->sanitized = true;
        return $inputs;
    }
}

这种特性允许我在验证前随时使用字段名编写一个自定义方法。使用这种方法可以让你有一种类似的方法

class YourRequest extends Request {
    use SanitizeRequest;
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }
    ...
   protected function filename($value) {
       return $value . '.txt';
   }
}