Update form field with AJAX Symfony2 - php

I have form:
<form action="{{ path('book_create') }}" method="post" {{ form_enctype(form) }}>
{{ form_start(form) }}
{{ form_row(form.bookFoto) }}
{{ form_row(form.bookTitle) }}
{{ form_row(form.categories) }}
<p class="new_category">+ Add category</p>
{{ form_row(form.authors) }}
<p>+ Add author</p>
{{ form_end(form) }}
When I click 'Add category' I load with AJAX form for create new Category Entity, and save it with AJAX too.
But I don't understand how can I update entity field type categories without reloading form.

To deal with embedded collection you need to manage your form with JS. Symfony has built-in helper for it. It is called prototype and used for populating form with new rows of embedded collection:
<ul class="tags" data-prototype="{{ form_widget(form.categories.vars.prototype)|e }}">
...
</ul>
You can read more at official documentation:
http://symfony.com/doc/current/cookbook/form/form_collections.html

Related

Accessing Object Attributes within Symfony Forms

I am trying to display a list of courses with a checkbox allowing the user to select any number of courses from the list. I am new to Symfony and trying to follow the form approach but do not understand how to display additional attributes of an object beyond using the choice_label.
If I were just passing the course objects, I could simply use:
Template:
<form>
{% for course in courses %}
<div class="row">
<div><input type="checkbox" name="course[]" value="{{ course.id }}"></div>
<div>{{ course.name }}</div>
<div>{{ course.description }}</div>
<div>{{ course.semester }}</div>
</div>
{% endfor %}
</form>
Using the form builder, it seems my template would look like this:
{{ form_start(form) }}
<div class="row">
<div>{{ form_row(form.courses) }}</div>
</div>
{{ form_end(form) }}
How can I access these additional object attributes (name, description, etc.) within the form row? Is there a reason to use to the form builder in this case instead of the first 'by hand' approach? In summary, I need granular control of the object attributes within a given form row and the choice_label attribute alone does not seem sufficient. What is a potential solution?
First, for accessing each option of the choice label, it's fairly simple... because the form.courses is an array.
You can access individual checkbox by doing this :
{{ form_widget(form.courses[0]) }}
And you can use a loop to access them individually. And for customizing the rendering of your forms, you can use form_errors, form_label and form_help functions, so your final code will be something like this :
{{ form_start(form) }}
{{ form_errors(form) }}
{% for course in form.courses %}
<div class="row">
{{ form_widget(course) }}
{{ form_label(course) }}
</div>
{% endfor %}
{{ form_help(form.courses) }}
{{ form_end(form) }}
Note: The label is the key value in the array passed to the « choices » option in Form Builder.
Sources:
How to Customize Form Rendering : https://symfony.com/doc/current/form/form_customization.html

Symfony Customize form errors css

