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})";
}
Related
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');
I am using dropdown menu to select the site as:
{{ Form::select('site', $sites, null, ['class' => 'form-control', 'placeholder' => 'Select a Site']) }}
{{ Form::label('site','site*') }}
I am storing the selected site in the database as site_id.
As this returns the index of the selected item from the list, so if the first item is selected the site_id would be stored as 0 in the database.
The problem here is, site_id is a foreign_key in my database and this is causing errors while matching it with the column:id of the sites table. As the id column generated by laravel migration scaffolding starts from 1 and the site_id returned by Form::select starts from 0.
Is there anyway that the index returned from Form::select would start from 1?
Or is there any other way to solve the problem?
When you're getting the sites from DB, keep original IDs to solve the problem:
$sites = Site::pluck('name', 'id');
This code will generate a list of sites' names with real IDs:
[1 => 'Site name 1', 2 => 'Site name 2']
You have many solutions
First, just verify with the validation, site must not be 0 since your controller
use Illuminate\Validation\Rule;
/** ... */
$this->validate($request, [
'site'=>['required',Rule::notIn([0])]
])
You can also check where you save Site model :
DB::transaction(function() use($request){
/** mapping of site attribute */
if($site->save())
{
}
}
You can also check since client side via JavaScript
{{ Form::select('site', $sites, null, ['id'=>'sites', 'class' => 'form-control', 'placeholder' => 'Select a Site']) }}
{{ Form::label('site','site*') }}
$("form input['type=submit']").click(function(e){
e.preventDefault();
if($('#sites').val() == 0)
{
alert('Please, select a site');
return;
}else{
$(this).submit();
}
})
if you want to inset name without id you can use $sites = Site::pluck('name', 'name'); I hope this will work.
I would like to duplicate the same field several times with different values in the drop-down list in twig. I add a simple form with a TextType, but in twig in a for loop, the rendering of the field is done only once. How can I make this system under symfony ? ( In a for loop )
When you try to create a form in your controller and then you render it to you'r view , it gonna be one and only one form , you can't duplicated with a loop because at the end, it gonna give you 2 forms with the same form_id , so if you need 2 forms you need to instantiate them with your builder the same thing with you'r fileds.
Take a look:
$task1 = new Task();
$task2 = new Task();
$form1 = $this->createFormBuilder($task1)
->add('task', TextType::class)->add('task2', TextType::class);
$form2 = $this->createFormBuilder($task2)
->add('task', TextType::class);
And about the drop down , you need to create a form with ChoiceType Field :
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
$builder->add('Tasks', ChoiceType::class, array(
'choices' => array('task1','task2','task3));
CollectionType field type is used to render a "collection" of some field or form. In the easiest sense, it could be an array of TextType fields that populate an array values.
Example:
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
// ...
$builder->add('emails', CollectionType::class, [
// each entry in the array will be an "email" field
'entry_type' => EmailType::class,
// these options are passed to each "email" type
'entry_options' => [
'attr' => ['class' => 'email-box'],
],
]);
The simplest way to render this is all at once:
{{ form_row(form.emails) }}
A much more flexible method would look like this:
{{ form_label(form.emails) }}
{{ form_errors(form.emails) }}
<ul>
{% for emailField in form.emails %}
<li>
{{ form_errors(emailField) }}
{{ form_widget(emailField) }}
</li>
{% endfor %}
</ul>
Please refer this documents for further details : https://symfony.com/doc/current/reference/forms/types/collection.html
I'm new to Laravel and trying some stuff out. Hope you guys can point me in the right direction.
I have a form to add categories on a the page with the overview of all the categories. Now I'm routing to the same page when you want to add or edit a category. Problem is, I want the normal add form to show when the url is category/add and when the url is category/edit/id I want it to show the form to edit (with form model binding). I placed the code in an if else statement, but it always shows the edit form, therefore if I want to add a new category on the url categories I get an error because it's the edit form.
Here's the code in the index.bade.php:
#if(Request::url() === 'categories' )
{{ Form::open(array('url' => 'category/add', 'class' => 'form-horizontal')) }}
#else
{{ Form::model($category, array(
'url' => 'category/edit/'.$category->id ,
$category->id,
'class' => 'form-horizontal'))
}}
#endif
My controller:
public function index()
{
$categories = Category::get();
$category = new Category();
View::share('title', 'Mini Blog - Categories');
$view = View::make('category.index')->with('category', $category);
$view->categories = $categories;
return $view;
}
public function edit($id)
{
$categories = Category::get();
$category = Category::find($id);
View::share('title', 'Mini Blog - Edit category');
return View::make('category.index')->with(array('category' => $category, 'categories' => $categories));
}
Is this the way to go? Or is there a better way?
Instead of checking the URL I suggest you depend on the model variable. Ever Eloquent model has a property exists that tells you if the model has been loaded from the DB or it's a new instance.
#if( ! $category->exists)
{{ Form::open(array('url' => 'category/add', 'class' => 'form-horizontal')) }}
#else
{{ Form::model($category, array(
'url' => 'category/edit/'.$category->id ,
$category->id,
'class' => 'form-horizontal'))
}}
#endif
Actually you can use Form::model with an empty model as well, so you should be able to reduce it to this:
{{ Form::model($category, array(
'url' => ($category->exists ? 'category/edit/'.$category->id : 'category/add'),
'class' => 'form-horizontal'))
}}
There is a much better way.
In your controller do this:
public function create()
{
return View::make('create');
}
public function edit($id)
{
return View::make('edit')->withId($id);
}
Then in your views you have
Create.blade.php
{{ Form::open(array('url' => 'category/add', 'class' => 'form-horizontal')) }}
#include ('form')
{{ Form::close() }}
Edit.blade.php
{{ Form::model($category, array('url' => 'category/edit/'.$category->id, category->id, 'class' => 'form-horizontal')) }}
#include ('form')
{{ Form::close() }}
Form.blade.php
//Put your HTML form stuff here
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.