我正在使用 FOSUserbundle 和 symfony2 制作应用程序
但我可以输入 2 字节字符作为用户 ID。如何限制输入数据。
我应该检查mysql级别吗?还是学说实体级别?
根据@ManseUK 的帮助,
我试过像
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;
如果我输入了 0-9 以外的超过 10 个字符或字母,则可以正常输入。
你能找到一些提示吗?
我正在使用 FOSUserBundle 和 SonataAdminBundle
所以编辑用户Entity数据有三点。
在 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())
);
}
在 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(),
));
}
在 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));
}
}
感谢@ManseUK
我几乎解决了这个问题,但仍然存在神秘的东西。
/**
* @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;
简而言之,
* @Assert\MaxLength(limit="10", message="The tel number is too long.")
作品
但
* @Assert\Regex(
* pattern="/[0-9]/",groups={"Registration", "Profile"}
* )
不工作。
我不能超过10个字母,
但我可以输入除 [0-9] 以外的其他字母,例如字母表。