Assign attributes to form elements

Published 9 months ago by JokersMild85

I asked this question a couple of days ago but the answers I received did not resolve my problem. I am trying to assign attributes to form elements, namely an ID attribute to my form checkboxes. When I attempt to do this, all it does is set all the checkboxes in my form to checked. Any thoughts why this is happening and how to successfully assign my checkboxes an ID attribute?

@foreach($exercise_type->exercises as $exercise)

    {!! Form::label('exercise_name', $exercise->exercise_name) !!}
    {!! Form::checkbox( 'exercise[]', '{$exercise->id}_{$exercise->exercise_type_id}_{$exercise->exercise_name}', ['id' => '{$exercise->exercise_name}'] ) !!}
    </br>

@endforeach
Best Answer (As Selected By JokersMild85)
ejdelmonico

@JokersMild85 You can't find much on it because most people stopped using Laravel Collective when Laravel 5.0 came out. It is much easier to use a standard old HTML5 form. Besides the package does not support 5.5 as of yet. Also, attributes can be added with an array.

Here is what you can add to a checkbox per the code:

/**
     * Create a checkbox input field.
     *
     * @param  string $name
     * @param  mixed  $value
     * @param  bool   $checked
     * @param  array  $options
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function checkbox($name, $value = 1, $checked = null, $options = [])
    {
        return $this->checkable('checkbox', $name, $value, $checked, $options);
    }
Snapey
Snapey
9 months ago (993,685 XP)

Its good that the Form library saves you sooooo much time because you can then use the time saved (plus some) working out how to make it actually work

ejdelmonico

Good one! Snapey!

Snapey
Snapey
9 months ago (993,685 XP)

However, to be fair to the Form library, you should probably want to be using double braces {{ }} if you expect blade to replace parameters

JokersMild85

Don't need to use double brackets if they are already inside the {!! !!} brackets. I had to use double quotes instead of the single quotes in the value parameter to fix that.

@foreach($exercise_type->exercises as $exercise)

    {!! Form::label('exercise_name', $exercise->exercise_name) !!}
    {!! Form::checkbox( 'exercise[]', "{$exercise->id},{$exercise->exercise_type_id},{$exercise->exercise_name}") !!}
    </br>

@endforeach 

Why is it that I can't find an answer for adding an attribute to form elements? This shouldn't be that hard to do.

ejdelmonico

@JokersMild85 You can't find much on it because most people stopped using Laravel Collective when Laravel 5.0 came out. It is much easier to use a standard old HTML5 form. Besides the package does not support 5.5 as of yet. Also, attributes can be added with an array.

Here is what you can add to a checkbox per the code:

/**
     * Create a checkbox input field.
     *
     * @param  string $name
     * @param  mixed  $value
     * @param  bool   $checked
     * @param  array  $options
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function checkbox($name, $value = 1, $checked = null, $options = [])
    {
        return $this->checkable('checkbox', $name, $value, $checked, $options);
    }
JokersMild85

I'm sorry, I'm still getting used to working in Laravel. Where would this function be placed so I can call it?

ejdelmonico

That is what the Collective package uses. I only posted it so that you could see that your options (attributes) can be inserted as an array in the fourth param and a name in the first param. If you want something custom, then override the method but it really isn't worth your time in all honesty. Using Collective just slows you down. It is easier to just write a normal form.

JokersMild85

Ok, thank you. Only used it because that is what was being used in the Laracasts tutorial.

ejdelmonico

That must be an outdated tutorial because I don't think Jeffrey has used it in a few years. Some other tutorials around probably still use it but as you can see on the site, there is no version for 5.5. I would probably say that interest is very low in using the package since it was separated from Laravel.

Snapey
Snapey
9 months ago (993,685 XP)

check this thread https://laracasts.com/discuss/channels/laravel/old-data-didnt-display-in-edit-page for examples of how to natively use all the various form elements and taking into account the current state of the model

Please sign in or create an account to participate in this conversation.