symfony2 EWZRecaptchaBundle extra Fields - symfony

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;

Related

Any reason why the choice_attr in Symfony3 ChoiceType can't set the id attribute?

I have created a Symfony3 form class with the following ChoiceType element for my form:
->add('deg_certs', ChoiceType::class, array(
'mapped' => false,
'expanded' => true,
'multiple' => true,
'label' => 'Degrees/Certificates received:',
'choices' => array(
'BA/BS' => 'ba_bs',
'AA/AS' => 'aa_as',
'Voc. Cert.' => 'voc_cert',
'Other' => 'other',
),
'choice_attr' => function($val){
return ['class' => $val];
},
))
This works fine and sets the class attribute to the choice value. But I want to instead set the id attribute.
I've tried this:
'choice_attr' => function($val){
return ['id' => $val];
},
Which I was expecting to work but doesn't.
I also tried in my Twig template this:
{{ form_widget(form.deg_certs[3],
{'attr':{'id':'deg_other',
'onchange':'changeDegCertOther()',
'style':'float:left'}}) }}
But that doesn't set the id attribute either. Note: the above is trying to set the id of the 3rd item in the choices in case you are not familar.
This appears to be an issue with the Symfony3 code not supporting this. Can anyone confirm?
The reason I want to set the id attribute, is because in my Javascript I want to use getElementById not getElementsByClassName (which gets an array).
I've explained in https://github.com/symfony/symfony/issues/20965 why this is expected and how to work around that.

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.

symfony form builder update option field

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');

Custom query or specific getter method for sonata_type_collection form type

I have a Trip entity that has many HoneymoonComponent. In the TripAdmin, I am using a sonata_type_collection to edit inline all the HoneymoonComponent related:
$formMapper
->add('isHoneymoonEnabled', null)
->add('honeymoonComponents', 'sonata_type_collection', array('by_reference' => false), array(
'edit' => 'inline',
'inline' => 'table',
));
This works properly, but what I would like to achieve, is to only show the honeymoonComponents that match a criteria (for example: HoneymoonComponent::enabled = false).
I can not find a way to specify a custom query for that, and I have a workaround which I don't know if it's good which is override the HoneymoonComponent::getHoneymoonComponent to only return the ones that match the criteria... but I don't much like it.
Is there a way to specify which "getter" method should sonata_type_collection to use? (or specify custom query). Both solutions would be fine for me.
Thanks in advance !
You can try this:
$formMapper
->add('isHoneymoonEnabled', null)
->add('honeymoonComponents', null, array(
'by_reference' => false,
'class' => 'My\Bundle\Entity\MyEntity',
'query_builder' => $this->modelManager->createQuery('My\Bundle\Entity\MyEntity', 'h')
->where('h.enabled = 0'),
), array(
'edit' => 'inline',
'inline' => 'table',
));

Sonata Exception get too many admin registered

I get the message:
Unable to found a valid admin for the class: Aman\VarshneyBundle\Entity\ArticleTable, get too many admin registered: sonata.admin.appsreview,sonata.admin.review,sonata.admin.article
I am not able to figure out this issue.
you have to specify "admin_code" option in your field definition
in your admin class while building your form
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('user', 'entity', array(), array(
'admin_code' => 'your.user.admin.service'
));
}
It only happens when you have multiple admin classes for the same entity.
I will put code with the use for the 'configureListFields' method, if it's usefull for someone.
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('filename', null, array('admin_code' => 'your.file.admin.service', 'label' => 'File Name'))
->add('parent', 'sonata_type_list', array('admin_code' => 'your.file.admin.service', 'label' => 'Parent File'))
->add('_action', 'actions', array(
'label' => 'Actions',
'actions' => array(
'download' => array(
'template' => 'FileAdminBundle:File:list__action_download.html.twig'
)
)
));
}
As we see, if we have multiple fields, we must put the 'admin_code' in all of them, excepts the actions (if we have it).
Hope it helps.

Resources