Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate the file name in Laravel 5.4

I have a form with three input fields. I want to validate the input value before processing them. Where I want to validate the file name before processing it. I use regular expression and alpha_dash. But I got an error for a valid file name. I want my file name only to contain small letter, numbers, underscore and dashes. How can I check the validation of the file name for my file?

Html

 <form action="create" method="POST"  enctype="multipart/form-data">
          {{csrf_field()}}
      <table cellpadding="2" width="20%"  align="center"
         cellspacing="2">

         <tr>
           <td colspan=2>
           <center><font size=4><b>Add the iteams  please</b></font></center>
           </td>
         </tr>

         <tr>
           <td>Heading</td>
           <td><input type="text" name="heading" id="heading" size="30">
           {!! $errors->first('heading', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td>Image</td>
           <td><input type="file" name="image" id="image" size="40">
           {!! $errors->first('image', '<p class="red">:message</p>') !!}
           </td>
         </tr>

         <tr>
           <td></td>
           <td colspan="2"><input type="submit" name="submit" value="Add Item" /></td>
         </tr>
     </table>
 </form>

controller part

  1. Using Regular expression format:

- I got error message, “ The image format is invalid” .

 public function store(){
    $this->validate(request(),[
          'heading'=>'required',
          'contentbody'=>'required',
           ‘image'=>['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048','regex:/^[a-z0-9-_]+$/' ]

        ]);

}
  1. using Alpa_dash:

- I got error message, “The image may only contain letters, numbers, and dashes” .

public function store(){
 $this->validate(request(),[
           'heading'=>'required',
          'contentbody'=>'required',
          'image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|alpha_dash'
}

Please help, Thank you!

like image 250
Jona Avatar asked Dec 24 '22 13:12

Jona


1 Answers

  1. Create custom validation rule class, for example Filename, and put it at the path app/Rules/Filename.php.
<?php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Filename implements Rule
{
    protected $regex;

    public function __construct(string $regex)
    {
        $this->regex = $regex;
    }

    public function passes($attribute, $value)
    {
        if (!($value instanceof UploadedFile) || !$value->isValid()) {
            return false;
        }

        return preg_match($this->regex, $value->getClientOriginalName()) > 0;
    }

    public function message()
    {
        return 'The :attribute name is invalid.';
    }
}
  1. In your request rules add this custom rule like this:
use App\Rules\Filename;

public function store() {
    $this->validate(request(), [
        'heading' => 'required',
        'contentbody' => 'required',
        'image' => [
            'required',
            'image',
            'mimes:jpeg,png,jpg,gif,svg',
            'max:2048',
            new Filename('/^[a-z0-9-_]+$/'), // This is your custom rule
        ],
    ]);
}
like image 110
Modder Avatar answered Jan 05 '23 02:01

Modder