Laravel Validation İssue

Viewed 49

If the user adds a picture, I want to cancel the min part that requires the story to be written. I want the 'story' => min part to be canceled if the user adds a picture. That is, I want the minimum character input to be canceled when the image is added. If the picture is deleted or the picture format is not suitable (size, extension, etc.), I want the minimum character input to come back.

i tried this but i couldn't

if($request->hasFile('photo')) {
            $required['story'] = 'nullable';
        }
public function store(Request $request)
    {
        
        if(Settings::find('active_upload')->value == 0){
            toastr()->warning(__('main.new_entries_paused'));
            return redirect('/');
        }
        
        Validator::make($request->all(), [
            'title' => 'required|string|max:255',
            'story' => 'required|string|min:'.Settings::find('minimum_characters')->value.'|max:'.Settings::find('maximum_characters')->value,
            'tags' => 'nullable',
            'category_id' => 'required',
            'genders_id' => 'required',
            'age' => 'required',
            'photo' => 'nullable|image|mimes:jpeg,png,jpg|max:2048'
        ],[
            'title.required' => 'Bir Başlık Giriniz.',
            'title.max' => 'Başlık 255 Karakterden Uzun Olamaz.',

            'story.required' => 'Boş bırakılmamalıdır.',
            'story.min' => 'Girilen Metin '.Settings::find('minimum_characters')->value.' Karakterden Fazla Olmalıdır.',
            'story.max' => 'Girilen Metin ' .Settings::find('maximum_characters')->value.' Karakterden Az Olmalıdır.',

            'category_id.required' => 'Kategori Seçmeniz Gerekiyor.',

            'photo.image' => 'Lütfen Resim Dosyası Yükleyiniz.',
            'photo.mimes' => 'Dosya Biçimi jpg,png veya jpeg olmalıdır.',
            
            
        
        ])->validateWithBag('write');

        if($request->hasFile('photo')) {
            $required['story'] = 'nullable';
        }
1 Answers

When you use the Validator::Make method, this already validates the request, so when the code reaches the lines

if($request->hasFile('photo')) {
    $required['story'] = 'nullable';
}

the validation has already been done.

You could set the validation rules' string before the Validator::Make method, something like this:

$storyRules = '';

if (!$request->hasFile('photo')) {
    $storyRules .= 'required|string';
    $storyRules .= '|min:' . Settings::find('minimum_characters')->value;
} else {
   $storyRules .= 'nullable|string';
}

$storyRules .= '|max:' . Settings::find('maximum_characters')->value;

Validator::make($request->all(), [
    'title' => 'required|string|max:255',
    'story' => $storyRules,
    # other validations here
],
[
    # custom messages here
]
)->validateWithBag('write');

Basically, we set the rules before the validation occurs, verifying if the user has not added a photo, so when the user adds a photo the validation of min characters will not be added to the $storyRules variable, consecutively it will not be validated.

Related