Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kappafix opened this issue Feb 10, 2021 · 11 comments · Fixed by #5223
Closed

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

kappafix opened this issue Feb 10, 2021 · 11 comments · Fixed by #5223

Comments

@kappafix
Copy link

kappafix commented Feb 10, 2021

Version information

  • Laravel: v7.16.1
  • Voyager: v1.4.1
  • PHP: 7.3.19
  • Database: MySql 8.0.21

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

@kappafix kappafix changed the title Radio button from bread doesn't show the selected value if "0" (zero) Radio button from bread doesn't show the selected value if "0" (zero) is selected Feb 10, 2021
@emptynick
Copy link
Collaborator

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

@kappafix
Copy link
Author

kappafix commented Feb 10, 2021 via email

@MrCrayon
Copy link
Collaborator

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 🤣

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

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

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

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

MrCrayon commented Feb 10, 2021

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

kappafix commented Feb 10, 2021

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

@kappafix that looks even worse than before 🤣

MrCrayon added a commit to MrCrayon/voyager that referenced this issue Feb 11, 2021
Removed some insanity and simplified with _new_ null coalescing operator.

Fixes thedevdojo#5221
@MrCrayon
Copy link
Collaborator

@kappafix fixed in #5223, try with that code

emptynick added a commit that referenced this issue May 3, 2021
Removed some insanity and simplified with _new_ null coalescing operator.

Fixes #5221

Co-authored-by: Christoph Schweppe <info@cschweppe.de>
@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants