If you specify regex in Ravel validation, you will not be able to submit blanks in the input form.

Asked 2 years ago, Updated 2 years ago, 38 views

I'm using Ravel to create an input form.
I would like to use regular expressions in the validation rules and specify 4 digits or a blank to check the input, but when I specify regex, the blank gets stuck in the validation.

For example,

$rules=[
    'value1' = > ['regex:/^\d{4}$/',
    'value2' = > ['regex:/^(\d{4})?$/']
    'value3' = > ['regex:/^(\d{4}|\s*)$/',
    'value4' = > ['regex:/^(\d{4}|\n*)$/',
    'value5' = > ['regex:/^(\d{4}|*)$/' ]
];

I have tried this method, but if I submit it blank, it will get stuck in validation.
I've tried other possible methods, but I can't.
When I set regex to begin with, I think the blanks are also disabled.

'value'=>['regex://\s*/']

By the way, take out \d and use a method like ↑.

'value'=>['regex:/.*/']

I also tried ↑, but both of them said it was a full-width space, but it didn't work if it was a half-width space or a blank space.

How should I set it up?

php laravel

2022-09-30 19:14

1 Answers

I was able to set it up using nullable

'value'=>['nullable', 'regex:/^\d{4}$/',

Configure one validation instead of adding it to the regular expression.

When using |(pipe) within regex, |(pipe) cannot be used to separate variations, so it is not necessary, so

'value'=>'nullable|regex:/^\d{4}$/',

It's done now.
Thank you.


2022-09-30 19:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.