Symfony2, ERROR: Unable to automatically check string type values. Specify a limit

Symfony 2 ERROR: Unable to automatically check string type values. Specify a restriction.

I do not understand what restriction and where should I provide?

Below I will give an example from the book "Extending Symfony 2", chapter 4. I am trying to make a custom annotation "ValidateUser" and use it in Entity User $ phone. I also have a "ValidUserListenerCustom" registered as a service that redirects the response of the join4Action controller action. This action is annotated with the specified user annotation "@ValidateUser (" join_event ")". This action should add the user to the event only if the user has a mobile phone number in his profile.

C: \ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ star \ SRC \ Yoda \ UserBundle \ Entity \ User.php

/**
 * @ORM\Column(type="string", length=255, name="phone")
 * @Assert\NotBlank(groups={"join_event"})
*/
protected $phone;

C: \ Bitnami \ wampstack-5.5.30-0 \ sym_prog \ star \ SRC \ Yoda \ UserBundle \ Security \ ValidUserListenerCustom.php

<?php

namespace Yoda\UserBundle\Security;

use Doctrine\Common\Annotations\Reader;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;


class ValidUserListenerCustom
{
    private $reader;
    private $annotation_name = 'Yoda\UserBundle\Security\Annotation\ValidateUser';

    private $router;
    private $session;
    private $sc;
    private $container;

    public function __construct(Reader $reader, Router $rou, Session $session, SecurityContext $sc, Container $cont)
    {
        /** @var AnnotationReader $reader */
        $this->reader = $reader;

        $this->router = $rou;
        $this->session = $session;
        $this->sc = $sc; //security.context
        $this->container = $cont; //container        
    }

    public function onKernelController(FilterControllerEvent $event)
    {

        $controller = $event->getController();
        if (!is_array($controller)) {
            return;
        }
        $class_name = get_class($event->getController()[0]);
        $method_name = $event->getController()[1];

        dump($class_name); // "Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController"
        dump($method_name);

        $method = new \ReflectionMethod($class_name, $method_name);
        dump('method'); dump($method);

        // Read the annotation
        $annotation = $this->reader->getMethodAnnotation($method, $this->annotation_name);     
        dump($annotation);//null

        if (!is_null($annotation)) {
            // Retrieve the validation group from the annotation, and try to
            // validate the user
            $validation_group = $annotation->getValidationGroup();

            dump($validation_group);

            $user = $this->container->get('security.token_storage')->getToken()->getUser();            
            dump($user);

            $validator = $this->container->get('validator');
            dump($validator);

            $errors = $validator->validate($user);
            dump($errors);
            //original: $errors = $this->validator->validate($user, $validation_group);

            if (count($errors)) {
                $event->setController(function(){
                    $this->session->getFlashBag()->add('warning', 'You must fill in your phone number before joining a meetup.');
                    return new RedirectResponse($this->router->generate(
                            'user_edit',
                            array ('id' => '1') ) );
                });
            }
        }
    }
}

C:\Bitnami\wampstack-5.5.30-0\sym_prog\\SRC\Yoda\UserBundle\Security\\ValidateUser.php

<?php

namespace Yoda\UserBundle\Security\Annotation;

//  Extending Symfony 2
// annotation itself ...\src\Yoda\UserBundle\Security\Annotation\ValidateUSer.php
// used in class ...src\Yoda\UserBundle\Entity\User.php
// readed/analysed by ...src\Yoda\UserBundle\Security\Annotation\AnnotationServices\AnnotationDriver::loadMetadataForClass


/**
 * @Annotation
 */
class ValidateUser
{
    private $validation_group;

    public function __construct(array $parameters)
    {
        $this->validation_group = $parameters['value'];
    }

    public function getValidationGroup()
    {
        return $this->validation_group;
    }
}

C:\Bitnami\wampstack-5.5.30-0\sym_prog\\SRC\Yoda\UserBundle\Resources\Config\services.yml

yoda.user.security.valid_user_custom:
    class:      Yoda\UserBundle\Security\ValidUserListenerCustom
    arguments:  [@annotation_reader, @router, @session, @security.context, @service_container]
    tags:
        - { name: kernel.event_listener, event: kernel.controller, method: onKernelController }

"ValidateUser", , , , .

url: http://127.0.0.1:8000/events/5/join4

C:\Bitnami\wampstack-5.5.30-0\sym_prog\\SRC\Yoda\UserBundle\Controller\DefaultController.php

<?php ...
class DefaultController extends Controller
{ ...

    /**
     * @Route("/events/{event_id}/join4")
     * @Template()
     * @ValidateUser("join_event")
     */    
public function join4Action($event_id) {

        //Chapter 4
        $reader = $this->get('annotation_reader');
        $method = new \ReflectionMethod(get_class($this), 'join4Action');
        $annotation_name = 'Yoda\UserBundle\Security\Annotation\ValidateUser';
        $annotation = $reader->getMethodAnnotations($method);

        //Chapter 1
        $em = $this->getDoctrine()->getManager();
        $meetup = $em->getRepository('EventBundle:Event')->find($event_id);

        $form = $this->createForm(new JoinEventType(), $meetup, array(
            'action' => '',
            'method' => 'POST',
        ));
        $form->add('submit', 'submit', array('label' => 'Join'));

        $form->handleRequest($this->get('request'));

        //Do not know how to make it to work, chec later
        //$user = $this->get('security.context')->getToken()->getUser();
        $user = $em->getRepository('UserBundle:User')->find('1');

        if ($form->isValid()) {
            $meetup->addAttendee($user);
            $this->get('event_dispatcher')->dispatch(MeetupEvents::MEETUP_JOIN, new MeetupEvent($user, $meetup));
            $em->flush();
        } 

        $form = $form->createView();
        return compact('meetup', 'user', 'form');
    }  
+4

Source: https://habr.com/ru/post/1618291/


All Articles