How to set default selected option for Entity Type select? - forms

so I'm trying to set a selected option in my form but I can't seem to find out how to do this. I've Googled around and everything seems to be for Symfony2 where default was a thing, this seems to be no longer the case for Symfony4.
I've tried using data and empty_data but both don't select the correct value..
# weirdly, setting to ['guru'] gets undefined index error,
# setting to $options doesn't error
->add('guru', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username',
'data' => $options['guru']
))
and how I pass $options:
$form = $this->createForm(EditCategoryType::class, array('guru' => $guruName));

So with the help of #Juan I. Morales Pestana I found an answer, the only reason I've added the below as an answer rather than marking his as correct was because there seems to be a slight difference in how it works now..:
Controller now reads (Thanks to #Juan):
$category = $this->getDoctrine()->getRepository(Category::class)->find($id);
$category->setGuru($category->getGuru());
$form = $this->createForm(EditCategoryType::class, $category);
My EditCategoryType class:
->add('guru', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username',
'mapped' => false,
'data' => $options['data']->getGuru()
))
updated twig template:
{{ form_widget(form.guru, { 'attr': {'class': 'form-control'} }) }}

<a href="{{ path('register_new', { idpackage: p.id }) }}" class="btn_packages">
//sends to form type the package id
$fromPackage = '';
if($request->query->get('idpackage')) {
$fromPackage = $request->query->get('idpackage');
}
$form = $this->createForm(RegisterFormType::class, $register, ['fromPackage' => $fromPackage]);
in formType set the param in options:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Register::class,
'fromPackage' => null
]);
}
->add('package',EntityType::class, [
'label' => 'Select one item',
'class' => Package::class,
'choice_label' => function($package) {
return $package->getTitle() . ' | crédits : ' . $package->getAmount();
},
'attr' => ['class' => 'input-text', ],
'placeholder' => '...',
'required' => false,
'choice_attr' => function($package) use ($idFromPackage) {
$selected = false;
if($package->getId() == $idFromPackage) {
$selected = true;
}
return ['selected' => $selected];
},
])

$parentCategory = $categoryRepository->repository->find(2);
$category = new Category();
$category->setParentCategory(parentCategory);
$form = $this->createForm(CategoryType::class, $category);
we chose the predefined top category, it works if you use it this way.

try this :
empty_data documentation

