Esempio n. 1
0
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $user = $this->tokenStorage->getToken()->getUser();
     if (!$user) {
         throw new \LogicException(__CLASS__ . ' cannot be used without an authenticated user!');
     }
     $tags = $this->tagManager->getTextsByUser($user);
     $tag_ops = array_combine($tags, $tags);
     $builder->add('level', HiddenType::class, array('label' => 'Stress level'))->add('factorTexts', ChoiceType::class, array('choices' => $tag_ops, 'required' => false, 'label' => 'Factors', 'multiple' => true))->add('notes', TextAreaType::class, array('required' => false))->add('localtime', DateTimeType::class, array('date_widget' => 'single_text', 'label' => 'Local time'))->add('timezone', TimezoneType::class);
     // Callback function for adding tags to the select list,
     // used by event handlers below.
     $addTagsToForm = function (FormInterface $form, array $texts) {
         // Get the existing field & info
         $field = $form->get('factorTexts');
         $type = $field->getConfig()->getType()->getInnerType();
         $options = $field->getConfig()->getOptions();
         // Update the options
         foreach ($texts as $text) {
             $options['choices'][$text] = $text;
         }
         // Replace the field.
         $form->add('factorTexts', get_class($type), $options);
     };
     // Called before form data is populated.
     $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use($addTagsToForm) {
         $log = $event->getData();
         $form = $event->getForm();
         // Make sure all the *assigned* tags are included in the select list
         // (even if they haven't been persisted to the database yet).
         $addTagsToForm($form, $log->getFactorTexts());
     });
     // Called when form is submitted, before the submit is processed.
     $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use($addTagsToForm) {
         $data = $event->getData();
         // Make sure all *submitted* tags are included in the select list,
         // ex. for re-rendering form on error.
         if (array_key_exists('factorTexts', $data)) {
             $addTagsToForm($event->getForm(), $data['factorTexts']);
         }
     });
 }
 public function testGetUserTags()
 {
     $this->generateTags(array(array('texts' => array('alpha', 'bravo')), array('texts' => array('alpha', 'charlie', 'delta')), array('texts' => array('alpha', 'delta'))));
     $tags = $this->tagManager->getTextsByUser($this->user);
     $this->assertEquals(array('alpha', 'bravo', 'charlie', 'delta'), $tags);
 }