SonataAdminBundle do not escape input fields - php

I am trying to insert a query into an input field and also html code but it gets escaped, this is what i currently use:
class ShopAdmin extends Admin{
protected $datagridValues = array(
'_page' => 1, // display the first page (default = 1)
'_sort_order' => 'DESC', // reverse order (default = 'ASC')
'_sort_by' => 'website' // name of the ordered field
// (default = the model's id field, if any)
// the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
);
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name', 'text', array('label' => 'Item name'))
->add('description', 'textarea', array(
'label' => 'Item description',
'attr' => array(
'class' => 'redactor-init',
'style' => 'width: 683px;'
)
))
->add('amount', 'text', array('label' => 'Item price'))
->add('visible', 'checkbox', array('label' => 'Item visibility'))
->add('command', 'text', array('label' => 'Item command'))
->add('type', 'text', array('label' => 'Item type (SQL or COMMAND)'))
->add('image', 'text', array('label' => 'Item image'))
->add('reduction', 'text', array('label' => 'Item reduction'))
->add('reduction', 'text', array('label' => 'Item priority'))
->add('section', 'entity', array('class' => 'Maxim\CMSBundle\Entity\Section'))
//->add('server', 'entity', array('class' => 'Maxim\CMSBundle\Entity\Server'))
->add('website', 'entity', array('class' => 'Maxim\CMSBundle\Entity\Website'))
;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('name')
->add('visible')
->add('section')
//->add('server')
->add('website')
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('name')
->add('visible', 'boolean', array('editable' => true))
->add('section')
//->add('server')
->add('website')
;
}
}
However i am trying to make it not escape, i used the types textarea, string and text but none of them seem to correctly work
I need to have this inserted into the db:
"UPDATE `db_perks` SET life_boost=life_boost+10 WHERE name="{USER}";
but it escapes it to:
"UPDATE `db_perks` SET life_boost=life_boost+10 WHERE name=\"{USER}\";

Related

Sonata/AdminBundle: Selected Option

I'm new in Symfony and Sonata/AdminBundle. I would like to know how to mark selected an option when the entity has a field from other entity. For example: I have two entities: Shop and City. The Shop entity has a field called id_city.
My problem is when I'm rendering the edit form Shop because always the first id_city in the option is selected.
This is the piece of code where I'm rendering the configuration form in AdminStores class:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->tab('Tiendas')
->with('Content', array('class' => 'col-md-9'))
->add('nombreTienda', 'text')
->add('cifTienda', 'text')
->add('direccionTienda', 'text')
->add('personaContacto', 'text', array('required' => false,'empty_data' => ''))
->add('cp', 'text', array('label' => 'Código Postal', 'required' => false, 'empty_data' => '00000'))
->add('urlTienda', 'text', array('required' => false, 'empty_data' => ''))
->add('emailTienda', 'text')
->add('telefonoTienda', 'text')
->add('login', 'text')
->add('pass', 'password', array('required' => false))
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
->add('idProvincia', EntityType::class, array(
'class' => 'AppBundle:Provincias',
'label' => 'Provincia',
'choice_label' => 'provincia',
'choice_value' => 'getId',
'by_reference' => true,
))
->add('descripcionTienda', 'textarea')
->end()
->end()
->tab('Multimedia')
->with('Content', array('class' => 'col-md-3'))
->add('fotoTienda', 'file', array(
'label' => 'Imagenes (puedes subir hasta 6 imágenes)',
'attr' =>array('class' => 'form-control', 'multiple' => 'multiple', 'accept' => 'image/*'),
'data_class' => null,
'required' => false,
'empty_data' => 'noDisponible',
));
}
In this piece of code, I'm recovering all cities in AdminStores class:
->add('idMunicipio', 'entity', array(
'class' => 'AppBundle:Municipios',
'choice_label' => 'municipio',
'query_builder' => function (EntityRepository $er) {
$lista = $er->createQueryBuilder('ss')
->orderBy('ss.municipio', 'ASC');
},
'data' => $this->subject->getIdMunicipio()
)) // end array idMunicipio y add()
I would like to know, please, the logic for " if this->id_city == entity->id_city then, option is selected".
Thanks in advance
I edit this comment because I think that I solved it.
In my AdminController called ShopsAdmin I have created a method called getAllMunicipios which return an array with their name and id:
$allCities = array(
'Tokyo' => 1
'Madrid => 2
);
This is the method:
protected function getAllMunicipios()
{
$municipios = $this->getConfigurationPool()
->getContainer()
->get('doctrine')
->getRepository('AppBundle:Municipios')
->findBy([], ['municipio' => 'ASC']);
$todosmunicipios = array();
foreach ($municipios as $municipio) {
$todosmunicipios[(string)$municipio->getMunicipio()] = (int)$municipio->getId();
}
return $todosmunicipios;
}
Now my AdminStores::configureFormFields method like that this:
->add('idMunicipio', 'choice', array(
'choices' => $this->getAllMunicipios(),
'required' => false,
'by_reference' => false,
'data' => $this->subject->getIdMunicipio()
))
It is a good way to do it? I think that the method that return all, must be placed into the entity and not int the controller but I dont know how do it static
just call setCity(\AppBundle\Entity\City $city) in your Shop entity. and give the right city entity as the first and only parameter. Do this before you render the form

