Skip to content

Commit

Permalink
Merge pull request #28 from tominventisbe/feature/allow-disabling-for…
Browse files Browse the repository at this point in the history
…m-error-when-filled-in

Allow disabling form error if honeypot is filled in
  • Loading branch information
eymengunay committed Sep 19, 2019
2 parents fbb3f8d + 2fe41b6 commit 1faf275
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Form/Type/HoneypotType.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$honeypotManager = $this->honeypotManager;
$eventDispatcher = $this->eventDispatcher;

$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) use ($request, $honeypotManager, $eventDispatcher) {
$builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) use ($request, $honeypotManager, $eventDispatcher, $options) {
$data = $event->getData();
$form = $event->getForm();

Expand All @@ -85,7 +85,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// Save prey
$honeypotManager->save($prey);

$form->getParent()->addError(new FormError('Form is invalid.'));
if ($options['causesError']) {
$form->getParent()->addError(new FormError('Form is invalid.'));
}
});
}

Expand All @@ -95,10 +97,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'required' => false,
'mapped' => false,
'data' => '',
'attr' => array(
'required' => false,
'mapped' => false,
'data' => '',
'causesError' => true,
'attr' => array(
// autocomplete="off" does not work in some cases, random strings always do
'autocomplete' => 'nope',
// Make the field unfocusable for keyboard users
Expand Down

0 comments on commit 1faf275

Please sign in to comment.