3

my form have youtube_url field, its not required..
but if user fill this field, i must validate the url using regex..

'youtube_video' => [
    'regex:/(http:\/\/|https:\/\/|)(www.)?(youtu(be\.com|\.be))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/'
],

the regex works well, but its throw error if field is not fill (empty) .
how can validate only when user fill the field.

3
  • 1
    I think the nullable rule before your regex one will solve your problem. Commented Apr 25, 2018 at 15:57
  • @SystemGlitch thanks, its worked... Commented Apr 25, 2018 at 16:02
  • Possible duplicate of Laravel Request Validation Regex without Required Commented Apr 25, 2018 at 16:02

1 Answer 1

5

You shoul use nullable rule for it:

$this->validate($request, [
        'youtube_video' => [
            'regex:/(http:\/\/|https:\/\/|)(www.)?(youtu(be\.com|\.be))\/(video\/|embed\/|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/',
            'nullable'
        ],
    ]);
Sign up to request clarification or add additional context in comments.

1 Comment

The "nullable" validation rule is exactly what I was looking for, thanks!

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.