Symfony 3 - Best practice for concating form-blocks

I'm trying to learn how to build forms in symfony 3.
Following some tutorials I have built a PersonType
class PersonType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('gender', ChoiceType::class, array('label' => 'Anrede', 'choices' => array('Herr' => 'Herr', 'Frau' => 'Frau'), 'attr' => array('class' => 'form-control')))
->add('title', TextType::class, array('label' => 'Titel', 'attr' => array('class' => 'form-control')))
->add('firstname', TextType::class, array('label' => 'Vorname', 'attr' => array('class' => 'form-control')))
->add('lastname', TextType::class, array('label' => 'Nachname', 'attr' => array('class' => 'form-control')))
->add('birthdate', DateType::class, array('label' => 'Geburtsdatum', 'attr' => array('class' => 'form-control')))
->add('street', TextType::class, array('label' => 'Straße', 'attr' => array('class' => 'form-control')))
->add('streetnumber', TextType::class, array('label' => 'Hausnummer', 'attr' => array('class' => 'form-control')))
->add('zip', TextType::class, array('label' => 'PLZ', 'attr' => array('class' => 'form-control')))
->add('city', TextType::class, array('label' => 'Stadt', 'attr' => array('class' => 'form-control')))
->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control')));
}
public function getName() {
return 'person';
}
}
And some other types.
In the controller I have
$person = new Person();
$form = $this->createForm(PersonType::class, $person);
My question now is, how do I now concat the PersonType to some other Types to get one Form out of it? And how do I then set the submit-button?
You cannot concatenate but you can include a subset of fields in several forms.
Here you have a nice example in the Symfony documentation :
http://symfony.com/doc/current/cookbook/form/inherit_data_option.html
Recap :
Create a form with your subfields, with the option 'inherit_data' => true.
Use it in another form as field.
First of all, please note how you add fields to PersonType form, because it will be exactly the same.
->add('email', TextType::class, array('label' => 'E-Mail', 'attr' => array('class' => 'form-control')));
What you do here is adding a subform TextType. It actually contains single field, but it's still a form.
The same way you can add PersonType to any other form. That would be something like:
->add('person', PersonType::class, array(/* some options if needed*/);
And how do I then set the submit-button?
As mentioned in Best Practices for Symfony Forms, I would suggest to add them in template, not to the form object.

Adding extra options in Symfony2 EntityType always gets invalid when submitted

Hi I have successfully added an extra option in my Entity Type field in Symfony.
I have the following code:
class ReportFilterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
$builder
->setMethod('GET')
->add('users', 'entity', array(
'attr' =>
array(
'class' => 'form-control',
),
'expanded' => false,
'multiple' => false,
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.firstName', 'ASC');
},
))
->add('dateFrom', 'date', array(
'attr' =>
array(
'id' => 'dateFrom',
'placeholder' => 'From',
'class' => 'form-control',
'data-format' => "dd/MM/yyyy",
),
'widget' => 'single_text',
'html5' => false,
))
->add('dateTo', 'date', array(
'attr' =>
array(
'id' => 'dateTo',
'placeholder' => 'To',
'class' => 'form-control',
'data-format' => "dd/MM/yyyy",
),
'widget' => 'single_text',
'html5' => false,
))
->add('filterSubmit', 'submit', array(
'attr' => array('class' => 'btn btn-default'),
'label' => 'Filter'
))
->add('pdfSubmit', 'submit', array(
'attr' => array('class' => 'btn btn-default'),
'label' => 'Export to PDF'
));
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
$new_choice = new ChoiceView(new User(), 'all', 'All Employees'); // <- new option
$view->children['users']->vars['choices'][] = $new_choice;//<- adding the new option
}
public function getName()
{
return 'report_filter';
}
}
The problem here is, when I submitted my form and choose the extra option that I added it never gets valid. Why is that so? I cannot see where the problem is originating.
Thanks!

