Laravel preg_match(): No ending delimiter '/' found
Asked Answered
E

2

58

Im working on Laravel 4.2. Im trying to use Validator to validate a name field with regex, here is my rule below:

 public static $rules_save = [

        'class_subjects' => 'required|regex:/[0-9]([0-9]|-(?!-))+/'
    ];

But as soon as I call the rule to be validated an error is thrown, see below:

preg_match(): No ending delimiter '/' found
Ensemble answered 27/9, 2015 at 16:51 Comment(0)
A
187

Since your regex has a pipe in it, you have to use an array:

public static $rules_save = [
    'class_subjects' => ['required', 'regex:/[0-9]([0-9]|-(?!-))+/'],
];

From the docs:

When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.

Aleksandr answered 27/9, 2015 at 16:57 Comment(4)
Thank you it works. I read this earlier and just thought that I wont need it.Ensemble
haha why did you think that? It explicitly says that if you have a pipe...Amphibology
Let me explain directly. You just need to add [ ] before and after the validation rule if using pipe characterLollard
i am using an array but i get the same error during testing. it passes the 1st test but second test it fails with this messagePollaiuolo
L
0

Laravel 6.x, 2nd line can work when regex with pipe, but not first line. It verify 'reference' field of 'companies' table: a) required, b) 'reference' must BV000 or A000 format, c) no duplicate.

$this->validate($request,[
// 'records.reference' => 'required|regex:/^[A-Z][0-9]{3}$/|unique:companies,reference,'.$request->records['id'],
'records.reference' => ["required","regex:/^(BV|[A-Z]?)[0-9]{3}$/","unique:companies,reference,".$request->records['id']],
]
Leaseholder answered 20/10, 2022 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.