【问题标题】:validation error regarding the validation file in laravel关于 laravel 中的验证文件的验证错误
【发布时间】:2020-08-10 14:27:04
【问题描述】:

我正在调用验证请求,但收到此错误。

方法 Illuminate\Validation\Validator::validateFile,不存在。

我的用户请求

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Http\Controllers\ProfileController;

class UserRequest 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 [
            'name'=> 'string|required|max:255',
            'username'=> 'string|required|max:255|alpha_dash',
            'avatar'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
            'email'=> 'email|required|string',
            'password'=>'required|min:10|string|confirmed',
            'background'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
            'description'=>'string|max:255'
        ];
    }
    public function messages()
    {
        return [
            'name.required' => 'A name is required',
            'username.required' => 'A username is required',
            'email.required' => 'An email is required',
            'password.required' => 'A password is required',
            'description' => 'A description should be string',

        ];
    }
}

我的控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserRequest;
use Illuminate\Http\File ;
use App\User;
use Hash;
use Session;

public function update(User $user,UserRequest $request)
    {
        
        $user->update(['name'=>request('name'),'username'=>request('username'),'email'=>request('email'),'password'=> Hash::make(request('password')),'avatar'=>$path,'background'=>$background,'description'=>request('description')]);

        Session::flash('success', 'You have successfully updated a post!');
       return redirect($user->path())->with(['message'=>'Profile updated']);
    }

我的刀

@extends('layouts.app')
@section('content')

    <h1 class="text-gray-700 text-2xl font-bold text-center">Edit Profile</h1>
    <div class="mt-5 mb-6">
        <img src="{{$user->avatar()}}" alt=""
             class="rounded-full mr-2  bottom-0 transform  "
             width="150px" style="transform: translateX(16.5rem);" >
    </div>
    
    <form method="POST"  action="{{$user->path() }}" enctype="multipart/form-data">
        @csrf
        @method('PATCH')
        <div id="flip" class="p-5 bg-gray-200">Basic Info</div>
        <div id="panel">
            <div class="mb-6 mt-6">
                <label for="avatar">Select Profile to Upload</label>
                <input id="avatar" type="file" class="@error('avatar') is-invalid @enderror" name="avatar">
                @error('avatar')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="name">Name</label>
                <input id="name" type="text" class="@error('name') is-invalid @enderror border border-gray-400 p-2 w-full" name="name" value="{{$user->name}}">
                @error('name')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="username">Username</label>
                <input id="username" type="text" name="username" class="@error('username') is-invalid @enderror border border-gray-400 p-2 w-full" value="{{$user->username}}">
                @error('username')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="email">Email</label>
                <input id="email" type="email" name="email" class="@error('email') is-invalid @enderror border border-gray-400 p-2 w-full" value="{{$user->email}}">
                @error('email')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="password">Password</label>
                <input id="password" type="password" name="password" class="@error('password') is-invalid @enderror border border-gray-400 p-2 w-full" >
                @error('password')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="password-confirm">Confirm Password</label>
                <input id="password-confirm" type="password" name="password-confirm" class="@error('password-confirm') is-invalid @enderror border border-gray-400 p-2 w-full" >
                @error('password-confirm')
                <div class="text-red-800">{{ $message }}</div>
                @enderror
            </div>



        </div>
        <div id="main" class="p-5 bg-gray-200">Background</div>
        <div id="child">
            <div class="mb-6 mt-6">
                <label for="background">Select Background image to Upload</label>
                <input id="background" type="file" class="@error('background') is-invalid @enderror" name="background">
                @error('background')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
            <div class="mb-6">
                <label for="description">Description</label>
                <input id="description" type="text"  name="description" class="@error('description') is-invalid @enderror border border-gray-400 p-2 w-full" value="{{$user->description}}">
                @error('description')
                <div class="alert alert-danger">{{ $message }}</div>
                @enderror
            </div>
        </div>
        <div class="mt-6">
            <button type="submit" class="bg-blue-500 text-white rounded py-2 px-4 hover:bg-blue-800'">Submit</button>
        </div>
    </form>



    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#flip").click(function(){
                $("#panel").slideToggle("slow");
            });
            $("#main").click(function(){
                $("#child").slideToggle("slow");
            });
        });
    </script>
@endsection


我已经尝试了堆栈溢出可用的解决方案,但仍然没有一个有效 有谁知道我为什么会收到这个错误? 任何帮助将不胜感激。

【问题讨论】:

  • file,| 去掉'background'=&gt;'file,|mimes:jpeg,png,gif,jpg|max:2048'这里的逗号
  • 现在我收到此错误**文件“C:\xampp\tmp\php2028.tmp”不存在**
  • 更改为 'avatar'=&gt;'image|mimes:jpeg,png,gif,jpg|max:2048', 并确保在表单中添加了 enctype="multipart/form-data"
  • 我尝试将 UserRequest 更改为 Request ,它工作正常,我不知道为什么它在 UserRequest 上给出错误
  • 这是 ``` 'avatar'=>'image|mimes:jpeg,png,gif,jpg|max:2048', ``` 中的问题,但现在当我添加确认在密码中,即使我输入错误的密码也不会出错

标签: laravel


【解决方案1】:

删除这些验证行中的逗号。

  1. '头像'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',
  2. 'background'=>'file,|mimes:jpeg,png,gif,jpg|max:2048',

【讨论】:

  • 现在我收到此错误文件“C:\xampp\tmp\php2028.tmp”不存在
  • 关于图像的问题现已解决。这是 ``` 'avatar'=>'image|mimes:jpeg,png,gif,jpg|max:2048', ``` 中的问题,但是现在当我在密码中添加确认时,它没有给出即使我输入错误的密码也会出错
  • 确保您有两个密码输入,第一个字段有名称(password),第二个字段有名称(password_confirmation)。
  • 检查密码输入的名称。必须是(密码,密码确认)。
  • 我照你说的做了,但还是不行 ``` 'password'=>'required|min:10|string|confirmed', ``` &lt;input id="password" type="password" name="password" class="@error('password') is-invalid @enderror border border-gray-400 p-2 w-full" &gt; &lt;input id="password_confirmation" type="password" name="password_confirmation" class="border border-gray-400 p-2 w-full" &gt;
【解决方案2】:

在messages数组中添加password.confirmed。

'password.confirmed' => 'YOUR ERROR MESSAGE',

【讨论】:

    猜你喜欢
    • 2017-06-13
    • 2018-09-05
    • 2013-10-31
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多