luddinus
1277
6
General

Regex validation messages

Posted 2 years ago by luddinus

Hi,

Imagine I have some regex that checks the username. It accepts only letters and one space or one dot.

e.g: "luddinus", "ludd.inus", "ludd inus".

Suppose I enter a name with two dots, so I would like the message to be "You can enter only one dot."

In a FormRequest class, I can set the messages, but only for the regex.

public function rules()
{
   return [
      'username' => 'required|regex:/someregexhere/';
   ];
}

public function messages()
{
   return [
      'username.regex' => 'Username is not valid' // Always be this message...
   ];
}

Is there an easy way to achieve this?

Thx.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.