Laravel Blade Form::checkbox rendered as select - forms

I have a form that should display as checkboxes but is appearing as a select dropdown.
In the controller I defined the variables:
$user = Auth()->user();
$users = User::all();
$assignedUsers = User::where('assigned', true)->get();
Here is where the form is defined in the view:
{{ Form::open( route('users.access', [$user->id]), ['type' => 'PUT']) }}
{{ Form::checkbox(
'Users',
[
'label' => __('Select Users'),
'options' => $users,
'value' => $assignedUsers
]
) }}
{{ Form::button('submit', ['label' => __('Save changes')]) }}
{{ Form::close() }}
The form is rendering but not as checkboxes but a select dropdown. What am I missing here?

Form::checkbox() method accepts 3 arguments, name, value, and default. You seem passing wrong arguments for the checkbox list so it renders as dropdown, to make a checkbox list, you and use a loop, should be something like so:
#foreach ($users as $user)
<label>
{{ Form::checkbox('users[]', $user->id, isset($assignedUsers[$user->id])) }}
{{ $user->name }}
</label>
#endforeach
Note that I have converted your $assignedUsers variable to key by id for more convernience. You can use your own check method.
$assignedUsers = User::where('assigned', true)->get()->keyBy('id');

Related

Laravel collective custom forms, action Controller#update not defined

This is my route:
Route::get('admin/edit-news/{id}', 'AdminNewsController#edit');
My Controller#update method:
public function update(Request $request, $id)
{
$news = News::find($id);
$news->title = $request->input('title');
$news->content = $request->input('content');
$news->save();
return redirect ('/admin');
}
and my view with custom form:
{{ Form::open(['action' => ['AdminNewsController#update', $news->id], 'method' => 'POST']) }}
{{ Form::bsText('title', $news->title) }}
{{ Form::bsTextArea('content', $news->content) }}
{{ Form::hidden('_method', 'PUT') }}
{{ Form::bsSubmit('Confirm', ['class' => 'btn btn-primary center-block']) }}
{!! Form::close() !!}
The error im getting is
"Action App\Http\Controllers\AdminNewsController#update not defined. (View: D:\xampp\htdocs\laravelhotel\resources\views\admin\news\edit_news.blade.php)"
I dont know why, since the action i put is update function, and i have all the components registered in FormServiceProvider.
If you use PUT method, which is simulated by POST form-method and _method field ({{ Form::hidden('_method', 'PUT') }}), you need to use the corresponding route:
Route::put('admin/edit-news/submit', 'AdminNewsController#update');
// ^^^

Laravel form face use $key => $value ($value2) instead of $key => $value

Sorry if the title is misleading...
I have the following to create a select box using the laravel Form facade:
{{ Form::select('category_select', $categories_select, null, array('class' => 'selectpicker show-tick', 'data-live-search' => 'true', 'id' => 'category_select')) }}
Now $catgories_select is a pluck() of id and name.
I want to do the same for another select (Tax rules) but there I want to have it like the following:
<option value="id">$value1 ($value2)</option>
How can I do this?
If you pluck the models before the view, you'll need two collections. But you could just do one query, then pluck inline. Let's assume $categories is your queried collection before the pluck:
{{ Form::select('category_select', $categories->pluck('name', 'id'), null) }}
Then on the other you'd pluck a mutated property:
{{ Form::select('category_select_tax', $categories->pluck('compositeName', 'id'), null) }}
And then, on your Category model you'd have:
public function getCompositeNameAttribute()
{
return "{$this->name} ({$this->value2})";
}

bootstrap style conflicts with forms bound with models laravel 4

I am developing an application with laravel 4, I have forms that are filled with data from a model, in other words a model is bound to the form
I need to add some bootstrap style to the form and I do it this way
this is my view file:
{{
Form::model($user, array( $user->id))
}}
{{ Form::label('last_name', 'Last Name') }}
{{ Form::text('last_name', '', array('class' => 'form-control')) }}
{{ Form::close() }}
and this is my controller code:
public function edit(){
$user = User::find(Auth::user()->id);
return View::make('edit')->with('user',$user);
}
The Issue:
As soon as I add this class to the form's element the content of the form will disappear and no data is bound to it anymore, how should I fix it?
I found the answer the second argument of this
{{ Form::text('last_name', '', array('class' => 'form-control')) }}
is the input value of text field so it should be like this:
{{ Form::text('last_name', $user->last_name, array('class' => 'form-control')) }}

Symfony2.1: how to render a "repeated" widget by hand

In Symfony2.1 (as well in Symfony2.0) one can render a widget by hand in a Twig template. So, if one would to render a text field name "username", the related label, error and input can be rendered separately, i.e.:
{{ form_label(form.username) }}
{{ form_errors(form.username) }}
{{ form_widget(form.username) }}
In Symfony2.1, the Repeated field group has been introduced. It is useful to ensure the user is not inserting a wrong value for an important entry (e.g. the email or password).
The question is, how to render it in a Twig template by hand?
Please, notice that {{ form_widget(form.username) }} in this case will render the whole component (i.e. both labels and inputs).
$builder->add('userPass', 'repeated', array(
'type' => 'password',
'label' => 'Zayso Password',
'required' => true,
'invalid_message' => 'The password fields must match.',
'constraints' => new NotBlank(),
'first_options' => array('label' => 'Zayso Password'),
'second_options' => array('label' => 'Zayso Password(repeat)'),
'first_name' => 'pass1', // form.userPass.pass1
'second_name' => 'pass2', // form.userPass.pass2
));
In your template you can do: {{ form_widget(form.userPass.pass1 }}. Not sure where it is documented but found it somewhere.
Late answer, but you can also add
{{ form_widget(form.username.first) }}
to generate the first widget element.
If you want to display the label and widget separately, you can use:
{{ form_label(form.username.first) }}
{{ form_errors(form.username.first) }}
{{ form_widget(form.username.first) }}
{{ form_label(form.username.second) }}
{{ form_errors(form.username.second) }}
{{ form_widget(form.username.second) }}
Here is the documentation for Repeated Field Type.

Symfony2 Accessing each entity field elements value in Twig

I have one form that contains entity type field parameters:
->add('parameters', 'entity', array(
'class' => 'SPlaceBundle:Parameter',
'query_builder' => function(ParameterRepository $er)
{
return $er
->createQueryBuilder('s')
->where('s.type = :type1 or s.type = :type2')
->setParameter('type1', 1)
->setParameter('type2', 2)
->orderBy('s.name', 'ASC');
},
'property' => 'name',
'multiple' => true,
'expanded' => true,
))
As you can see I only display parameters with type=1 or type=2.
While rendering template I would like to place hr (or something else) between checkboxes representing different parameter types.
I was trying to use {{ field.get('value').type }} trick to get parameter type:
{% for p in form.parameters %}
{{ form_widget(p) }}
{{ form_label(p) }}
{{ p.get('value').type }}
<br>
{% endfor %}
The problem is that above {{ p.get('value') }} returns parameter id (int) instead of parameter object.
Is there a way to return object?
It's not really elegant, but you could concatenate the type and name in your select, and use that as label. Then, when displaying the labels, split on the delimiter and you have both type and name.