symfony- update entity and changed fields - php

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

Related

CKEditor shows me a textarea inside text input

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.

Symfony - How to format an entity field in a form (to display more fields than just title)?

I have a form where user can submit a new Mandate. Every Mandate can have multiple Profiles linked to it.
Here is how the form looks like at this moment:
The Profiles field is added to the form like that (from MandateController):
$form = $this->createFormBuilder($mandate)
->add('content')
->add('name')
->add('company_name')
->add('budget')
->add('phone')
->add('email')
->add('description')
->add('profiles', EntityType::class, array(
'multiple' => true,
'expanded' => true,
'class' => 'KrownDashboardBundle:Profile',
'choice_label' => 'title',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.title', 'ASC');
},
))
->getForm();
And the view is handled this way:
{{ form_start(form) }}
{{ form_errors(form) }}
<div>
<div class="col_full">
<label for="" class="control-label">{{ form_label(form.content) }}</label>
{{ form_widget(form.content, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.content) }}
</div>
<div class="col_full">
<label for="" class="control-label">Prénom et nom</label>
{{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.name) }}
</div>
<div class="col_full">
<label for="" class="control-label">Nom de lentreprise</label>
{{ form_widget(form.company_name, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.company_name) }}
</div>
<div class="col_full">
<label for="" class="control-label">Budget</label>
{{ form_widget(form.budget, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.budget) }}
</div>
<div class="col_full">
<label for="" class="control-label">Numéro de téléphone</label>
{{ form_widget(form.phone, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.phone) }}
</div>
<div class="col_full">
<label for="" class="control-label">E-mail</label>
{{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.email) }}
</div>
<div class="col_full">
<label for="" class="control-label">Description</label>
{{ form_widget(form.description, {'attr': {'class': 'form-control'}}) }}
{{ form_errors(form.description) }}
</div>
<div class="col_full">
<label for="" class="control-label">Profiles</label>
{{ form_widget(form.profiles) }}
{{ form_errors(form.profiles) }}
</div>
</div>
<button id="login-form-submit" class="button button-3d button-black nomargin" value="Log in" name="login-form-submit">Ok</button>
{{ form_end(form) }}
I want to modify the Profiles listing, so they look like this (all these fields are saved into the Profile entity):
Instead:
How can I achieve that? What's the best way to do it?

How can i have the form text empty?

I dont know the way to leave the the form text empty is there is no result.I need something like this.....
If there is no userCreator = form text empty
My code
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(isset($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else{ {{-- What should i put inside here ? --}}
}
#endif
</div>
</div>
#if(isset($userCreator) && count($userCreator) > 0)
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
</div>
</div>
#endif
try this ...
<div class="col-md-4">
<div class="form-group">
{{ Form::label('userCreator','UserCreator') }}
#if(!empty($userCreator))
#foreach($userCreator as $cs)
{{ Form::text('userCreator',$cs->home_lastname,['class'=>'form-control']) }}
#endforeach
#else
{{ Form::text('userCreator','', ['class'=>'form-control']) }}
#endif
</div>
</div>

Symfony2 - Empty CSRF token

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.

Symfony change style of 'repeated' field

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.

Categories