symfony form builder update option field - symfony

is it possible to update an option field after adding it ?
$builder
->add('examens', 'entity', array(
'class' => 'TelegrammeExamenBundle:ExamExamen',
'property' => 'libelle',
'required' => true,
'empty_value' => 'Sélectionnez un examen',
//'data' => $this->em->getReference("TelegrammeExamenBundle:ExamExamen", 510),
'data' => null,
'query_builder' => function(ExamenRepository $r) {
return $r->getSelectList();
},
'attr' => array('class' => 'bg_white_filet_gris')
))
;
how modify field option ??? (setOption don't exist)
if (...) $builder->get('examens')->setOption('property', 'test');

You can simply ->add() it again. As the API documentation suggests for the add method:
Adds or replaces a child to the form
http://api.symfony.com/2.8/Symfony/Component/Form/FormInterface.html#method_add
This can be used to modify form elements for example in a FormEvent.
Alternatively the FormBuilder provides a setAttribute() method which can be used as follows:
$builder->get('examens')->setAttribute('property', 'test');

Related

Symfony 3.3 EntityType using distinct values in a drop down

I am working on an existing Symfony 3.3 project, and I have been asked to register a dropdown field in a results filter form that should contain all the values from a field in the database.
Until now, what I have do is the following that partially works:
$builder->add(
'newField',
EntityType::class,
[
'class' => Transaction::class,
'required' => false,
'multiple' => false,
'label' => 'New Field',
'choice_label' => 'newField',
'placeholder' => 'Choose a value'
]
)
The above code is able to display the values correctly, but unfortunately, because the table has repeatedly the same values, I get a long list of all the same values.
Instead, what I need to achieve is to display the DISTINCT values from the database.
I have tried the query_builder, but to be honest I am not sure I use it correctly as I am not an experienced Symfony developer.
Any idea on how to display the entries of the given column with DISTINCT results?
As you said yourself, you can use the query builder to achieve this. Something as simple as this should achieve what you're after:
->add('fooBar', EntityType::class, [
'query_builder' => function (EntityRepository $repository) {
return $repository
->createQueryBuilder('f')
->distinct();
},
]);
You can also specify a flag to be distinct and remember not to return the result but return the query. You can read more here
You are right,
Query_builder is the way to go.
So first create the method of what you want to do in TransactionRepository.
class TransactionRepository extends EntityRepository
{
//....
public function getDistinctValues()
{
//Return what you want to get, I cannot write this for you without your entity
}
}
Then use it in the FormType :
$builder->add(
'newField',
EntityType::class,
[
'class' => Transaction::class,
'required' => false,
'multiple' => false,
'label' => 'New Field',
'choice_label' => 'newField',
'placeholder' => 'Choose a value',
'query_builder' => function(TransactionRepository $repository) {
return $repository->getDistinctValues();
}
]
)
And you should be good to go.

How can I use the bootstrap-daterangepicker in symfony 3.4?

I try with the next code:
In the form builder
->add('daterange', DateType:class, array(
'widget' => 'single_text',
'attr' => ['class' => 'js-daterangepicker'],
'html5' => false,
))
And the javascript
$('.js-daterangepicker').daterangepicker({
});
This code works, but retrieve data invalid as validator result.
Is DateType the class? How do I recover the selected data in the widget?

Symfony entity type can not set select value, placeholder was selected

I am calling entity to provide drop down options. I set a place holder value. I tried setting the data value, but regardless placeholder value is alway tag with selected.
My PostFormType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('price', TextType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('artist', EntityType::class, [
'data'=>2,
'placeholder'=>'Choose Artist',
'class'=>Artist::class,
'choice_label'=>'artist',
'query_builder'=> function (EntityRepository $er) {
return $er->createQueryBuilder('artist')
->orderBy('artist.artist', 'ASC');
},
'empty_data' => null,
'attr' => array(
'class' => 'form-control'
)
])
->add('userId', HiddenType::class )
->add('description', TextareaType::class, array( 'attr' => array(
'class' => 'form-control'
)))
->add('purchaseDate','date')
->add('id',HiddenType::class)
;
}
You shouldn't configure data for property artist if it has to be modified by user in the form.
In case you want to set the default value of a new Entity. Even if you do, the change from UI form will not affect as the data attribute will reset the value to 2 as provided after submit.
You can always use Constructor of the relevant Entity to set any default value.
But, as artist is a referenced property. You should do it in Controller before you build the form.
$artist = $em->getRepository("AppBundle:Artist")->find(2);
$entity->setArtist($artist)
// Load the form here. The default value automatically set in the form
Hope this helps!

Symfony2 - Drop down with the possibility of adding fields

Sorry for my english.
I need to create a drop down list (select option) with values ​​taken from an Entity.
I must also add options (I use select2 to do this).
When I send the form it doesn't work. How can I configure the field type to make it work?
I need the possibilities to adding multiple persons
My current code is
...
->add('person', 'entity', array(
'attr' => array(
'class' => 'tags'
),
'class' => 'AppBundle:Person',
'data_class' => null,
'label' => 'Persons',
'mapped' => false,
'multiple' => true,
'required' => false
));
Select2
$('select.tags').select2({
language: 'it',
tags: true,
tokenSeparators: [','],
width: '100%'
});
Error message
The offset "0" does not exist.

symfony2 EWZRecaptchaBundle extra Fields

I have a problem with the EWZRecaptcha Bunlde (dev-master) and symfony 2.1.0.
The reCaptcha is displayed correctly and the image changes so i think the configuration is ok. But the reCaptcha is not validated and after submitting, $form->getErrorsAsString() says: This form should not contain extra fields.
Well, i think the extra fields are recaptcha_challenge_field and recaptcha_response_field that are sent from reCaptcha but i don think that i missed something in the docu so what can be wrong with them?
For validation i use the code from the docu: (i also tried the alternative, that was mentioned there)
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;
//...
/**
* #Recaptcha\True
*/
public $recaptcha;
//...
in config:
framework:
validation: { enable_annotations: true }
i added the field like this:
$builder->add('recaptcha', 'ewz_recaptcha', array(
'property_path' => false,
'attr' => array(
'options' => array(
'theme' => 'clean'
)
)
));
Maybe i forgot something essential, that was not mentioned in the docu?
Possibly try adding a 'constraints' option to the builder. My recaptcha builder add looks like this:
$builder->add('recaptcha', 'ewz_recaptcha', array(
'attr' => array(
'options' => array(
'theme' => 'red'
)
),
'label' => "Verification",
'property_path' => false,
'constraints' => array(
new True()
),
'help' => "Enter the words in the box for verification purposes."
));
So add a 'use' statement for the constraint:
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;
and then add the constraint option:
'constraints' => array(
new True()
),
finally found the solution!
to get rid of the extra fields i added those two fields in my form class:
$builder->add('recaptcha_challenge_field', 'hidden', array('property_path' => false));
$builder->add('recaptcha_response_field', 'hidden', array('property_path' => false));
the validation then works with:
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;
...
'constraints' => array(
new True()
)
the annotation doesn`t work for me:
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints AS Recaptcha;
...
/**
* #Recaptcha\True
*/
public $recaptcha;

Resources