As I said in my comments you are doing something wrong. I will explain all the process:
calling the form in the controller
$person = new Person();
$person->setName('My default name');
$form = $this->createForm('AppBundle\Form\PersonType', $person);
$form->handleRequest($request);
then the createForm function is executed here is the code
Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait
protected function createForm($type, $data = null, array $options = array())
{
return $this->container->get('form.factory')->create($type, $data, $options);
}
As you can see the data or options are nos setted directly in the form, another function is called and then the form is created
This is the result when I make a dump inside the PersonType
PersonType.php on line 20:
array:35 [▼
"block_name" => null
"disabled" => false
"label" => null
"label_format" => null
"translation_domain" => null
"auto_initialize" => true
"trim" => true
"required" => true
"property_path" => null
"mapped" => true
"by_reference" => true
"inherit_data" => false
"compound" => true
"method" => "POST"
"action" => ""
"post_max_size_message" => "The uploaded file was too large. Please try to upload a smaller file."
"error_mapping" => []
"invalid_message" => "This value is not valid."
"invalid_message_parameters" => []
"allow_extra_fields" => false
"extra_fields_message" => "This form should not contain extra fields."
"csrf_protection" => true
"csrf_field_name" => "_token"
"csrf_message" => "The CSRF token is invalid. Please try to resubmit the form."
"csrf_token_manager" => CsrfTokenManager {#457 ▶}
"csrf_token_id" => null
"attr" => []
"data_class" => "AppBundle\Entity\Person"
"empty_data" => Closure {#480 ▶}
"error_bubbling" => true
"label_attr" => []
"upload_max_size_message" => Closure {#478 ▶}
"validation_groups" => null
"constraints" => []
"data" => Person {#407 ▼ // Here is the data!!!
-id: null
-name: "My default name"
-salary: null
-country: null
}
]
As you can see the data is indexed at data so that is the reason why you get an undefined index error
So the proper way is to set the entity value from the controller or use your form as a service and call the repository and set the value using the data option which has not changed since version 2. My point is that you are using the form wrong.
Please change your code.
Hope it help
EDITED IN THE SYMFONY 4 WAY(with out namespaces bundles)
Let's see please, I have a Person Entity with a name just for this example
The controller
$person = new Person(); //or $personsRepository->find('id from request')
$person->setName('My default name');
$form = $this->createForm(PersonType::class, $person);
$form->handleRequest($request);
The form
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,[
//'data' => 'aaaaa' // <= this works too
]);
}
The options array value which proves that you are accessing wrong
PersonType.php on line 20:
array:35 [▼
"block_name" => null
"disabled" => false
"label" => null
"label_format" => null
"translation_domain" => null
"auto_initialize" => true
"trim" => true
"required" => true
"property_path" => null
"mapped" => true
"by_reference" => true
"inherit_data" => false
"compound" => true
"method" => "POST"
"action" => ""
"post_max_size_message" => "The uploaded file was too large. Please try to upload a smaller file."
"error_mapping" => []
"invalid_message" => "This value is not valid."
"invalid_message_parameters" => []
"allow_extra_fields" => false
"extra_fields_message" => "This form should not contain extra fields."
"csrf_protection" => true
"csrf_field_name" => "_token"
"csrf_message" => "The CSRF token is invalid. Please try to resubmit the form."
"csrf_token_manager" => CsrfTokenManager {#457 ▶}
"csrf_token_id" => null
"attr" => []
"empty_data" => Closure {#480 ▶}
"error_bubbling" => true
"label_attr" => []
"upload_max_size_message" => Closure {#478 ▶}
"validation_groups" => null
"constraints" => []
"data" => Person {#407 ▼
-id: null
-name: "My default name" //Here is the data
-salary: null
-country: null
}
]
AppBundle is just another folder in my folder structure. Be pleased to test your self. The second parameter to the formCreate function is the entity or data not the options you are thinking that the options is the data.

Related

Radio button: input was not found in the haystack?

Whenever I submit the form I get this message:
The input was not found in the haystack.
This is for the shipping-method element (radio button). Can't figure out what it means, the POST data for that element is not null.
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
// Some other basic filters
$inputFilter->add(array(
'name' => 'shipping-method',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim')
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 20,
),
),
array(
'name' => 'Db\RecordExists',
'options' => array(
'table' => 'shipping',
'field' => 'shipping_method',
'adapter' => $this->dbAdapter
)
),
),
));
$inputFilter->get('shipping-address-2')->setRequired(false);
$inputFilter->get('shipping-address-3')->setRequired(false);
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
I only keep finding solutions for <select>.
Here's the sample POST data:
object(Zend\Stdlib\Parameters)#143 (1) {
["storage":"ArrayObject":private] => array(9) {
["shipping-name"] => string(4) "TEST"
["shipping-address-1"] => string(4) "test"
["shipping-address-2"] => string(0) ""
["shipping-address-3"] => string(0) ""
["shipping-city"] => string(4) "TEST"
["shipping-state"] => string(4) "TEST"
["shipping-country"] => string(4) "TEST"
["shipping-method"] => string(6) "Ground"
["submit-cart-shipping"] => string(0) ""
}
}
UPDATE:
form.phtml
<div class="form-group">
<?= $this->formRow($form->get('shipping-method')); ?>
<?= $this->formRadio($form->get('shipping-method')
->setValueOptions(array(
'Ground' => 'Ground',
'Expedited' => 'Expedited'))
->setDisableInArrayValidator(true)); ?>
</div>
ShippingForm.php
$this->add(array(
'name' => 'shipping-method',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'Shipping Method',
'label_attributes' => array(
'class' => 'lbl-shipping-method'
),
)
));
The problem lies with when you use the setValueOptions() and the setDisableInArrayValidator(). You should do this earlier within your code as it is never set before validating your form and so the inputfilter still contain the defaults as the InArray validator. As after validation, which checks the inputfilter, you set different options for the shipping_methods.
You should move the setValueOptions() and the setDisableInArrayValidator() before the $form->isValid(). Either by setting the right options within the form itsself or doing this in the controller. Best way is to keep all of the options in one place and doing it inside the form class.
$this->add([
'name' => 'shipping-method',
'type' => 'Zend\Form\Element\Radio',
'options' => [
'value_options' => [
'Ground' => 'Ground',
'Expedited' => 'Expedited'
],
'disable_inarray_validator' => true,
'label' => 'Shipping Method',
'label_attributes' => [
'class' => 'lbl-shipping-method',
],
],
]);
Another small detail you might want to change is setting the value options. They are now hardcoded but your inputfilter is checking against database records whether they exist or not. Populate the value options with the database records. If the code still contains old methods but the database has a few new ones, they are not in sync.
class ShippingForm extends Form
{
private $dbAdapter;
public function __construct(AdapterInterface $dbAdapter, $name = 'shipping-form', $options = [])
{
parent::__construct($name, $options)
// inject the databaseAdapter into your form
$this->dbAdapter = $dbAdapter;
}
public function init()
{
// adding form elements to the form
// we use the init method to add form elements as from this point
// we also have access to custom form elements which the constructor doesn't
$this->add([
'name' => 'shipping-method',
'type' => 'Zend\Form\Element\Radio',
'options' => [
'value_options' => $this->getDbValueOptions(),
'disable_inarray_validator' => true,
'label' => 'Shipping Method',
'label_attributes' => [
'class' => 'lbl-shipping-method',
],
],
]);
}
private function getDbValueOptions()
{
$statement = $this->dbAdapter->query('SELECT shipping_method FROM shipping');
$rows = $statement->execute();
$valueOptions = [];
foreach ($rows as $row) {
$valueOptions[$row['shipping_method']] = $row['shipping_method'];
}
return $valueOptions;
}
}
Just had this happen yesterday.
The select and multi select ZF2+ elements have a built in in_array validator.
Remember filters occur before validators.
You may be doing too much here -- it is very rare to need to filter or add validators ot select and multi select form elements in ZF2 forms. The built in element validator is robust, ZF does a lot of work for us.
Try removing both filter and validator for the element, such as:
$inputFilter->add(array(
'name' => 'shipping-method',
'required' => true,
));
There is another edge case that I have seen: changing the select element's valueOptions somewhere in the controller (or view) resulting in different valueOptions used in view vs form validation (in our case it was replacing the element with a new one before validation).
I think your problem lies in the fact you are adding your value options after the InArray validator has been set, hence the validator has no haystack.
Try this
$this->add(array(
'name' => 'shipping-method',
'type' => 'Zend\Form\Element\Radio',
'options' => array(
'label' => 'Shipping Method',
'label_attributes' => array(
'class' => 'lbl-shipping-method'
),
'value_options' => array(
'Ground' => 'Ground',
'Expedited' => 'Expedited'
),
'disable_inarray_validator' => TRUE,
)
));
and remove setValueOptions and setDisableInArrayValidator from your view.
Hope this works.

