0

How can I validate something else after the regular validation in a form-request? I need to verify that a folder does exists or not based on the name given in a input.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateFolder extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return (auth()->check() && auth()->user()->can('create folders'));
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|between:1,64|string'
        ];
    }
}

I want to use the same name when I validate that the folder exists or not after the regular validation. The docs didn't specify anything useful as I could see.

2
  • Can you specify the location where you want to check if folder exists or not ? Commented Jul 27, 2019 at 14:10
  • Sure, I want to check if the folder exists or not in: public/files/ Commented Jul 27, 2019 at 14:11

2 Answers 2

1

You can use custom rule as a closure, so other rules will be same.

return [
    'name' => ['required','between:1,64','string',function ($attribute, $value, $fail) {
        if (file_exists(public_path('files/').$value)) {
            $fail(':attribute directory already exists !');
        }
    }]
]

I hope you understand.

Sign up to request clarification or add additional context in comments.

1 Comment

@Kaizokupuffball Glad to know it works. you can see customization of rules over here, laravel.com/docs/5.8/validation#custom-validation-rules
0

Laravel has a mechanism to write a custom rule for validation. Please have a look at https://laravel.com/docs/5.8/validation#custom-validation-rules

Furthermore, I would suggest using a Storage object to check whether a file exists or not will be a more convenient and robust solution. One can refer to the official documentation at https://laravel.com/docs/5.5/filesystem#retrieving-files

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreateFolder extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return (auth()->check() && auth()->user()->can('create folders'));
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => ['required', 
                       'between:1,64', 
                       'string',
                       function ($attribute, $value, $fail) {
                         if (!Storage::disk('local')->exists('file.jpg')) {
                           $fail($attribute.' does not exist.');
                         }
                       }, 
                      ];
       ]
    }
}

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.