How to set min value for field in form? - symfony

I have form with integer field - price.
$builder->add('list',
CollectionType::class,
[
'required' => false,
'allow_add' => true,
'error_bubbling' => true,
])
->add('price',
IntegerType::class,
[
'required' => false,
'error_bubbling' => true,
]);
How to set, for example, for validation i need min value for price 0 or greater?
I tried this one, but it's not work:
'constraints' => [
new GreaterThanOrEqual(50)
],
Thanks for all help.
controller Action
public function getProductAction(Request $request)
{
$variables = $request->get('list');
$price = $request->get('price');
$form = $this->createForm(ProductForm::class, null, ['csrf_protection' => false, 'allow_extra_fields' => true]);
$form->submit(['variables' => $variables, 'prices' => $price]);
if(!$form->isValid()) {
$errors = '';
foreach ($form->getErrors() as $error) {
$errors = $error->getMessage();
}
return new JsonResponse([
'errors' => $errors
],Response::HTTP_BAD_REQUEST);
} else {
$product = $this->getDoctrine()
->getRepository(Product::class)
->findByListAndPrice($list, $price);
if (!$product) {
return new JsonResponse([
'errors' => 'Product not found.'
],Response::HTTP_BAD_REQUEST);
}
return new JsonResponse($product);
}
}
Form not validate, and don't show errors, $form->isValid() === true

According to https://github.com/symfony/symfony/issues/3533 you can use min and max for the IntegerType even though the documentation might not mention this.
$builder->add('list',
CollectionType::class,
[
'required' => false,
'allow_add' => true,
'error_bubbling' => true,
])
->add('price',
IntegerType::class,
[
'required' => false,
'error_bubbling' => true,
/*'min' => 50*/
'attr' => [
'min' => 50
]
]);
EDIT: According to the documentation the 'min' property has to be inside 'attr' tag. This will add the min inside the input in the HTML.

You can use RangeType instead of IntegerType.
use Symfony\Component\Form\Extension\Core\Type\RangeType;
// ...
$builder->add('list',
CollectionType::class,
[
'required' => false,
'allow_add' => true,
'error_bubbling' => true,
])
->add('price',
RangeType::class,
[
'required' => false,
'error_bubbling' => true,
'min' => 0,
'max' => 50
]);

Related

How to getData() on a collectionType field - Symfony 4

I have a collectionType (Equipements) on a Symfony 4 ORM form (intervention).
For example, this equipement field :
<input type="file" id="intervention_equipements_0_photoGraffiti" name="intervention[equipements][0][photoGraffiti]">
I try to get the data of this field with :
$fileToTransfer = $form['intervention_equipements_0_photoGraffiti']->getData();
or
$fileToTransfer = $form['intervention[equipements][0][photoGraffiti]']->getData();
I have the error Child "intervention[equipements][0][photoGraffiti]" does not exist.
Here is my intervention Type :
$builder
->add('idInstallation', EntityType::class, [
'class' => Installation::class,
'choice_label' => 'numeroInstallation',
'required' => false,
])
->add('equipements', CollectionType::class, array(
'entry_type' => EquipementInterventionType::class,
'entry_options' => array('label' => false),
))
And the equipement entity:
$builder
->add('nom', TextType::class, array(
'error_bubbling' => true,
'required' => false,
'disabled' => true
))
->add('numero', TextType::class, array(
'error_bubbling' => true,
'required' => false,
'disabled' => true
))
->add('etatEquipement', ChoiceType::class, array(
'choices' => $choices_controle,
'expanded' => false,
'multiple' => false,
'required' => false,
'error_bubbling' => true,
))
->add('photoEquipement', FileType::class, array(
'error_bubbling' => true,
'required' => false,
'disabled' => false,
'data_class' => null
))
->add('graffiti', CheckboxType::class, array(
'required' => false,
'error_bubbling' => true,
))
->add('photoGraffiti', FileType::class, array(
'error_bubbling' => true,
'required' => false,
'disabled' => false,
'data_class' => null
))
...
How can I fix this, please ?
Directly use $form->getData(). This will return the array with all your form datas that you are looking for.
in your form controller
$equipements = $intervention->getEquipement();
will return the array of collections then
foreach ($equipements as $equipement) {
$fileToTransfer = $equipement->getPhotoGraffiti()
}
Hope you have getter and setter in your entity class

how to map violation to Symfony form manually?

OrderType
$builder
->add('items', FormTypes\CollectionType::class, [
'entry_type' => OrderItemType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'acme.form.order.items',
])
->add('channel', ChannelSelectType::class, [
'required' => true,
'label' => 'acme.form.order.channel',
])
OrderItemType
$builder
->add('service', ServiceSelectType::class, [
'label' => 'acme.form.order_item.service',
])
->add('product', ProductSelectType::class, [
'label' => 'acme.form.order_item.product',
])
->add('quantity', FormTypes\IntegerType::class, [
'label' => 'acme.form.order_item.quantity',
]);
How to map the error to OrderItemType product field?
the order item is valid when a product is applied to a specific channel. however we have no way to get the submited channel in OrderItemType,
because child form type is submmited before its parent. so $event->getForm()->getParent()->getData()->getChannel() is empty. the only way I have
is to validate order item in OrderType, or create a validator which is added to Order class. the problem is how can I map the error to OrderItemType product field.
$orderItems = $order->getItems();
$channel = $order->getChannel();
foreach($orderItems as $index => $orderItem) {
$product = $orderItem->getProduct();
if (!$this->isProductAvailableForChannel($channel, $product)) {
$message = sprintf('product %ss is not available for channel "%s"', $product->getName(), $channel->getName());
}
if (null !== $message) {
$this->context
->buildViolation($this->constraint->message)
->setParameter($message)
->atPath("items.children[$index].product") // this doesn't work, the error will be added to root form.
->addViolation()
;
}
}
You are able to get the channel on the pre submit event (Symfony\Component\Form\FormEvents::PRE_SUBMIT)
And there you can add your channel based validation for the order item
$addItems = function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$options = [];
if (is_array($data) && array_key_exists('channel', $data)) {
$options['constraints'] = [
new OrderItemConstraint(['channel' => $data['channel']])
];
}
$form->add('items', FormTypes\CollectionType::class, [
'entry_type' => OrderItemType::class,
'entry_options' => $options,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'acme.form.order.items',
]);
};
$builder->addEventListener(FormEvents::PRE_SET_DATA, $addItems);
$builder->addEventListener(FormEvents::PRE_SUBMIT, $addItems);

Sonata date picker month resets to 1

I use sonata_type_datetime_range_picker. I works correct on the frontend side. But when I submit form it remakes month value to 1. I.e. my submitted value is "2017-02-02 03:14:00" and value after submit is "2017-01-02 03:14:00". According to the profiler date is correct in the Request object but is wrong in Symfont Form Component.
My admin code
$datePickerOptions = [
'format' => "YYYY-MM-DD HH:mm:SS",
'datepicker_use_button' => false,
'dp_use_minutes' => false,
'dp_use_seconds' => false,
'dp_side_by_side' => true,
'dp_language' => 'en',
];
$datagridMapper
->add(
'dateTime',
'doctrine_orm_datetime_range',
[
'show_filter' => true,
'field_type' => 'sonata_type_datetime_range_picker',
'field_options' => [
'field_options_start' => $datePickerOptions,
'field_options_end' => $datePickerOptions,
],
]
);
What am I doing wrong?
Ok, I kinda found the working solution myself. Maybe it helps someone other sometime:
$now = new \DateTime();
$historyStart = new \DateTime('Dec 20 2016');
$dateFormat = 'd.m.y H';
$datagridMapper
->add(
'dateTime',
'doctrine_orm_datetime_range',
['show_filter' => true,],
'sonata_type_datetime_range_picker',
[
'field_options_start' => [
'dp_min_date' => $historyStart->format($dateFormat),
'dp_max_date' => $now->format($dateFormat),
'dp_default_date' => $now->format($dateFormat),
'dp_language' => 'en',
'format' => 'dd.MM.yy HH',
'datepicker_use_button' => false,
'dp_use_minutes' => false,
'dp_use_seconds' => false,
'dp_side_by_side' => true,
],
'field_options_end' => [
'dp_min_date' => $historyStart->format($dateFormat),
'dp_max_date' => $now->format($dateFormat),
'dp_default_date' => $now->format($dateFormat),
'dp_language' => 'en',
'format' => 'dd.MM.yy HH',
'datepicker_use_button' => false,
'dp_use_minutes' => false,
'dp_use_seconds' => false,
'dp_side_by_side' => true,
]
]
);

Symfony2 collection of choices different option for each elements