Symfony2 Form entity field with query_builder

I have a form with an entity field, without the query_builder work fine, but with the query_builder the form display the result of the query but is not valid when is submited.
//tecnicosType
$builder->add('dt', 'entity', array(
'class' => 'MSKLigaBundle:Jugadores',
'label' => 'DT',
'query_builder' => function(
\MSK\LigaBundle\Entity\JugadoresRepository $er) use($equipoId){
return $er->getJugadores($equipoId);
},
'property' => 'nombreCompleto',
'empty_value' => "Sin definir",
'required' => false
))
And the function is
public function getJugadoresQueryBuilder($equipo)
{
$queryBuilder = $this->createQueryBuilder('j');
return $queryBuilder->select('j')
->where('j.equipo_id = :equipo')
->setParameter('equipo', $equipo);
}
when the form is submited return
array
'dt' =>
array
0 => string 'This value is not valid.'
If i coment the query_builder the form validate fine.
I can't find the solution, thank for any help.

zend 2 form filters not working

I'm trying to use some Zend\Filter with forms to ensure that my data is correctly formatted.
I have a typical input (text) named 'parent_id' which I apply some filters :
'filters' => array(
array('name' => 'Zend\Filter\Int'),
array('name' => 'Zend\Filter\Null', 'options' => array('type' => \Zend\Filter\Null::TYPE_INTEGER)),
),
After that, I use a custom validator on another field which needs to check the 'parent_id' value via the $context.
public function isValid($value, $context = null) {
var_dump($context);
}
When I dump the var $context, I get this:
array(2) {
["parent_id"]=> string(0) ""
["target_id"]=> string(1) "1"
}
I checked the execution order and my filters are running before the validator, so why I don't get :
["parent_id"]=> NULL OR ["parent_id"]=> INT(something) ?
Thank you for your help !

