Symfony 2 Create a entity form field with 2 properties - php

I am using symfony2 and have a form to save the relation of one user to some rules. These rules are set by the admin user of the company. In this form, after I selected a user to update, I have to select which rule this user have permission.
The problem is that I may have more then one rule with the same name (it's another entity) but the values are different. So, when I build the selectbox I must show the name and the value like:
Quantity of items - 10
Quantity of items - 20
Value of the item - 200
Value of the item - 500
But now I just can show without the "- $value" using the code bellow:
$form = $this->createFormBuilder()->add('myinput', 'entity', array(
'class' => 'myBundle:Rule',
'property' => 'childEntity.name',
'label' => 'Filas Permitidas',
'expanded' => false,
'multiple' => true,
'choices' => $this->getDoctrine()
->getRepository('MyBundle:Rule')
->findAll(),
'required' => true,
))->getForm();
So, as property I wanted to get $myEntity->getChildEntity()->getName() and the $myEntity->getValue().
Is there some way to do this?

Yes, define a getUniqueName() method in the entity class like:
public function getUniqueName()
{
return sprintf('%s - %s', $this->name, $this->value);
}
And edit the property form option:
'property' => 'childEntity.uniqueName',
You also can omit the property option and define the __toString() method same way in order to not repeat the setting of the property option in every form.

Related

How to save multiple values from select2_from_ajax_multiple in Backpack laravel

everyone
I am tring to save the Multiple values from select option, but i don't have knowledge about how to save in laravel Backpack .I have been trying to solve the error from 2 days, but still it's not working from my side.
Please have a look, help would be appreciated.
Here is Screen shot of multiple select option ---
But it does not saving in db, but if i am going to edit the record , it gives this error ---
Here is my controller code
CRUD::addField([ // select2_from_ajax: 1-n relationship
'label' => "City Name", // Table column heading
'type' => 'select2_from_ajax_multiple',
'name' => 'location_id', // the column that contains the ID of that connected entity;
'entity' => 'location', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'tab' => 'Texts',
'data_source' => url('api/location'), // url to controller search function (with /{id} should return model)
'placeholder' => 'Select location', // placeholder for the select
'include_all_form_fields' => true, //sends the other form fields along with the request so it can be filtered.
'minimum_input_length' => 0, // minimum characters to type before querying results
'dependencies' => ['state'], // when a dependency changes, this select2 is reset to null
// 'method' => 'GET', // optional - HTTP method to use for the AJAX call (GET, POST)
"allows_multiple" => true,
'pivot' => true,
]);

how to add custom attribute in catalog/category in magento 1.9

how to add custom attribute in catalog/category in admin general tab which will display only on specific store not for all.
$installer = $this;
$installer->startSetup();
$installer->addAttribute(Mage_Catalog_Model_Category::ENTITY,
'banner_img1',
array(
'group' => 'General',
'type' => 'text',
'label' => 'Door Banner Image1',
'visible' => true,
'required' => false,
'visible_on_front' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$installer->endSetup();`
but above code create attribute in all store view i want to show only for 1 store
Magento attributes do not work that way. Store scope means that attribute can have different value for each store view, but you can't have it exist for one and not for other.
Even tho there's no way to add an attribute only for one store view, you can upload the image only for particular store view and add the check in your theme which will output the attribute only if it's present.

Symfony Sonata project: How to add multiple input texts to block?

I would like to add a collection of input text with same name (i.e. name="blabla[]") filed to admin block with add/delete buttons.
I'm using collection form field type but can't see add/delete buttons
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('title', 'collection',
array('type' => 'text' ,
'required' => true,
'allow_add' => true,
'data' => array('First' => 'One')
)
)
)
));
}
I get below result without add/delete buttons!
Any idea how to get it working ?
I think you should use sonata_type_collection or sonata_type_native_collection instead of collection.
Here is an extract of the field doc :
14.1.7. SONATA_TYPE_NATIVE_COLLECTION (PREVIOUSLY COLLECTION)
This bundle handle the native Symfony collection form type by adding:
an add button if you set the allow_add option to true. a delete button
if you set the allow_delete option to true.

other in entity choice list form

I'm new to symfony. I have a drop down in a form with data fetched from DB.
$builder->add('category', 'entity', array(
'label' => 'category',
'class' => 'MyBundle:category',
'expanded' => false,
'multiple' => false,
'mapped' => false,
'empty_value' => 'category'
));
$builder->add('other_category', 'text', array(
'label' => 'category',
'required' => false,
'invalid_message' => 'Please enter a valid category',
'mapped' => false,
));
the user can also add new category to the table. when other is selected from drop down, the 'other_category' input field is shown, else its hidden.
'Other' was added to drop down with the help of this code.
public function finishView(FormView $view, FormInterface $form, array $options)
{
$new_choice = new ChoiceView(array(), 'other', 'Other');
$view->children['category']->vars['choices'][] = $new_choice;
}
If a option is selected from drop down the form works fine. Data gets stored without any error. But if user selects 'other' and enters a new category the page reloads with 'This value is no valid' under the category options and there is no form validation for the 'other_category' entered by user.
Can someone help me with the form validation and also entering of a new category or suggest a better way to implement the above functionality.
The validation error is happening because the form field type is Entity, but there is no "MyBundle:category" entity with the identifying value "other".
You've not specified the "choice_label" property in your Entity form type so I'll assume your "MyBundle:category" entity has a __toString() function. This would mean none of the "MyBundle:category" entities return "other" in their __toString() function.
I can think of two options to work around this right now:
1) Add a "MyBundle:category" entity with value "other". This is the easiest way, but it's fairly assumed you don't want such a category to exist in your database.
2) Load the list of "MyBundle:category" entities in advance from your controller, build them into an associative array, append your "other" option to the array, then pass that array to the form. You'd need to swap the Entity form type for a Choice type and use the categories array as the choices.
If your form is a FormType class you'll need to pass the array in with the class constructor.
If you don't mind having a new category with an "other" value in your category table, just add it. Otherwise go for option 2, which won't make much difference to what you do after receiving the submitted form as this form field isn't mapped to an entity property anyway.

How to add dynamically-created sub-forms as a collection in Symfony/Silex FormBuilder?

I'm trying to dynamically generate a form based on user-provided field definitions. This is fairly straightforward for the basic form:
$builder = $app['form.factory']->createBuilder('form', $my_data);
foreach ($my_user_provided_field_definitions as $field) {
$builder->add($field->handle, $field->type, $field->options);
}
$form = $builder->getForm();
But I also want to have sub-forms using the collection field type, and while it is so easy to dynamically generate the top-level form as I've done above, it does not seem possible to dynamically generate a sub-form for the collection field because all of the docs and sample code I've come across utilize either a single field like so:
$builder->add('my field', 'collection', array(
'type' => 'text',
'allow_add' => true,
));
...or a custom "Type" object, like so:
$builer->add('my field', 'collection', array(
'type' => new TagType(),
'allow_add' => true,
));
The TagType class in the above example must be defined in code (see http://symfony.com/doc/master/cookbook/form/form_collections.html )... but my problem is that I cannot have the class defined in code because these sub-forms are dynamically generated based on user data -- so I do not know what fields the sub-form will contain until run-time!
Is there a way to dynamically generate the sub-forms that can then be passed in to the top-level form, or is this a use case that is not handled by the Symfony FormBuilder?

Categories