Home > database >  Radio Button Returns 0 Boolean Value Every Time Laravel 8 Bootstrap
Radio Button Returns 0 Boolean Value Every Time Laravel 8 Bootstrap

Time:09-17

Hello, The following code segment is intended to ask the user if they want to be a guest author at our blogging site, however the radio toggle always returns a zero value. The MYSQL column is type tinyint(1).

                    <!-- Author? -->

                    <div class="author" style="text-align: center; margin-bottom: 1rem;">
                        <h3><strong>Are you interested in earning money blogging for us?</strong></h3>                            
                        <div class="form-check-inline">
                            <input class="form-check-input" name="is_author" type="radio" value="true" id="is_author1" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
                            <h4 class="form-check-label" for="is_author">Yes, please.</h4>
                        </div>

                        <div class="form-check-inline">
                            <input class="form-check-input" name="is_author" type="radio" value="false" id="is_author0" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
                            <h4 class="form-check-label" for="is_author">No, thank you.</h4>
                        </div>
                    </div>

And here is how 'is_author' is referenced in the controller:

protected function validator(array $data)
    {
        return Validator::make($data, [
            'is_author' => ['required', 'boolean'],
        ]);
    }

protected function create(array $data)
{
    return User::create([
        'is_author' => $data['is_author'],
    ]);
}

Thanks in advance

CodePudding user response:

The reason you always get 0 is that your radion buttons always send an input value as a string type. So, it's "true" instead of true. So, you can't validate "true" and "false" using the boolean validation rule. because it's a string and the validation rule will fail always.
In the laravel 8 dock it clearly mentioned " Accepted input are true, false, 1, 0, "1", and "0". " (https://laravel.com/docs/8.x/validation#rule-boolean)

so you can validate your radio buttons using "1" as true and "0" as false without a problem so,
New HTML code,

<div class="author" style="text-align: center; margin-bottom: 1rem;">
<h3><strong>Are you interested in earning money blogging for us?</strong></h3>                            
<div class="form-check-inline">
<input class="form-check-input" name="is_author" type="radio" value="1" id="is_author1" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
<h4 class="form-check-label" for="is_author">Yes, please.</h4>
</div>

<div class="form-check-inline">
<input class="form-check-input" name="is_author" type="radio" value="0" id="is_author0" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
<h4 class="form-check-label" for="is_author">No, thank you.</h4>
</div>
</div>

CodePudding user response:

I solved this by adding 'is_author' to my model in the $fillable array:

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'website',
        'email',
        'password',
        'is_author', /* <--- NEW ELEMENT */
    ];
}

Previously, all values were being returned as my default value I had set in the database, which was zero. Hope this helps someone in the future.

  • Related