After several search and try I don't know how to solve my problem.
I found this: Symfony2 Form Collection Field with Different Choices but solution was not given only a search trail and I didn't found how to adapt solution in my case.
I have Many to Many relation between Localization and Region, Many to Many relation between LOcalization and Department and Many to Many relation between Localization and City.
To create a localization i have this form:
class LocalizationType extends AbstractType{
private $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('regions', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => (array_key_exists('regions', $options['localization_value']) ? $options['localization_value']['regions'] : array('' => '')),
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'region input'),
),
'data' => (array_key_exists('regions', $options['localization_data']) ? $options['localization_data']['regions'] : null),
))
->add('departments', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => (array_key_exists('departments', $options['localization_value']) ? $options['localization_value']['departments'] : array('' => '')),
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'department input')
),
'data' => (array_key_exists('departments', $options['localization_data']) ? $options['localization_data']['departments'] : null),
))
->add('cities', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => (array_key_exists('cities', $options['localization_value']) ? $options['localization_value']['regions'] : array('' => '')),
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'city input')
),
'data' => (array_key_exists('cities', $options['localization_data']) ? $options['localization_data']['cities'] : null),
))
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event){
$data = $event->getData();
if(!empty($data['regions']) && is_array($data['regions'])){
$regions = array();
foreach($data['regions'] as $region){
$regions[] = $region;
}
$data['regions'] = $this->manager->getRepository('LocalizationBundle:Region')->findRegionsForCreateEntity($regions);
}
if(!empty($data['departments']) && is_array($data['departments'])){
$departments = array();
foreach($data['departments'] as $department){
$departments[] = $department;
}
$data['departments'] = $this->manager->getRepository('LocalizationBundle:Departments')->findDepartmentsForCreateEntity($departments);
}
if(!empty($data['cities']) && is_array($data['cities'])){
$cities = array();
foreach($data['cities'] as $city){
$cities[] = $city;
}
$data['cities'] = $this->manager->getRepository('LocalizationBundle:City')->findCitiesForCreateEntity($cities);
}
$form = $event->getForm();
$form->add('regions', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => $data['regions'],
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'region input')
)
));
$form->add('departments', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => $data['departments'],
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'department input')
)
))
->add('cities', CollectionType::class, array('entry_type' => ChoiceType::class,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'entry_options' => array(
'choices' => $data['cities'],
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'city input')
)
))
;
});
}
public function configureOptions(OptionsResolver $resolver){
$resolver->setDefaults(array(
'data_class' => Localization::class,
'localization_data' => array('regions' => '', 'departments' => '', 'cities' => ''),
'localization_value' => array('regions' => '', 'departments' => '', 'cities' => ''),
));
}
I choose a ChoiceType empty because I have several City for example( more of 25k) so I prefer load few of them with AJAX in my view and render them in a select2, it works for add action but for edit action I have a problem I want each field of my collections have a different value.
To illustrate my story, I want this result:
<label>Region n°1</label>
<select id="" name="">
<option value="foo1">bar1</option>
</select>
<label>Region n°2</label>
<select id="" name="">
<option value="foo2">bar2</option>
</select>
And the result I have for the moment is:
<label>0</label>
<select id="" name="">
<option value="foo1" selected="selected">bar1</option>
<option value="foo2">bar2</option>
</select>
<label>1</label>
<select id="" name="">
<option value="foo1">bar1</option>
<option value="foo2" selected="selected">bar2</option>
</select>
To change label if I understand I need to create my own template, ok but to display only option selected and not others I think I need a FormEventListener on PRE_SET_DATA but I don't see how implement this. So if someone has a solution, I'll take it.
I answer my own question, after try with FormEventListener on PRE_SET_DATA, on POST_SET_DATA, or with a choice_loader it's seems to be impossible to do what i want with a Symfony way, but i don't well understand why. I feel back on Jquery way to remove option who are not selected with following code, i dislike but I don't see others solutions:
<script type="text/javascript">
$.each($('.region'), function(i, val){
$(val).find('option:not(:selected)').remove();
})
$.each($('.department'), function(i, val){
$(val).find('option:not(:selected)').remove();
})
$.each($('.city'), function(i, val){
$(val).find('option:not(:selected)').remove();
})
</script>
I don't pass the topic as solved in case I'm wrong

Generate data attribute to each checkbox in Symfony type

->add('research_subject', EntityType::class, array(
'mapped' => false,
'class' => Subject::class,
'label' => 'Research Subject',
'expanded' => true,
'multiple' => true,
'query_builder' => function (EntityRepository $er) {
$db = $er->createQueryBuilder('w');
$db ->where($db->expr()->andX(
$db->expr()->isNotNull('w.pid')
));
$db->orderBy('w.pid', 'ASC');
return $db;
},
'choice_label' => 'name_system',
))
I need to add to each check box data attribute. Is that is possible?
I need that for extra sort of checkboxes in twig latter. I need to group checkboxes by pid value in separate div section.
'choice_attr' => function($val, $key, $index) {
return ['data-pid' => $val->getPid()];
},
I had use this as solution, like https://stackoverflow.com/users/4224384/yceruto
Suggest.
You would use the "choice_attr" option for this:
[...]
'expanded' => true,
'multiple' => true,
'choice_attr' => function($val, $key, $index) {
return ['data' => $key];
},
[...]

Resources