You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
tos/Form/AcceptTosType.php

55 lines
1.6 KiB

<?php
namespace vierbergenlars\AuthserverTosBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Constraints\IsTrue;
class AcceptTosType extends AbstractType
{
private static function generateTermLink(array $term)
{
return 'the <a href="' . htmlentities($term['url'], ENT_HTML5 | ENT_QUOTES) . '">' . htmlentities($term['label'], ENT_HTML5) . '</a>';
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$terms = $options['terms'];
if (count($terms) > 1) {
$lastItem = ' and ' . self::generateTermLink(array_pop($terms));
} else {
$lastItem = '';
}
$termsString = implode(', ', array_map([
self::class,
'generateTermLink'
], $terms)) . $lastItem;
$builder->add('accept', CheckboxType::class, [
'label' => 'I accept ' . $termsString,
'attr' => [
'align_with_widget' => true
],
'constraints' => [
new IsTrue([
'message' => 'You must accept.'
])
]
]);
}
/**
*
* {@inheritdoc}
* @see \Symfony\Component\Form\AbstractType::configureOptions()
*/
public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
{
$resolver->setDefined('terms')
->setRequired('terms')
->addAllowedTypes('terms', 'array')
->setDefault('label', false);
}
}