robsontenorio / mary

Laravel Blade UI Components for Livewire 3
https://mary-ui.com
Other
904 stars 108 forks source link

Choises bug #526

Closed cooffeeRequired closed 3 weeks ago

cooffeeRequired commented 1 month ago

maryUI version

1.34.2

daisyUI version

4.12.10

Livewire version

3.5.2

What browsers are affected?

Firefox, Chrome, Safari, Microsoft Edge

What happened?

When i submit an form the choises stop working at all.

ERROR image

PHP

    #[LiveComponent] public function create_recipe(): void
    {
        try {
            $this->recipe['belongs_to'] = Auth::user()->toid;
            $ingredients = array();

            foreach ($this->recipe['ingredients'] as $id) {
                $ingredient = Ingredient::find($id);
                if ($ingredient) {
                    $ingredients[] = ["$ingredient->code" => $this->ingredients_percents[$id]];
                }
            }

            $this->recipe['ingredients'] = $ingredients;

            $this->recipe['max'] = array_sum(array_map(function($ingredient) {
                return array_values($ingredient)[0];
            }, $this->recipe['ingredients']));

            $validator = Validator::make($this->recipe, [
                'name' => 'required|unique:recipes',
                'code' => 'required|string|unique:recipes',
                'max' => 'required|numeric|between:100,100', // Changed to between:0,100 for clarity
                'ingredients' => 'required|array',
                'belongs_to' => 'required|string',
            ], [
                'name.required' => 'Název je povinný.',
                'name.unique' => 'Název musí být unikátní.',
                'code.required' => 'Kód je povinný.',
                'code.string' => 'Kód musí být řetězec.',
                'code.unique' => 'Kód musí být unikátní.',
                'max.numeric' => 'Maximální hodnota musí být číselná.',
                'max.between' => 'Recept vždy musí být naplněn do 100%',
                'ingredients.required' => 'Ingredience jsou povinné.',
                'ingredients.array' => 'Ingredience musí být json.',
                'belongs_to.required' => 'Patří k je povinné.',
                'belongs_to.string' => 'Patří k musí být řetězec.',
            ]);

            if ($validator->fails()) {
                $errors = $validator->errors()->toArray();

                foreach ($errors as $errorKey => $val) {
                    if (is_array($val)) {
                        foreach ($val as $indexedError) {
                            $this->addError($errorKey, $indexedError);
                        }
                    } else {
                        $this->addError($errorKey, $val);
                    }
                }
                return;
            }

            $this->recipe['ingredients'] = base64_encode(json_encode($ingredients));
            unset($this->recipe['max']);
            Recipe::factory()->create($this->recipe);

            $this->success("Receptura", "byla úspěšně přídána!");
            $this->close_recipe_creation();
        } catch (Throwable $e) {
            $this->addError('Něco se pokazilo', $e->getMessage());
        }
    }

        #[LiveComponent] public function search_in_ingredients(string $value = ''): void
    {
        $value = strtolower($value);

        $query = Ingredient::query();

        $query->orderByRaw("
            CASE
                WHEN LEFT(code, 1) = 'S' THEN 0
                ELSE 1
            END
        ");

        $query->orderBy('code');

        if ($value !== '') {
            $query->where(function ($q) use ($value) {
                $q->where(DB::raw('LOWER(name)'), 'like', "%$value%")
                    ->orWhere(DB::raw('LOWER(code)'), 'like', "%$value%");
            });
        }

        $ingredientIds = $this->recipe['ingredients'];
        $this->searchable_ingredients = $query->select(['id', 'code', 'name'])->get()
        ->merge(  Ingredient::whereIn('id', $ingredientIds)->select(['id', 'code', 'name'])->get());
    }

HTML/Blade

 <x-choices
                        label="Ingredience"
                        wire:model="recipe.ingredients"
                        :options="$searchable_ingredients"
                        search-function="search_in_ingredients"
                        no-result-text="Nic podobného neexistuje"
                        searchable
                    >
                        @scope('item', $ingredient)
                        <x-list-item :item="$ingredient" sub-value="bio">
                            <x-slot:actions>
                                <x-badge :value="$ingredient->code" />
                            </x-slot:actions>
                        </x-list-item>
                        @endscope
                        @scope('selection', $ingredient)
                            <div class="relative inline">
                                <span>{{ $ingredient->code }}</span>
                                <input min="0" max="100" wire:model.live="ingredients_percents.{{$ingredient->id}}" @click.stop="true" type="number" name="quantity"
                                       class="!text-right bg-transparent px-3 w-[90px] without-arrows  py-1 my-1 border border-gray-300 rounded-md shadow-sm focus:outline-none sm:text-sm pr-8">
                                <span class="absolute right-2 top-1/2 transform -translate-y-1/2 pointer-events-none text-gray-500">%</span>
                            </div>
                        @endscope
                    </x-choices>
cooffeeRequired commented 1 month ago

image i tried remove the inputs and use another model, but same issue happens

robsontenorio commented 1 month ago

Please, provider a minimal code to reproduce it. I can't run your full code.