I'm using symfony 2.8. In registration form, when I submit form with empty fields, it is showing validation errors in tag. It is really making the UI looks bad. How can I change the css of each validation error messages.
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register','novalidate': 'novalidate'}}) }}
<div class="form-group">
{{ form_row(form.name) }}
</div>
<div class="form-group">
{{ form_row(form.email) }}
</div>
<div class="form-group">
{{ form_row(form.username) }}
</div>
<div class="form-group">
{{ form_row(form.plainPassword.first) }}
</div>
<div class="form-group">
{{ form_row(form.plainPassword.second) }}
</div>
<div>
<input type="submit" class="btn btn-primary" value="{{ 'registration.submit'|trans }}" />
</div>
{{ form_end(form) }}
You are rendering form fields using form_row widget which renders label, field & related error to do custom styling you can render your fields and their labels and errors individually like
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register','novalidate': 'novalidate'}}) }}
{{ form_label(form.email) }}
<div class="some_class">{{ form_errors(form.email) }} </div>
{{ form_widget(form.email) }}
{{ form_end(form) }}
<style type="text/css">
.some_class{
/* write custom styling rules here */
}
</style>
Or get all errors in one place like
{# render any "global" errors #}
{{ form_errors(form) }}
Reference: Twig Template Form Function and Variable Reference
The errors have their own element and classes that you could target from your css.
One basic approach could by using a form theme as described here, or you could create your own form theme.
If you want to customise the tags it renders you could do what this guy does here.

Symfony2 and Twig form rendering in if - else condition

In my Symfony 2 app I got the following code rendering the form:
{{ form_start(form) }}
{{ form_errors(form) }}
<div class="form-group">
{{ form_label(form.title) }}
{{ form_widget(form.title) }}
</div>
<div class="form-group">
{{ form_label(form.message) }}
{{ form_widget(form.message) }}
</div>
{% if extras == true %} //this block should be rendered only if extras var is true
<div class="form-group">
{{ form_label(form.description) }}
{{ form_widget(form.description) }}
</div>
{% endif %}
{{ form_end(form) }}
The problem is that I get rendered {{ form_widget(form.description) }} even if my extras var is false, not with all other form fields but somewhere at the bottom of the form which is obviously a bug. How to make it render only if extras is true and disappear completely from the page in case extras is false?
Thank you.
All other form fields are automatically added to the end of your form by default. It triggers {{ form_rest() }} by default. Use this code to prevent this behavior:
{{ form_end(form, {'render_rest': false}) }}
http://symfony.com/doc/current/reference/forms/twig_reference.html#form-end-view-variables

Prevent form fields from rendering

I'm trying to control the rendering of password fields based on whether i'm editing an user or creating one. I'm doing this with a simple session boolean variable as follows:
{{ form_start(userForm) }}
{{ form_errors(userForm) }}
<div id="user-fg-email" class="form-group">
{{ form_label(userForm.email) }}
{{ form_errors(userForm.email) }}
{{ form_widget(userForm.email) }}
</div>
{% if app.session.get('editingUser') == false %}
<div id="user-fg-pp1" class="form-group">
{{ form_label(userForm.plainPassword.first) }}
{{ form_widget(userForm.plainPassword.first) }}
</div>
<div id="user-fg-pp2" class="form-group">
{{ form_label(userForm.plainPassword.second) }}
{{ form_errors(userForm.plainPassword.first) }}
{{ form_widget(userForm.plainPassword.second) }}
</div>
{% endif %}
<div id="user-fg-role" class="form-group">
{{ form_label(userForm.role) }}
{{ form_errors(userForm.role) }}
{{ form_widget(userForm.role) }}
</div>
<button type="submit" class="btn btn-primary pull-right">Submit</button>
{{ form_end(userForm) }}
However when this boolean is evaluated as true, which is supposed to prevent these fields from rendering, they are still being rendered assumingly by the later following form_end tag.
Is there a way to prevent that from happening?
edit:
if editingUser == true the password fields are actually rendered after the button, hence my assumption it's done so by the form_end tag.
Because you have to specify Twig to not display all the rest of the fields which are not explicitly rendered in the form : http://symfony.com/doc/current/reference/forms/twig_reference.html#form-end-view-variables
{{ form_end(form, {'render_rest': false}) }}

Laravel 4.1 _token error on form submit

I am doing simple cms in laravel 4.1 , i created many form and they working fine, but the last form which i create throws error on submit.
Illuminate \ Database \ Eloquent \ MassAssignmentException
_token
The data posted by form also show on error page.
_token KLlDjuFgaEmuGHKMpFjqSrukYT3sawOYYZLPGxnb
name asdf
body asdfasdfa
bio sdfasdf
So its mean the _token is also posted then why i am getting this error.
My form look like this.
{{ Form::open(array('route' => 'admin.teachers.store','files'=>true)) }}
<ul>
<li>
{{ Form::label('image', 'Image:') }}
{{ Form::file('image') }}
</li>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::label('body', 'Body:') }}
{{ Form::textarea('body',null,array('class'=>'ckeditor')) }}
</li>
<li>
{{ Form::label('bio', 'Bio:') }}
{{ Form::textarea('bio',null,array('class'=>'ckeditor')) }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
</li>
</ul>
{{ Form::close() }}
I see one related question to _token issue on forum but it didn't help me.
Thanks in advance :)
In fact your error is MassAssignmentException, which means that you are using
Model::create($input);
In your controller and not using
protected $fillable = array('columnA', 'name'...);
or
protected $guarded = array();
In your Model, to tell Laravel which fields of your table are mass assignable.
Take a look at the docs: http://laravel.com/docs/eloquent#mass-assignment

Categories