thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

Radio button from bread doesn't show the selected value if "0" (zero) is selected #5221

Closed kappafix closed 3 years ago

kappafix commented 3 years ago

Version information

Description

Radio button from bread doesn't show the "selected" value if "0" (zero) "Optional Details" of the radio button { "default": "0", "options": { "1": "Attivo", "0": "Draft" } }

Steps To Reproduce

Steps to reproduce the behavior:

  1. Go to a 'Bread'
  2. Click on 'edit'
  3. Create a radiobutton field, specifiend in the "Optional Details" the json as described
  4. Save it
  5. When you go to edit the record the radio button is "no selected" if the value is "0" zero.

Expected behavior

The radio button should be selected

Screenshots

image image

Additional context

emptynick commented 3 years ago

We don't know the value inside your database. Does it work when you add an entry?

kappafix commented 3 years ago

Yes it works

Il giorno mer 10 feb 2021 alle 12:44 Christoph Schweppe < notifications@github.com> ha scritto:

We don't know the value inside your database. Does it work when you add an entry?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/the-control-group/voyager/issues/5221#issuecomment-776651478, or unsubscribe https://github.com/notifications/unsubscribe-auth/AK7AUVJDNIZNQSRQKPSUS73S6JWSFANCNFSM4XMW66CQ .

-- Cosimo Elefante ell: +39 392 15 47 100 mail: miele123@gmail.com

MrCrayon commented 3 years ago

Probably something to do with the use of empty here: https://github.com/the-control-group/voyager/blob/3c2d3149c8a99157d9568717cd6330f6a8af3102/resources/views/formfields/radio_btn.blade.php#L1

I'm wrapping my head around that check :rofl:

I think something like this should do the same job:

$selected_value = old($row->field, $dataTypeContent->{$row->field} ?? null);

@kappafix Can you try and let us know if that works?

emptynick commented 3 years ago

We still don't know what the value in the database is when you edit an entry. It could just be an empty string. Because it's working when adding an entry it makes me think that it should work the same with proper values in the DB.

kappafix commented 3 years ago

The value in the database is "0" zero when it doesn't work. The db field is "INT", default value of the db is "0". I could change the value of radio, if set 1 I see it selected when I access it again. If I set the value by the radio it changes to zero into the the db, but when I reopen my record from bread for editing it seems not selected.

kappafix commented 3 years ago

@emptynick , @MrCrayon mrcr image doing a vardump in the "voyager/resources/views/formfields/radio_btn.blade.php", at th begininng of the blade, like this: "var_dump($dataTypeContent->{$row->field});" we can see the value is "int 0", as expected, but the radio doesn't work. @MrCrayon how could patch/modify the blade script? It's not clear for me. TKS

MrCrayon commented 3 years ago

@emptynick the problem is this is false when the value is zero: empty($dataTypeContent->{$row->field})

@kappafix you can override the view in your project creating this file: yourproject/resources/views/vendor/voyager/formfields/radio_btn.blade.php

kappafix commented 3 years ago

Many thanks @MrCrayon , I found it after your indications. I patched it as following:

<?php 
$selected_value = (isset($dataTypeContent->{$row->field}) && NULL !== old( #&& isset(old(
    $row->field,
                $dataTypeContent->{$row->field}
)
#)
) ? old(
                    $row->field,
        $dataTypeContent->{$row->field}
                ) : old($row->field); ?>
                                        <?php $default = (isset($options->default) && !isset($dataTypeContent->{$row->field})) ? $options->default : null; ?>
<ul class="radio">
    @if(isset($options->options))
        @foreach($options->options as $key => $option)
            <li>
                <input type="radio" id="option-{{ \Illuminate\Support\Str::slug($row->field, '-') }}-{{ \Illuminate\Support\Str::slug($key, '-') }}"
                       name="{{ $row->field }}"
                       value="{{ $key }}" @if($default == $key && $selected_value === NULL) checked @endif @if($selected_value == $key) checked @endif>
                <label for="option-{{ \Illuminate\Support\Str::slug($row->field, '-') }}-{{ \Illuminate\Support\Str::slug($key, '-') }}">{{ $option }}</label>
                <div class="check"></div>
            </li>
        @endforeach
    @endif
</ul>
MrCrayon commented 3 years ago

@kappafix that looks even worse than before :rofl:

MrCrayon commented 3 years ago

@kappafix fixed in #5223, try with that code

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.