0

I use regex validation in Laravel. But error occures. I want to control, it is image or not. Image extension can be .jpg, .png or only .jp2. Here is my implementation :

protected $rules = [
      'preview_path'  => 'required|regex:[^.*\.(jpg|png)$]',
      'slide_path'    => 'required|regex:[^.*\.(jp2)$]'
];

Error :

preg_match(): No ending matching delimiter ']' found



EDIT

I am not send a file, i send just string path as a json. So i can not use mime type validation.

3
  • why dont you use mimes for this ? 'preview_path' => 'required|mimes:jpg,png' 'slide_path'=>'required|mimes:jp2' Commented May 17, 2017 at 6:48
  • @Rodrane it is REST API part, i send path as a json (string) not file. Mimes doesn't work. Commented May 17, 2017 at 6:52
  • you are not asking your question clearly then obviously they are not images Commented May 17, 2017 at 6:54

2 Answers 2

1

This one should work

protected $rules = [ 
    'preview_path' => ['required|regex:/^.*\.(jpg|png)$/'], 
    'slide_path' => ['required|regex:/^.*\.(jp2)$/'] 
];
Sign up to request clarification or add additional context in comments.

Comments

1

you can use this for extension validation which is better than regex https://laravel.com/docs/5.4/validation#rule-mimes

Comments

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.