1

i am trying to validate some inputs using this :

$request->validate([
        'prenom' => 'required',
        'nom' => 'required',
        'telephone' => ['required | regex:/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/|digits:10'],
        'password' => 'required |string|min:8|confirmed',
        'confirm_pass' => 'required',
        'email' => 'required|email|max:255|string|unique:users'
            ]);

but it gives me an error:

Method Illuminate\Validation\Validator::validateRequired|Regex does not exist.

please any idea how to fix it ?!

2
  • What version of Laravel are you using? Commented Mar 2, 2021 at 8:56
  • @MhluziBhaka laravel 8 Commented Mar 2, 2021 at 8:58

3 Answers 3

3

The pipe | sign is available in your regular expression pattern so it's conflicting with the separator. When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, change your code to :

'telephone' => array(
      'required',
      'regex:/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/',
      'digits:10'
)
Sign up to request clarification or add additional context in comments.

Comments

0
$validatedData = $request->validate([
        'prenom' => 'required',
        'nom' => 'required',
        'telephone' => ['required | regex:/^(?:(?:(?:\+|00)212[\s]?(?:[\s]?\(0\)[\s]?)?)|0){1}(?:5[\s.-]?[2-3]|6[\s.-]?[13-9]){1}[0-9]{1}(?:[\s.-]?\d{2}){3}$/|digits:10'],
        'password' => 'required |string|min:8|confirmed',
        'confirm_pass' => 'required',
        'email' => 'required|email|max:255|string|unique:users'
            ]);

1 Comment

Please add some explanation to your answer such that others can learn from it - what have you changed, and why?
0
use Validator;

add on top of controller

1 Comment

Please add some explanation to your answer such that others can learn from it - what makes you think that any class Validator exists in the root namespace?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.