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)
{
$this->reader = $reader;
$this->router = $rou;
$this->session = $session;
$this->sc = $sc;
$this->container = $cont;
}
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);
dump($method_name);
$method = new \ReflectionMethod($class_name, $method_name);
dump('method'); dump($method);
$annotation = $this->reader->getMethodAnnotation($method, $this->annotation_name);
dump($annotation);
if (!is_null($annotation)) {
$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);
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;
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
{ ...
public function join4Action($event_id) {
$reader = $this->get('annotation_reader');
$method = new \ReflectionMethod(get_class($this), 'join4Action');
$annotation_name = 'Yoda\UserBundle\Security\Annotation\ValidateUser';
$annotation = $reader->getMethodAnnotations($method);
$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'));
$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');
}