Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

checkboxes is behaving strange #7

Open
rabol opened this issue Apr 8, 2020 · 5 comments
Open

checkboxes is behaving strange #7

rabol opened this issue Apr 8, 2020 · 5 comments

Comments

@rabol
Copy link

rabol commented Apr 8, 2020

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
Copy link
Author

rabol commented Apr 8, 2020

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
Copy link

please submit a pr, this is a good change

@lalov
Copy link

lalov commented Jun 5, 2020

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
Copy link

pdiveris commented Jan 3, 2021

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
Copy link

pdiveris commented Jan 3, 2021

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants