kdion4891 / laravel-livewire-forms

A dynamic, responsive Laravel Livewire form component with realtime validation, file uploads, array fields, and more.
243 stars 54 forks source link

checkboxes is behaving strange #7

Open rabol opened 4 years ago

rabol commented 4 years ago

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 commented 4 years ago

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 :)

kejojedi commented 4 years ago

please submit a pr, this is a good change

lalov commented 4 years ago

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);
            }
        }
    }
pdiveris commented 3 years ago

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 commented 3 years ago

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?