【问题标题】:Laravel Validation on storage path存储路径上的 Laravel 验证
【发布时间】:2021-04-02 03:44:35
【问题描述】:

以 Laravel 验证为例:

$validatedData = $request->validate([
    'title' => ['required', 'unique:posts', 'max:255'],
    'body' => ['required'],
]);

我想添加路径是否存在于存储中。

Storage::exists('/path/to/your/directory');

基于此,我想做类似的事情:

 $validatedData = $request->validate([
        'title' => ['required', 'unique:posts', 'max:255'],
        'body' => ['required'],
        'path' => ['storage_file_exists_or_not']
    ]);

如何实现。谢谢。

【问题讨论】:

标签: laravel validation


【解决方案1】:

您可以使用创建自定义规则

php artisan make:rule StorageFileExists

并在 pass 函数中指定你的条件并写下你的信息。

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Storage;

class StorageFileExists implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return (Storage::exists($value)) ? false : true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'File is present.';
    }
}

并像这样编写验证规则


$request->validate([
    'path'      =>  new StorageFileExists(), // your Rulename
]);

【讨论】:

    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 2017-06-28
    • 2015-11-18
    • 1970-01-01
    • 2021-12-31
    • 2017-02-11
    • 2014-03-05
    • 1970-01-01
    相关资源
    最近更新 更多