i have entity languages with format
id; name
en; English
pl; Polish
etc...
And I have some Entity translations, that in one column has an associative array:
{pl: "Some txt in PL", en: "Some txt in EN",...}
Everything is almost perfect, but i don't know how to create an form for editing such a thing :D I tried almost everything.
Translation.orm.yml:
...
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
value:
type: array
...
For new Entity you must prepare your array in Controller ( before creating form). Something like that:
$value = array('en' => 'here en', 'pl' => 'here pl');
$translation->setValue($value);
$form = $this->createForm(new TranslationType()...
In Your FormType:
$builder->add('value', 'collection', array(
'type' => 'text',
'options' => array(
'required' => true,
),
)
);
That's all.
If you have {{ form_rest(form) }} in your twig, you'll see two additional fields for translations in your form and it will be work.
Additional information about collection field type is here
Related
Normally in the backpack crud controller we pass the column value like
$this->crud->addColumn([
'name' => 'name', // The db column name
'label' => "Username", // Table column heading
'type' => 'Text'
]);
$this->crud->addColumn([
'name' => 'age', // The db column name
'label' => "Age", // Table column heading
'type' => 'Number'
]);
and get the value in the backpack blade like
$crud->columns
output will come like
[
name: {name: "name", label: "Username", type: "Text", .....},
age: {name: "age", label: "Age", type: "Number", .....}
]
Above array index value are name, age. But i want it as 0, 1. How do i get index value as number?
As this is an array so we can use the PHP's array_values() function to re-index the array.
like as follow.
array_values($crud->columns);
or in case of using this value in laravel blade js section then you can use as follow
{!! json_encode(array_values($crud->columns)) !!};
Ref. link:- https://www.php.net/manual/en/function.array-values.php
I have a form in Symfony 2.7.10 which definition looks like this:
<?php
// ...
class RecipeType extends AbstractType
{
// ...
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('meal_schema', 'choice', [
'label' => 'Mealtime schema',
'choices' => [
'breakfast' => 'Breakfast',
'second_breakfast' => 'Second breakfast',
'lunch' => 'Lunch',
'dinner' => 'Dinner',
'snack' => 'Snack',
'supper' => 'Supper',
],
'multiple' => true,
'expanded' => true,
'label_attr' => ['class' => 'col-md-2'],
'required' => true,
])
}
// ...
}
This is how validation looks in validation.yml file:
My\Real\Namespace\Entity\Recipe:
properties:
name:
- NotBlank: ~
mealSchema:
# Look below
Name field validation works, but mealSchema validation doesn't.
Settings which I've already tried without success:
# This one works but assigns error to form instead of field so it's displayed on top of the page
- NotBlank:
message: Mealtime schema should not be blank
# This doesn't work
- Choice:
callback: [RecipeMealSchemaChoices, getChoiceKeys] # This method isn't even called
min: 1
minMessage: "Please select meal schema"
# This also doesn't work
- Count:
min: 1
max: 99
minMessage: Mealtime schema should not be blank
maxMessage: Max meal time exceeded
Ok, I solved it.
My mistake was to not provide information about mealSchema field in my entity when asking on stackoverflow. If I'd do that then I would realise that the entity field is in fact a smallint.
My colleague wanted to emulate a MySQL "SET" field type in Doctrine so he used an array as entrypoint values and converted it to bit values in setter method (and the other way around in the getter method).
That's why none of the choice-related validation rules worked. For now, I've made 2 quick solutions:
Change all fields' names in RecipeType from underscore to camelCase because that is how they are named in our entity. This helped for the error being attached to the form instead of the field.
Used a NotNull validator because the default value of mealSchema was null, not an empty array.
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.
I have the following line of code on a CakePHP view:
<?php
echo $this->Form->input(
'person_id',
array(
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)
);
?>
I want to create a text input with this line of code, but if the field name has the suffix _id, the rendered HTML changes from a text field to a drop-down select.
If I change the prefix to anything else, for example person_idd or abc_idd, it renders a text input, but if the field name ends with _id suffix, it renders a drop-down select, which doesn't allow me to write anything.
Is this some CakePHP feature? How can I avoid this behavior and produce a text input with a field ending with the _id suffix?
It's a CakePHP feature:
This method will automatically inspect the model field it has been supplied in order to create an appropriate input for that field.
Taken from Cookbook 2.x: FormHelper: Creating form elements.
To get a text input, add 'type' => 'text' to the options array:
<?php echo $this->Form->input('person_id', array(
'type' => 'text',
'label' => false,
'div' => false,
'class' => 'form-control search-person'
)); ?>
I'm trying to validate a form in a symfony 2.3 project,
So I have a 'Customer' field :
$builder
->add('customer',
'entity',
array('property'=> 'item',
'multiple' => true,
'expanded' => true,
'class' => 'OrdersBundle:Customer',
'required' => true, 'empty_value' => '',
'query_builder' => function(\Ella\OrdersBundle\Repository\CustomerRepository $er) {
return $er->createQueryBuilder('q')->andWhere("q.is_delete = 0")->orderBy('q.item', 'asc');
}));
I'm trying to return an error when user didn't select anything, so i do this :
properties:
customer:
- Choice: { min: 1, minMessage: 'message' }
Or
properties:
customer:
- NotBlank:
message: message
and other things, but nothing works , an idea of what I'm doing wrong ??
In the doc they say we could use an array, but this doesn't work either ...
Actually Symfony return :
Either "choices" or "callback" must be specified on constraint Choice
For the Choice validator you either need to specify an array with the available allowed choices or a callback function, from the docs:
This constraint is used to ensure that the given value is one of a given set of valid choices. It can also be used to validate that each item in an array of items is one of those valid choices.
What you could use may be the Count validator:
customer:
- Count:
min: 1
max: 99
minMessage: "Min message"
maxMessage: "You cannot specify more than {{ limit }}"