Laravel 验证 space 字符总是失败

Laravel validating space character always failed

正在尝试验证输入框中是否只有空格。

这是我的验证:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use App\Rules\allAreSpaces;

class StoreUser 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 [
            'email' => 'required|email|max:64',
            'phone' => 'bail|required|numeric|phone_min|phone_max|unique:users|not_in:0',
            'password' => [new allAreSpaces, 'required', 'min:8', 'max:16', 'confirmed'],
            'password_confirmation' => 'required',
        ];

    }

    public function messages()
    {
        return [
            'email.required'     => 'Email address cannot be empty',
            'email.email'     => 'Enter a valid Email address',
            'email.max'     => 'Email address cannot exceed 64 characters',
            'email.unique'     => 'Email already exists',


            'phone.required'     => 'Mobile number cannot be empty',
            'phone.numeric'     => 'Mobile number has to be numeric',
            'phone.phone_min'     => 'Mobile number should contain a minimum 5 characters',
            'phone.phone_max'     => 'Mobile number cannot exceed 11 characters',
            'phone.unique'     => 'Mobile number already exists',
            'phone.not_in'     => 'Enter a valid mobile number ',


            'password.required'     => 'Password cannot be empty',
            'password.min'     => 'Password should contain minimum 8 characters',
            'password.max'     => 'Password cannot exceed 16 characters',
            'password.confirmed'     => 'Password mismatch.Retry',

            'password_confirmation.required'     => 'Confirm Password cannot be empty',
        ];
    }


    public function withValidator(Validator $validator)
    {


        $email = $this->request->get( 'email' );  // Start with 

        $user = \App\User::Where('email', $email)->first();


        if($user && $user->activated == 0){

            $row = \App\User::find($user->id);
            $row->delete();

        }else{

            $validator->sometimes('email', 'unique:users', function ($input) {
                return true;
            });
        }


    }

}

以下是我创建的自定义规则对象:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class allAreSpaces implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {

        if(strlen($value) > 0){
            if (strlen(trim($value)) == 0){
                return false;   
            }
        }
        return true;

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Password cannot contain space character alone';
    }
}

最初返回,'Password cannot contain space character alone'。但是在我输入一些空格然后提交后它显示 'Password cannot be empty'。但是如果键入的字符只是空格,我想要 'Password cannot contain space character alone'。

尽管我确实使用了以下代码,但它没有显示输入是否有空格,而是显示 'Password cannot be empty'.

 public function passes($attribute, $value)
    {
        return false;

    }

我想知道这是为什么?

您是否检查过全局中间件,即在您的 HTTP 内核中设置的应用程序中所有路由的 运行?这些可能会起作用:

"By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. These middleware will automatically trim all incoming string fields on the request, as well as convert any empty string fields to null. This allows you to not have to worry about these normalization concerns in your routes and controllers.

If you would like to disable this behavior, you may remove the two middleware from your application's middleware stack by removing them from the $middleware property of your App\Http\Kernel class."

Laravel 5.5 Docs - Requests - Input trimming and normalization

这也可能是相关的,涉及由于这些中间件而可能发生的一些复杂情况:

"By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid. "

Laravel 5.5 Docs - Validation - A note on optional fields