duan2891 2017-12-02 05:27
浏览 141
已采纳

使用自定义验证规则的Laravel表单请求验证无法查找所有空格字符

This is my register controllers, register method:

public function register(StoreUser $request)
    {
            $user = $this->create($request->all());

            $user = User::findOrFail($user->id);

            Mail::to($user->email)->queue(new EmailVerification($user));

            return view('auth.verify-email');

    }

Here is the StoreUser Request:

<?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;
            });
        }


    }

}

Here is that custom Rule named 'allAreSpaces':

<?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';
    }
}

What i want is if user type only the spaces in all password field, i want to throw the 'Password cannot contain space character alone' message. It is working when user didn't type anything. if they typed space its not working.

how can i achieve this?

is there any other easy way to show error message if all are spaces?

  • 写回答

1条回答 默认 最新

  • dqoeghe9452 2018-03-21 05:09
    关注

    @foo.ar,
    In App\Http\Kernel.php, you can see TrimStrings middleware is applied. it will trim all white spaces. if u enter only spaces in any input, it will trim those spaces and u will end up with no values in that input.

    so if u want to validate spaces, u have to add the below code in

    \App\Http\Middleware\TrimStrings.php

    <?php
    
    namespace App\Http\Middleware;
    
    use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
    
    class TrimStrings extends Middleware
    {
        /**
         * The names of the attributes that should not be trimmed.
         *
         * @var array
         */
        protected $except = [
            'password',
            'password_confirmation',
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog