Using Laravel 4's Radio form helper, I cannot use Bootstrap 3's radio button syntax and get the inputs to be checked by default.
Here is a normal radio button setup, that works properly
{{Form::radio('awesome_radio_input', 'yes', true)}} Yes
{{Form::radio('awesome_radio_input', 'no')}} No
And here is the BS3 syntax, where I am not getting any radio inputs checked by default
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'yes', true)}}
Yes
</label>
</div>
<div class="radio-inline">
<label>
{{Form::radio('silly_radio_input', 'no')}}
No
</label>
</div>
Any ideas as to why the syntax would break the Radio helper?
Try this :
<div class="form-group">
{{ Form::label('gender', 'Gender') }}
<div class="form-inline">
<div class="radio">
{{ Form::radio('gender', 'true', true) }}
{{ Form::label('men', 'Men') }}
</div>
<div class="radio">
{{ Form::radio('gender', 'false') }}
{{ Form::label('woman', 'Woman') }}
</div>
</div>
</div>
Related
I am rendering a form inside a twig file. I just want to add a default/selected option to the select field and I need to do it inside the twig rather than controller, entity, or the formType.
Here is the code in my twig file:
{{ form_start(prdForm) }}
<div class="form-group">
{{ form_row(prdForm.productTitle, {'label': "Product Title", 'attr': {'placeholder': "Enter Product Name", 'class': "form-control"}}) }}
</div>
<div class="form-group">
{{ form_row(prdForm.productDescription, {'label': "Description", 'attr': {'placeholder': "Provide a short description for this product.", 'class': "form-control"}}) }}
</div>
<div class="form-group">
<option value="" selected="selected" disabled="disabled">Choose an item...</option>
{{ form_widget(prdForm.category, {'label': "Select Category", 'attr': {'class': "form-control"} }) }}
</div>
{{ form_end(prdForm) }}
you can use choice type inside your form. take look on this https://symfony.com/doc/current/reference/forms/types/choice.html#example-usage
cheers
Ok, here is the solution to add a placeholder option into your select field within the twig file, not the best answer but at least it works:
{% do prdForm.category.setRendered %}
<div class="form-group">
<label for="{{prdForm.children.category.vars.id}}" class="required">Category</label>
<select name="{{ prdForm.children.category.vars.full_name }}" id="{{prdForm.children.category.vars.id}}" class="form-control">
<option value="" selected="selected" disabled="disabled">Choose a category</option>
{% for option in prdForm.children.category.vars.choices %}
<option value="{{option.value}}">{{option.label}}</option>
{% endfor %}
</select>
</div>
Of course, thanks to another user for his answer here:
Placeholder for form_widget choices on Twig
I have a list of checkbox created in a form with the following
->add('ISPreNbStudents', ChoiceType::class, [
'multiple' => false,
'choices' => [
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
],
'expanded' => true
])
I can then display this in the twig file with this : {{ form_widget(form.ISOptMonths) }}
The problem is that it now display a list of label and input like this
<input type="radio" id="availability_ISPreNbStudents_0" name="availability[ISPreNbStudents]" value="1">
<label for="availability_ISPreNbStudents_0">1</label>
<input type="radio" id="availability_ISPreNbStudents_1" name="availability[ISPreNbStudents]" value="2">
<label for="availability_ISPreNbStudents_1">2</label>
I need to put wrapper around each label/input like this
<div class="styled-input-single">
<input type="checkbox" name="case-1" id="1" />
<label for="1">1</label>
</div>
<div class="styled-input-single">
<input type="checkbox" name="case-2" id="2" />
<label for="2">2</label>
</div>
How can I achieve this ?
I've finally found the solution :
Here is how you print your list :
{{ form_row(registrationForm.firstRecourse) }}
Now is you want to add wrapper around every option :
<div class="row">
{{ form_label(registrationForm.ISPreNbStudents) }}
{% for checkbox in registrationForm.ISPreNbStudents.children %}
<div class="styled-input-single">
{{ form_widget(checkbox) }}
{{ form_label(checkbox) }}
{{ form_errors(checkbox) }}
</div>
{% endfor %}
</div>
You have to use Symfony Customize Form Rendering.
This way you can access your form field by field, like:
{{ form_start(form) }}
<div class="my-custom-class-for-errors">
{{ form_errors(form) }}
</div>
<div class="row">
<div class="col">
{{ form_row(form.task) }}
</div>
<div class="col" id="some-custom-id">
{{ form_row(form.dueDate) }}
</div>
</div>
{{ form_end(form) }}
You are able to render the fields manually also:
<div class="styled-input-single">
{{ form_widget(form.name_of_form_element) }}
{{ form_label(form.name_of_form_element) }}
<!-- and for errors of this field -->
{{ form_errors(form.name_of_form_element) }}
</div>
For rendering a select or option box manually check this link:
https://symfonycasts.com
TokenMismatchException in VerifyCsrfToken.php line 68,when i try to post a form.
<form method="post" action="url('add')" >
{{ csrf_field() }}
<input type="text" name="title" />
<input type="submit" value="Add" />
</form>
Its was working fine before i added this package.
Instead of {{ Form::open() }} either you can manually add hidden input field in the form just like Andranik Petrosyan suggested
But still I would like you to try
{!! csrf_field() !!} instead of {{ csrf_field() }}
If you don't use Laravel Form, for example:
{!! Form::open([]) !!}
{!! Form::close() !!}
you can use:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Sometimes you need to clear your cache as well.
i have a bootstrap modal dialog which use laravel form to register a user.
Here's the code:
<div id="addPenggunaModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="ModalLabel">Tambah Pengguna Baru</h3>
</div>
<div class="modal-body">
{{ Form::open(array('url'=>'users/addpengguna','class'=>'form-horizontal', 'method'=> 'POST')) }}
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
<div class="control-group">
<label for="firstname" class="control-label">First Name:</label>
<div class="controls">
{{ Form::text('firstname', null, array('class'=>'span3', 'placeholder'=>'First Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="lastname" class="control-label">Last Name: </label>
<div class="controls">
{{ Form::text('lastname', null, array('class'=>'span3', 'placeholder'=>'Last Name')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="email" class="control-label">Email Address: </label>
<div class="controls">
{{ Form::text('email', null, array('class'=>'span3', 'placeholder'=>'Email Address')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="password" class="control-label">Password:</label>
<div class="controls">
{{ Form::password('password', array('class'=>'span3', 'placeholder'=>'Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="confirm_password" class="control-label">Confirm Password:</label>
<div class="controls">
{{ Form::password('password_confirmation', array('class'=>'span3', 'placeholder'=>'Confirm Password')) }}
</div>
</div> <!-- /field -->
<div class="control-group">
<label for="type_user" class="control-label">Tipe Pengguna:</label>
<div class="controls">
{{ Form::radio('level', '1'); }} Supervisor
{{ Form::radio('level', '0'); }} Sales
</div>
</div> <!-- /field -->
</form>
</div>
<div class="modal-footer">
{{ Form::submit('Simpan', array('class'=>'button btn btn-primary','id'=>'mdl_save_change'))}}
<button class="btn" data-dismiss="modal" aria-hidden="true">Batal</button>
</div>
{{ Form::close() }}
</div>
then i use the controller to save the details:
public function postAddpengguna(){
/* function to add user in data pengguna */
$validator = Validator::make(Input::all(), User::$rules);
if($validator -> passes()){
$user = new User;
$user->firstname = Input::get('firstname');
$user->lastname = Input::get('lastname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->level = Input::get('level');
/* save the following details */
$user->save();
return Redirect::to('pengguna');
} else {
return Redirect::to('index');
}
}
but the form doesn't save any data to database. I have another page called registration and it works.
my questions:
how to trace POST from laravel form submit, is there any browser extension?
how to trace error log in laravel
any ideas what's going on in my problem?
thank you in advance.
UPDATE
Here's the screenshot that describe how this works.
bootstrap modal:
when i press the submit button (blue button in modal) i want it to save the data to db. The function php is shown above.
PS. i don't use any AJAX to call the value from the FORM. But when i use the AJAX, it always error because TOKEN is missing.
First of all check the action and _token field of form . To add token field in your form you should include the following line in your form:
<input type="hidden" name="_token" value="{{csrf_token()}}">
To re-use bootstrap modal in your project you can check this Github link
In the latest versions of laravel 5 you can use a shortcut to get the token field.
<form ... >
{!! csrf_field() !!}
</form>
In this case you'll get something like
<input type="hidden" name="_token" value="hpyL7cUbCMFBGRfCi2dpzE5XHGj8WuyY2jqloKRx">
You can in any case get the token string calling csrf_token(), anyway I honestly prefer the csrf_field() alternative.
You may use this code with your ajax code:
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!}
}
});
});
I have seen various posts regarding same issue but nothing could become useful for me.
This is my Form
<form action="" method="post" {{ form_enctype(form) }} novalidate>
{{ form_errors(form) }}
<div class="login_field">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
<div class="clear"></div>
<div class="login_field">
{{ form_label(form.status) }}
{{ form_widget(form.status) }}
</div>
<div class="login_field">
<label> </label>
<input type="submit" value="Create" class="submit_btn" />
</div>
</form>
Error are not being displayed at all. How can i get out of this issue?
You have to include form_errors for every field ...
<div class="login_field">
{{ form_errors(form.name) }}
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
... or just use form_row to render all the three of them together ...
<div class="login_field">
{{ form_row(form.name) }}
</div>
... or let your form errors bubble up to the top using the error_bubbling option for your form-fields in your FormType class. This means they will then be rendered through {{ form_errors(form) }}.
$builder->add('fieldname', 'text', array(
'error_bubbling' => true,
));
quick tip: you can include the submit button in your FormType since symfony 2.3 and don't have to manually render it (reference).
To display all the errors at top of form do like below
<ul>
{% for error in form.vars.errors.form.getErrors(true) %}
<li>{{ error.message }}</li>
{% endfor %}
</ul>