FOSUserbundleとsymfony2でアプリケーションを作っています
ただし、ユーザーIDとして2バイト文字を入力できます。入力データを制限するにはどうすればよいですか。
I should check mysql level? or doctrine entity level ?
According to @ManseUK help,
I have tried like
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
class User extends BaseUser
{
/**
* @ORM\Column(type="string")
* @Assert\MaxLength(limit="10", message="The tel number is too long.")
* @Assert\Regex("/[0-9]/")
*/
protected $tel1;
if I put more than 10 characters or letters other than 0-9 ,it is inputted normally.
Could you find some hint?
I am using FOSUserBundle and SonataAdminBundle
So there are three points to edit user Entity data.
when edit user data in FOS\UserBundle\Controller\ProfileController
/**
* Edit the user
*/
public function editAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->container->get('fos_user.profile.form');
$formHandler = $this->container->get('fos_user.profile.form.handler');
$process = $formHandler->process($user);
if ($process) {
$this->setFlash('fos_user_success', 'profile.flash.updated');
return new RedirectResponse($this->getRedirectionUrl($user));
}
return $this->container->get('templating')->renderResponse(
'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
array('form' => $form->createView())
);
}
When regist user data in FOS\UserBundle\Controller\RegitrationController
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
When manage data in SonataAdminBundle
namespace Acme\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction($name)
{
return $this->render('AcmeAdminBundle:Default:index.html.twig', array('name' => $name));
}
}
Thanks to @ManseUK
I have almost sloved the problem,but still mysterious thing remained.
/**
* @ORM\Column(type="string")
* @Assert\MaxLength(limit="10", message="The tel number is too long.")
* @Assert\Regex(
* pattern="/[0-9]/",groups={"Registration", "Profile"}
* )
*/
protected $tel1;
In a nutshell,
* @Assert\MaxLength(limit="10", message="The tel number is too long.")
works
but
* @Assert\Regex(
* pattern="/[0-9]/",groups={"Registration", "Profile"}
* )
dosen't work.
I can't put more than 10 letters,
but I can put other letters than [0-9] such as alphabet.