【问题标题】:How To create Different Validation Rules in store and updata, in Form Request Laravel如何在存储和更新中创建不同的验证规则,在表单请求 Laravel
【发布时间】:2021-04-09 09:13:10
【问题描述】:

在 laravel 中使用请求表单,所以我想在存储和更新期间进行不同的验证,当运行存储方法时,照片字段是必需的,但在更新中我不想对照片提供任何验证领域,有人可以帮助我吗?

这是我的验证规则

class GalleryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'judul'=>'required|max:255',
            'photo'=>'sometimes|required|image|mimes:jpg,jpeg,png,gif,tif,svg|max:6000',
        ];
    }
}

本质上我只是想使用表单请求在 laravel 中的 store 和 update 之间进行不同的验证

【问题讨论】:

  • 你可以检查请求的HTTP方法是什么,并据此决定使用哪组规则

标签: javascript php laravel laravel-formrequest


【解决方案1】:

我假设 id 在插入/更新时都存在(插入时 id=0,更新时>0),因此基于它您可以在规则函数中执行类似的操作:

public function rules()
{
    if($this->inputs['id']<=0)
    {
            return [
               'judul'=>'required|max:255',
          'photo'=>'sometimes|required|image|mimes:jpg,jpeg,png,gif,tif,svg|max:6000',
             ];
    }else{
            return [
               'judul'=>'required|max:255'];
      }
}

【讨论】:

    猜你喜欢
    • 2020-07-17
    • 2017-06-13
    • 2018-05-18
    • 1970-01-01
    • 2017-12-04
    • 2020-08-15
    • 2015-05-01
    • 2020-11-28
    • 2017-12-06
    相关资源
    最近更新 更多