I have a problem with the render of my view.
Here is how I build my form :
$builder->add('intitule', CKEditorType::class, [
'label'=>'NEW_CGU',
'required'=>false,
'data'=>'testj'
]);
I'm displaying it like this :
<div class="row m-0">
<div class="col-xs-5 plr-16 pt-16 pb-16 background-white" style="width : 100%">
{{ form_label(form.intitule) }}
<textarea id="editor1" >
{{ form_start(form, { 'attr': { 'novalidate': 'novalidate' }}) }}
{{ form_widget(form.intitule, { 'attr': { 'class': 'inscription plr-16' , 'rows' : '20'}}) }}
{{ form_errors(form.intitule) }}
{#<p class="help-block">Maximum 500 caractères.</p>#}
</textarea>
<div class="text-right">
{{ "BACK"|trans }}
{% if is_granted('ROLE_PREVIOUS_ADMIN') %}
<button disabled class="button-square" title="non accessible en consultation">Valider</button>
{% else %}
<button type="submit" class="button-square" onclick="return confirm('Êtes vous sûr de vouloir effectuer ces modifications?')">Valider</button>
{% endif %}
</div>
{{ form_end(form) }}
</div>
</div>
Everything works, but there is something wrong with my display. I have a textarea within the "block text" of CKEditor
Finally, my JS looks like this :
$(function() {
CKEDITOR.replace( 'editor1', {
removePlugins: 'magicline',
height: 400
} );
});
What am I doing wrong please ?
Just simply render your widget out of html form inputs !
Remove your <textarea id="editor1" > tag.
Related
I try editAction in symfony but I have error when if not change input file it update field file in database to null ... how to not update field file if not changed value in update action
code action:
/**
* #Route("/babysitter/update/{id}", name="update_babysitter_by_admin")
*
* #param Request $request
* #param BabySitter $babySitter
* #ParamConverter("id", options={"id": "id"})
*
* #return mixed
*/
public function updateBabySitterAction(BabySitter $babySitter, Request $request){
$em= $this->getDoctrine()->getManager();
$form= $this->createForm(BabySitterType::class, $babySitter,['requiredFile'=> false]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
if($babySitter->getPicture()->getDocument()){
$this->uploadDocument->upload($babySitter->getPicture(), $this->getParameter('pictures_directory'));
}
if($babySitter->getCriminalRecord()->getDocument()){
$this->uploadDocument->upload($babySitter->getCriminalRecord(), $this->getParameter('criminalRecord_director_babySitter'));
}
if($babySitter->getIdCards()){
$this->uploadDocument->uploadIdCard($babySitter->getIdCards(), $babySitter,$this->getParameter('idCard_directory'));
}
$em->persist($babySitter);
$em->flush();
$url = $this->generateUrl('info_babySitter',['id'=> $babySitter->getId()]);
$response = new RedirectResponse($url);
return $response;
}
return $this->render('admin/registerBabySitter.html.twig',[
'form'=> $form->createView()
]);
}
code twig:
{% trans_default_domain 'FOSUserBundle' %}
<div class="register-box" style="width:460px">
<div class="register-box-body">
<p class="login-box-msg">Register a new BabySitter</p>
{{ form_start(form, {'method':'post', 'attr': {'class': 'fos_user_registration_register', 'novalidate': 'novalidate'}}) }}
<div class="form-group has-feedback">
{{ form_widget(form.email,{'attr': {'class': 'form-control', 'placeholder': 'Email'}}) }}
{{ form_errors(form.email) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.firstName,{'attr': {'class': 'form-control', 'placeholder': 'FirstName'}}) }}
{{ form_errors(form.firstName) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.lastName,{'attr': {'class': 'form-control', 'placeholder': 'LastName'}}) }}
{{ form_errors(form.lastName) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.plainPassword.first,{'attr': {'class': 'form-control', 'placeholder': 'Password'}}) }}
{{ form_errors(form.plainPassword.first) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.plainPassword.second,{'attr': {'class': 'form-control', 'placeholder': 'Repeat Password'}}) }}
{{ form_errors(form.plainPassword.second) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.genre,{'attr': {'class': 'form-control', 'placeholder': 'Genre'}}) }}
{{ form_errors(form.genre) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.dateBirth,{'attr': {'class': 'form-control', 'placeholder': 'date Birthday'}}) }}
{{ form_errors(form.dateBirth) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.linkVideo,{'attr': {'class': 'form-control', 'placeholder': 'link Video'}}) }}
{{ form_errors(form.linkVideo) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.criminalRecord) }}
{{ form_errors(form.criminalRecord) }}
</div>
<div class="form-group has-feedback">
<ul id="idCard-fields-list"
data-prototype="{{ form_widget(form.idCards.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}">
{{ form_row(form.idCards) }}
{% for idCardField in form.idCards %}
<li>
{{ form_errors(idCardField) }}
{{ form_widget(idCardField) }}
</li>
{% endfor %}
</ul>
<div class="row">
<button type="button"
class="add-another-collection-widget-idCard btn btn-primary btn-flat"
data-list="#idCard-fields-list">Add another idCard</button>
</div>
</div>
<div class="form-group has-feedback">
{{ form_widget(form.nbrYears,{'attr': {'class': 'form-control', 'placeholder': 'Number Years'}}) }}
{{ form_errors(form.nbrYears) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.rib,{'attr': {'class': 'form-control', 'placeholder': ' rib'}}) }}
{{ form_errors(form.rib) }}
</div>
<div class="form-group has-feedback">
{{ form_widget(form.presentation,{'attr': {'class': 'form-control', 'placeholder': 'presentation'}}) }}
{{ form_errors(form.presentation) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.adress) }}
{{ form_errors(form.adress) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.availability) }}
{{ form_errors(form.availability) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.assignement) }}
{{ form_errors(form.assignement) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.qualification) }}
{{ form_errors(form.qualification) }}
</div>
<div class="form-group has-feedback">
{{ form_row(form.picture) }}
{{ form_errors(form.picture) }}
</div>
<div class="form-group has-feedback">
<ul id="language-fields-list"
data-prototype="{{ form_widget(form.languages.vars.prototype)|e }}"
data-widget-tags="{{ '<li></li>'|e }}">
{{ form_widget(form.languages) }}
{% for languageField in form.languages %}
<li>
{{ form_errors(languageField) }}
{{ form_widget(languageField) }}
</li>
{% endfor %}
</ul>
<div class="row">
<button type="button"
class="add-another-collection-widget btn btn-primary btn-flat"
data-list="#language-fields-list">Add another language</button>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input type="submit" class="btn btn-primary btn-block btn-flat" value="{{ 'registration.submit'|trans }}">
</div>
<!-- /.col -->
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
<!-- /.form-box -->
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
jQuery(document).ready(function () {
jQuery('.add-another-collection-widget').click(function (e) {
var list = jQuery(jQuery(this).attr('data-list'));
// Try to find the counter of the list or use the length of the list
var counter = list.data('widget-counter') | list.children().length;
// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data('widget-counter', counter);
// create a new list element and add it to the list
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
newElem.appendTo(list);
addTagFormDeleteLink(newElem);
});
function addTagFormDeleteLink($tagFormLi) {
var $removeFormButton = $('<button class="btn btn-danger btn-flat" style="margin-top:2%;margin-left:50%" type="button">Delete this Language</button>');
$tagFormLi.append($removeFormButton);
$removeFormButton.on('click', function(e) {
// remove the li for the tag form
$tagFormLi.remove();
});
}
jQuery('.add-another-collection-widget-idCard').click(function (e) {
var list = jQuery(jQuery(this).attr('data-list'));
// Try to find the counter of the list or use the length of the list
var counter = list.data('widget-counter') | list.children().length;
// grab the prototype template
var newWidget = list.attr('data-prototype');
// replace the "__name__" used in the id and name of the prototype
// with a number that's unique to your emails
// end name attribute looks like name="contact[emails][2]"
newWidget = newWidget.replace(/__name__/g, counter);
// Increase the counter
counter++;
// And store it, the length cannot be used if deleting widgets is allowed
list.data('widget-counter', counter);
// create a new list element and add it to the list
var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
if(counter <= 3){ newElem.appendTo(list); }
});
function addIdCardFormDeleteLink($tagFormLi) {
var $removeFormButton = $('<button class="btn btn-danger btn-flat" style="margin-top:2%;margin-left:50%" type="button">Delete this idCard</button>');
$tagFormLi.append($removeFormButton);
$removeFormButton.on('click', function(e) {
// remove the li for the tag form
$tagFormLi.remove();
});
}
});
</script>
when i update entity and i not update field file i have field file in database null ... how to update entity without changed in input file
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.
I deployed a Symfony2 app, but all the forms have empty csrf tokens. This html comes from firebug. As you can see, the tag is properly rendered, but no value is assigned.
<input type="hidden" value="" name="category[_token]" id="category__token">
In my controller:
$form = $this->createForm(new CategoryForm(), new Category());
$form->handleRequest($request);
if ($form->isValid()) {
(...)
}
In the template, the form is inside of a bootstrap 3 modal.
{{ form_start(form) }}
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Voeg een tariefplan toe</h4>
</div>
<div class="modal-body">
<div class="form-group">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
</div>
<div class="form-group">
{{ form_label(form.parent) }}
{{ form_widget(form.parent, {'attr':{ 'class': 'form-control' }}) }}
</div>
<div class="form-group">
{{ form_label(form.sizes) }}
{{ form_widget(form.sizes, {'attr':{ 'class': 'form-control' }}) }}
</div>
<div class="form-group">
{{ form_label(form.description) }}
{{ form_widget(form.description, {'attr':{ 'class': 'form-control' }}) }}
</div>
{{ form_widget(form._token) }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Annuleren</button>
{{ form_widget(form.save, {'attr':{ 'class': 'btn btn-primary' }}) }}
</div>
</div>
</div>
{{ form_end(form) }}
I also created a gist for the CategoryForm class : http://goo.gl/6NWTkB.
Anyone who knows what I'm missing here?
You must provide the value:
<input type="hidden" value={{ csrf_token('category') }}"" name="category[_token]" id="category__token">
A couple of ideas:
{{ form_end(form) }} should output your token so there's no need to have: {{ form_widget(form._token) }}.
If you have a very large form php might truncate your request vars. Have a look at max_input_vars in your php.ini. Default is 1000 I believe.
I have a form with a repeated field:
$builder->add('password', 'repeated', array( 'type' => 'password' ));
I want this repeated field to render differently from the other fields - how do I do that? I'm new to Symfony and twig, so if you have suggestions with code, please add some information as to where to put the code.
My form.html.twig looks like this:
{{ form_widget(form) }}
Thanks in advance.
This is how i display my repeated field using twitter bootstrap, of course you can change those classes to the one you are using
<form action="{{ path('passwordReset') }}" method="post" role="form">
{{ form_errors(form) }}
<div class="login-screen">
<h4>Reset Your Password</h4>
<div class="login-form">
<div class="form-group">
{{ form_widget(form.password.first, { 'attr': {'class': 'form-control', 'placeholder': 'Enter your password', 'value':''} }) }}
{% if(form_errors(form.password.first)) %}
<div class="alert alert-danger">{{ form_errors(form.password.first) }}</div>
{% endif %}
<label class="login-field-icon fui-lock" for="login-password"></label>
</div>
<div class="form-group">
{{ form_widget(form.password.second, { 'attr': {'class': 'form-control', 'placeholder': 'Confirm your password', 'value':''} }) }}
{% if(form_errors(form.password.second)) %}
<div class="alert alert-danger">{{ form_errors(form.password.second) }}</div>
{% endif %}
<label class="login-field-icon fui-lock" for="login-name"></label>
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit">Submit</button>
<a class="login-link" href="{{ path('login') }}">Sign in</a>
</div>
</div>
{{ form_rest(form) }}
</form>
What you need are the following two
{{ form_widget(form.password.first, { 'attr': {'class': 'form-control', 'placeholder': 'Enter your password', 'value':''} }) }}
{{ form_widget(form.password.second, { 'attr': {'class': 'form-control', 'placeholder': 'Confirm your password', 'value':''} }) }}
Just assign them the class you want to assign them to make them look different.
Helo
'first_options' => array('label' => 'form.password','attr' => array('class' => 'mystyle'))
something like that in the formType, it add a class to your input element and let you customize.
I have a problem with displaying errors with twig.
The errors are displayed in the last form_error with twig and I do not know how to fix it to display each error in his field.
I use annotation for constraints in my entity like * #Assert\True(message = "This field is empty")
{% trans_default_domain 'FOSUserBundle' %}
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register" novalidate="novalidate">
{{ form_errors(form) }}
<article class="form-part part1">
<h2>1 - Information pour la création du compte</h2>
<label for="{{ form.pays.vars.id }}">Pays <span>*</span> : </label>
{{ form_widget(form.pays) }}
{{ form_errors(form.pays) }}
<label for="{{ form.username.vars.id }}">Pseudo <span>*</span> : </label>
{{ form_widget(form.username) }}
{{ form_errors(form.username) }}
<label for="{{ form.email.vars.id }}">Email <span>*</span> : </label>
{{ form_widget(form.email) }}
{{ form_errors(form.email) }}
{{ form_row(form.plainPassword) }}
<div class="birthday">
{{ form_row(form.birthday) }}
</div>
{{ form_row(form.nationalite) }}
</article>
<article class="form-part">
<h2>2 - Télécharger les 6 images de votre galerie</h2>
<div>
{{ form_row(form.images, {'label': ' '}) }}
</div>
</article>
<div class="bottom-form">
<div class="contrat-cgu">
<div>
{{ form_label(form.certificatAge) }}
{{ form_widget(form.certificatAge) }}
{{ form_errors(form.certificatAge) }}
Lire les Conditions générales d'utilisation
</div>
<div>
{{ form_label(form.artistContract) }}
{{ form_widget(form.artistContract) }}
{{ form_errors(form.artistContract) }}
Lire le Contrat artiste
</div>
</div>
{{ form_row(form.how) }}
{{ form_rest(form) }}
</div>
If someone has an idea