Zend Framework validation: set element value to empty string when there is no data in POST

I'm sorry if my question is a bit dumb, but I have looked at other questions/Googled this and still didn't get it.
I have a form with default and custom elements:
class Form_Client extends Planner_Form {
public function init() {
$this->addPrefixPath('Planner_Form_Element', 'Planner/Form/Element', 'element');
$this->setAttrib('name', 'clientForm');
$this->addElement('text', 'phone', array(
'label' => 'Телефон',
'required' => true,
'filters' => array('Digits'),
'ng-model' => 'clientForm.phone',
));
$this->addElement('text', 'extra_phone_1', array(
'label' => 'Дополнительный телефон',
'required' => false,
'filters' => array(
array('Digits'),
),
'ng-model' => 'clientForm.extra_phone_1',
));
$this->addElement('text', 'name', array(
'label' => 'Имя',
'required' => false,
'ng-model' => 'clientForm.name',
));
$this->addElement('datetime', 'birthday', array(
'label' => 'Дата рождения',
'required' => false,
'ng-model' => 'clientForm.birthday',
));
I send form via AngularJs, and when I check it
if ($request->isPost()) {
$body = $this->getRequest()->getRawBody();
$data = Zend_Json::decode($body);
Zend_Debug::dump($data);
$form = new Form_Client();
if ($form->isValid($data)) {
$values = $form->getValues();
Zend_Debug::dump($values);
}
I get the following:
array(1) {
["phone"] => string(10) "9138521376"
}
array(4) {
["phone"] => string(10) "9138521376"
["extra_phone_1"] => string(0) ""
["name"] => NULL
["birthday"] => NULL
}
So my question is: why extra_phone_1 field gets empty string, and name gets NULL? Is it because of filter on extra_phone_1 field? If so, how can I set field value to empty string, when there is no data in POST for this field?
You are right, the value is'' due Digits filter and null without this filter.
To have a non-null value in the name field, for example, you can use the same technique and try to put the filter 'StringTrim'

Magento, add and set a checkbox on grid and form backend

I've a fully working backend page with a grid and a corresponding form to edit the changes on the corresponding model. I added a new field on the table, bit type, as it will answer to a yes/no configuration option from the user. I added the checkbox on both grid and form.
My problem is that after a couple of hours of searching and trying different approaches I can not set the checkbox checked value both on the grid and the form reading the corresponding field from the database. Also when I click on save on the form the value corresponding to the checkbox is always saved with 1. Everything else on the grid and the form works as it should. I have read here, here, here, here and some more sites and SO questions/answers but still no clue on what I'm doing wrong. Some solutions recommend using a combo box with YES/NO options, but I want a checkbox, can't be so difficult.
Grid code inside the function _prepareColumns():
protected function _prepareColumns() {
...
$this->addColumn('banner_gral', array(
'header' => Mage::helper('banners')->__('General'),
'align' => 'center',
'index' => 'banner_gral',
'type' => 'checkbox',
'values' => $this->getBannerGral()==1 ? 'true' : 'false',
));
...
}
public function __construct()
{
parent::__construct();
$this->setId('bannersgrid');
$this->setDefaultSort('bannerid');
$this->setDefaultDir('asc');
$this->setSaveParametersInSession(true);
$this->setUseAjax(true);
}
public function getGridUrl()
{
return $this->getUrl('*/*/grid', array('_current'=>true));
}
protected function _prepareCollection()
{
$collection = Mage::getModel('banners/bannersadmin')->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
Form code to add the checkbox inside the function _prepareForm():
protected function _prepareForm()
{
$id = $this->getRequest()->getParam('id');
$params = array('id' => $this->getRequest()->getParam('id'));
if (Mage::registry('banners_data')->getdata()) {
$data = Mage::registry('banners_data')->getdata();
}
elseif (Mage::getSingleton('adminhtml/session')) {
$data = Mage::getSingleton('adminhtml/session')->getdata();
Mage::getSingleton('adminhtml/session')->getdata(null);
}
else {
$data = array();
}
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', $params),
'method' => 'post',
'enctype' => 'multipart/form-data',
));
...
$fieldset->addField('banner_gral', 'checkbox', array(
'label' => Mage::helper('banners')->__('Is general'),
'name' => 'banner_gral',
'class' => 'banner_gral',
'checked' => $this->getBannerGral()==1 ? 'true' : 'false',
'onclick' => 'this.value == this.checked ? 1 : 0',
'note' => Mage::helper('banners')->__('blablablabla'),
'tabindex' => 2
));
...
}
On the saveAction() of my form I have:
$campaign->setbanner_gral(!empty($data['banner_gral']));
In your controller saveAction() when saving the checkbox data do
$banner_gral = isset($your_form_Data['banner_gral']) ? 1 : 0;
For Grid and Form Page
In your controller you should have Mage::register(...)->getData() or Mage::register(...)
public function editAction()
....
Mage::register('example_data', $model);
On your form _prepareForm()
$model = Mage::registry('example_data'); // NOTE registry('example_data'); NOT registry('example_data')->getData();
$fieldset->addField('entire_range', 'checkbox', array(
....
'checked' => $model->getBannerGral()==1 ? 'true' : 'false',
......
))
see http://www.magentocommerce.com/boards/viewthread/20536/
On your grid _prepareColumns()
$this->addColumn('banner_gral', array(
....
'type' => 'checkbox',
'index' => 'banner_gral',
'values' => array(1,2),
'field_name' => 'checkbox_name',
....
));
#R.S answered one issue, how to save the checkbox value on the corresponding model/database field. But the issue on how to correctly display the checkbox on both the grid and the form was not solved. After doing some more searches I finally got to these two links that helped me solve my problem.
To correct the grid issue: Understanding the Grid Serializer Block
Now the part of code where the checkbox column is added, see that I added array(1,2) on the values element.
$this->addColumn('banner_gral', array(
'header' => Mage::helper('banners')->__('General'),
'width' => '20px',
'type' => 'checkbox',
'align' => 'center',
'index' => 'banner_gral',
'values' => array(1,2),
'editable' => 'false',
));
Also if you take a look into the core code of Magento, the class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Checkbox returns an array of values. Taking a look here finally got me into the right path.
/**
* Returns values of the column
*
* #return array
*/
public function getValues()
{
if (is_null($this->_values)) {
$this->_values = $this->getColumn()->getData('values') ? $this->getColumn()->getData('values') : array();
}
return $this->_values;
}
To correct the form issue: Mage_Adminhtml_Block_System_Store_Edit_Form Class Reference
The issue on this case was that I was trying to use the $this but what I needed to use was the $data that is loaded at the beginning of the _prepareForm function. #R.S pointed the right direction, but it is not possible to use $model->getBannerGral() as the $data on the registry is an array, not a model. So, using $data["banner_gral"] I could get the needed value for the checkbox. Tested and it is working.
$fieldset->addField('banner_gral', 'checkbox', array(
'label' => Mage::helper('banners')->__('Is general'),
'name' => 'banner_gral',
'checked' => $data["banner_gral"],
'onclick' => 'this.value = this.checked ? 1 : 0;',
'note' => Mage::helper('banners')->__('blablablabla'),
'tabindex' => 2
));