Symfony2 nothing happens when I display my form inside a class

So I am following the documentation and I am making a form inside its own class:
<?php
namespace Mp\ShopBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
class RegisterFormType extends AbstractType
{
public function registerForm(FormBuilderInterface $builder, array $options) {
$builder
>add('title', 'choice', array(
'choices' => array('-' => '-', 'mr' => 'Mr.', 'mrs' => 'Mrs.', 'mss' => 'Miss.'),
'label' => 'Title * ',
'attr' => array('class' => 'span1')))
->add('firstName', 'text', array(
'label' => 'First Name * ',
'attr' => array('placeholder' => 'First Name')))
->add('lastName', 'text', array(
'label' => 'Last Name * ',
'attr' => array('placeholder' => 'Last Name')))
->add('Email', 'email', array(
'label' => 'Email * ',
'attr' => array('placeholder' => 'Email')))
->add('Password', 'password', array(
'label' => 'Password * ',
'attr' => array('placeholder' => 'Password')))
->add('DateOfBirth', 'date', array(
'label' => 'Date Of Birth * ',
'widget' => 'choice'))
->add('Company', 'text', array(
'label' => 'Company ',
'attr' => array('placeholder' => 'Company')))
->add('Adress', 'text', array(
'label' => 'Adress * ',
'attr' => array('placeholder' => 'Adress')))
->add('Country', 'country', array(
'label' => 'Country * ',
'attr' => array('placeholder' => 'Country')))
->add('State', 'text', array(
'label' => 'State * ',
'attr' => array('placeholder' => 'State')))
->add('City', 'text', array(
'label' => 'City * ',
'attr' => array('placeholder' => 'City')))
->add('ZipPostalCode', 'text', array(
'label' => 'Zip / Postal Code *',
'attr' => array('placeholder' => 'Zip / Postal Code')))
->add('AdditionalInformation', 'textarea', array(
'label' => 'Additional Information ',
'attr' => array('placeholder' => 'Additional Information')))
->add('HomePhone', 'number', array(
'label' => 'Home phone ',
'attr' => array('placeholder' => 'Home Phone')))
->add('MobilePhone', 'number', array(
'label' => 'Mobile phone ',
'attr' => array('placeholder' => 'Mobile Phone')))
->add('save', 'submit', array('label' => 'Register'));
}
public function getName()
{
return 'register_form_users';
}
}
It looks like a simple form. Now in my controller I want to show it:
use Mp\ShopBundle\Form\Type\RegisterFormType;
public function registerAction()
{
$em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll();
$form = $this->createForm(new RegisterFormType());
return $this->render('MpShopBundle:Frontend:registration.html.twig', array(
'products'=>$products,
'form'=>$form->createView(),
));
}
My twig:
<h3>Your personal information</h3>
{{ dump(form) }}
{% form_theme form _self %}
{{ form(form) }}
The thing is im not getting my form. The page and the template loads fine, but not my form.
When I do {{ dump(form) }} I get something:
FormView {#2110 ▼
+vars: array:33 [▶]
+parent: null
+children: array:1 [▶]
-rendered: true
}
As you can see I am getting the form? But it is not displaying?... Why is that?
You must change your method
public function registerForm(FormBuilderInterface $builder, array $options) {
to
public function buildForm(FormBuilderInterface $builder, array $options)

Symfony2 form type entity add extra option

I have the following Symfony form field, it's a drop down that loads from an entity:
->add('measureunit', 'entity', array('label' => 'Measure Unit',
'class' => 'TeamERPBaseBundle:MeasureUnit',
'expanded' => false, 'empty_value' => '',
'multiple' => false, 'property' => 'abreviation'
))
As you can see I have added 'empty_value' => '' and everything works fine. Now, what I want is to have an extra option at the end to add a let say new measure unit. In other words the dropdown should display all the content of my entity, the empty value and other extra option called new measure unit or what ever I want to call it. Is it possible?
Edit: The whole form type file has this:
<?php
namespace TeamERP\StoresBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('label'=>'Product name', 'required' => true,
'attr' => array('class' => 'form-control')))
->add('code', 'text', array('label'=>'Code', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('description', 'text', array('label'=>'Description', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('cost', 'money', array('label'=>'Cost', 'divisor' => 100, 'currency' => 'BWP'))
->add('category', new CategoryType(), array('required' => false))
->add('measureunit', 'entity', array('label' => 'Measure Unit',
'class' => 'TeamERPBaseBundle:MeasureUnit',
'expanded' => false, 'placeholder' => '',
'multiple' => false, 'property' => 'abreviation'
))
->add('qtyToPurchase', 'number', array('label'=>'Quantity to purchase', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('reorderPoint', 'number', array('label'=>'Reorder point', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('qtyOnSalesOrder', 'number', array('label'=>'Quantity on sales order', 'required' => false,
'attr' => array('class' => 'form-control')));
}
public function getName()
{
return 'product';
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
$new_choice = new ChoiceView(array(), 'add', 'add new'); // <- new option
$view->children['measureunit']->vars['choices'][] = $new_choice;//<- adding the new option
}
}
Error:
Compile Error: Declaration of TeamERP\StoresBundle\Form\Type\ProductType::finishView() must be compatible with Symfony\Component\Form\FormTypeInterface::finishView(Symfony\Component\Form\FormView $view, Symfony\Component\Form\FormInterface $form, array $options)
Edit2 Working form file:
<?php
namespace TeamERP\StoresBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('label'=>'Product name', 'required' => true,
'attr' => array('class' => 'form-control')))
->add('code', 'text', array('label'=>'Code', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('description', 'text', array('label'=>'Description', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('cost', 'money', array('label'=>'Cost', 'divisor' => 100, 'currency' => 'BWP'))
->add('category', new CategoryType(), array('required' => false))
->add('measureunit', 'entity', array('label' => 'Measure Unit',
'class' => 'TeamERPBaseBundle:MeasureUnit',
'expanded' => false, 'placeholder' => '',
'multiple' => false, 'property' => 'abreviation'
))
->add('qtyToPurchase', 'number', array('label'=>'Quantity to purchase', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('reorderPoint', 'number', array('label'=>'Reorder point', 'required' => false,
'attr' => array('class' => 'form-control')))
->add('qtyOnSalesOrder', 'number', array('label'=>'Quantity on sales order', 'required' => false,
'attr' => array('class' => 'form-control')));
}
public function getName()
{
return 'product';
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
$new_choice = new ChoiceView(array(), 'add', 'add new'); // <- new option
$view->children['measureunit']->vars['choices'][] = $new_choice;//<- adding the new option
}
}
In your form type override the function finishView:
public function buildForm(FormbuilderInterface $builder, array $options){
$builder->add('measureunit', EntityType::class, array(
'label' => 'Measure Unit',
'class' => 'TeamERPBaseBundle:MeasureUnit',
'expanded' => false,
'empty_value' => '',
'multiple' => false,
'property' => 'abbreviation'
));
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
$newChoice = new ChoiceView(array(), 'add', 'Add New'); // <- new option
$view->children['measureunit']->vars['choices'][] = $newChoice;//<- adding the new option
}
You will get a new option 'add new' with value 'add' to the bottom of the field.
In addition to accepted answer:
If you choose the added option you will get validation error (because it's not valid entity), the following snippet can be used to overcome this error:
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) {
if ($event->getData() === 'add') {
$event->setData(null);
}
}
);
Then you can check if selected option is NULL, if it's => take value from additional input field.

Categories