bootstrap style conflicts with forms bound with models laravel 4 - forms

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')) }}

Related

Laravel Blade Form::checkbox rendered as select

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');

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');
// ^^^

Symfony2 form builder add field between fields

I have a form builder object that I am adding fields to. How do I added a field between 2 existing fields:
$formBuilder->add('name',...)
->add('phone',...);
//Somehow here add an 'email' field between the 'name' and 'phone' field
$form = $formBuilder->getForm();
You can change the order by rendering the form in parts with twig.
{{ form_row(form.name) }}
{{ form_row(form.email) }}
{{ form_row(form.phone) }}

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.