skip to Main Content

Using the following validation rules:

'somefield' => 'required|min:20',

An input value of "Extra space." will pass min:20 validation.

What is the best way to ignore more than single spaces in the middle of a string when applying the min validation rule so the above input value would fail? Not looking for a client-side solution.

2

Answers


  1. Use method prepareForValidation() in FormRequeet, try:

    use IlluminateSupportStr;
    
    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
    $this->merge([
        'somefield' => Str::squish('    laravel    framework    ');
        // laravel framework
        ]);
    }
    

    References:

    https://laravel.com/docs/9.x/validation#preparing-input-for-validation

    https://laravel.com/docs/9.x/helpers#method-str-squish

    Login or Signup to reply.
  2. You can use this simple validation.

    'somefield' => ['required','min:20','regex:/^S*$/u']
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search