0

Здравствуйте подскажите как можно подготовить и добавить массив files к массиву rules? чтобы при не удачной валидации на image64 возвращалось вместе с другими данными обратно на форму заполнения? Как правильно обработать в данном случае $imagesBase64 созданным валидатором который проверяет на image64:jpeg,jpg,png

реквест введите сюда описание изображения

Валидатор

    public function rules(CategoryRepository $categoryRepository): array
    {
        $categoryIds = $this->request->get('categories');
        $items = $this->prepareAttributesForValidations($categoryIds, $categoryRepository);
        $imagesBase64 = $this->request->get('files');

        if ($imagesBase64) {
            foreach ($imagesBase64 as $imageBase64) {
                $validation = Validator::make(['image' => $imageBase64 ], [
                    'image' => 'image64:jpeg,jpg,png']
                );

                //  dd($validation->fails());
            }
        }

        return array_merge([
            'categories' => 'required|array',
            'title' => 'required|string',
            'price' => 'required|string',
            'content' => 'required|string',
            'regionId' => 'required|string',
            'street' => 'required|string',
        ], $items);
    }

    private function prepareAttributesForValidations(array $categoryIds, CategoryRepository $categoryRepository): array
    {
        $items = [];

        if ($categoryIds) {
            $category = $categoryRepository->getSelectedCategory($categoryIds);

            foreach ($category->allAttributes() as $attribute) {
                $rules = [
                    $attribute->required ? 'required' : 'nullable',
                ];

                if ($attribute->isInteger()) {
                    $rules[] = 'integer';
                } elseif ($attribute->isFloat()) {
                    $rules[] = 'numeric';
                } else {
                    $rules[] = 'string';
                    $rules[] = 'max:255';
                }
                if ($attribute->isSelect()) {
                    $rules[] = Rule::in($attribute->variants);
                }

                $items['attributes.' . $attribute->id] = $rules;
            }
        }

        return $items;
    }

4
  • 1
    Можно сделать 1 валидацию для массива, ну нужно каждый эл-т прогонять по ней. Добавьте правило своё
    – InDevX
    19 июн 2020 в 8:03
  • если не сложно можно пример примерный?) чтоб понять как это должно выглядеть
    – Vlad
    19 июн 2020 в 10:19
  • 1
    Как-то так
    – InDevX
    19 июн 2020 в 10:32
  • спасибо! огромное за помощь!
    – Vlad
    24 июн 2020 в 18:04

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.