laravel-livewire-forms icon indicating copy to clipboard operation
laravel-livewire-forms copied to clipboard

checkboxes is behaving strange

Open rabol opened this issue 4 years ago • 5 comments

checkboxes is behaving strange:

my component:

<?php

namespace App\Http\Livewire;

use Kdion4891\LaravelLivewireForms\Field;
use Kdion4891\LaravelLivewireForms\FormComponent;

class SkeletonForm extends FormComponent
{
    public function fields()
    {
        return [
            Field::make('Name')->input()->rules('required'),
            Field::make('Features')->checkboxes(['Soft delete', 'API', 'Create from','Edit form','Show page','Delete action']),
        ];
    }

    public function success()
    {
        ddd($this->form_data);
    }

    public function saveAndStayResponse()
    {
        return redirect()->route('models.create');
    }

    public function saveAndGoBackResponse()
    {
        return redirect()->route('models.index');
    }
}

When the form is displayed and I check one checkbox, all are selected

rabol avatar Apr 08 '20 18:04 rabol

I have changed the 'view' for the checkboxes to this:

<div class="form-group row">
    <div class="col-md-2 col-form-label text-md-right py-md-0">
        {{ $field->label }}
    </div>

    <div class="col-md">
        @foreach($field->options as $value => $label)
            <div class="form-check">
                <input
                    id="{{ $field->name . '.' . $loop->index }}"
                    type="checkbox"
                    class="form-check-input @error($field->key) is-invalid @enderror"
                    value="{{ $value }}"
                    wire:model.lazy="{{ $field->key . '.' . $loop->index }}">

                <label class="form-check-label" for="{{ $field->name . '.' . $loop->index }}">
                    {{ $label }}
                </label>
            </div>
        @endforeach

        @include('laravel-livewire-forms::fields.error-help')
    </div>
</div>

the change is: from

wire:model.lazy="{{ $field->key }}"

to

wire:model.lazy="{{ $field->key . '.' . $loop->index }}"

seems to work :)

rabol avatar Apr 08 '20 18:04 rabol

please submit a pr, this is a good change

kejojedi avatar Apr 11 '20 04:04 kejojedi

In setFormProperties() in the FormComponent class, the checkbox type is set with default value array. When this is changed so that checkboxes is set to array everything works.


public function setFormProperties($model = null)
    {
        $this->model = $model;
        if ($model) $this->form_data = $model->toArray();

        foreach ($this->fields() as $field) {
            if (!isset($this->form_data[$field->name])) {
                $array = in_array($field->type, ['checkboxes', 'file']);
                $this->form_data[$field->name] = $field->default ?? ($array ? [] : null);
            }
        }
    }

lalov avatar Jun 05 '20 07:06 lalov

First things first, thank you for this package, it has cut down significantly the development time for a box standard admin panel I needed planting in a project. Second, I can confirm that I hae the same issues mentioned in this issue and that @rabol's solution is working for me too.

pdiveris avatar Jan 03 '21 00:01 pdiveris

In setFormProperties() in the FormComponent class, the checkbox type is set with default value array. When this is changed so that checkboxes is set to array everything works.

public function setFormProperties($model = null) { $this->model = $model; if ($model) $this->form_data = $model->toArray();

    foreach ($this->fields() as $field) {
        if (!isset($this->form_data[$field->name])) {
            $array = in_array($field->type, ['checkboxes', 'file']);
            $this->form_data[$field->name] = $field->default ?? ($array ? [] : null);
        }
    }
}

I can't see the diference from current code; furthermore, it didn't work for me. Am I missing something?

pdiveris avatar Jan 03 '21 00:01 pdiveris