如何将输入数据仅限制为mysql [a-zA-Z0-9_]

时间:2013-07-03 07:05:40

标签: symfony fosuserbundle

我正在使用FOSUserbundle和symfony2

进行申请

但我可以输入2byte caracter作为userid。 如何限制输入数据。

我应该检查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;

如果我输入超过10个字符或0-9以外的字母,则输入正常。

你能找到一些提示吗?

我正在使用FOSUserBundle和SonataAdminBundle

因此,有三点可以编辑用户实体数据。

编辑用户数据时 在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]之外的其他字母,例如字母。

1 个答案:

答案 0 :(得分:1)

你可以在Symfony中使用正则表达式作为验证......这样的事情会起作用:

/* your user entity file */
use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @Assert\Regex("/[a-zA-Z0-9_]/")
     */
    protected $username;
}

Documentation on the Regex constraint here

更新

请阅读以下note on the FOSUserBundle forms section

  

默认情况下,在验证新用户注册时使用注册验证组。除非您在配置中覆盖了此值,否则请确保将名为Registration的验证组添加到您的name属性中。

所以你需要这样的东西:

/**
 * @Assert\Regex(
 *        pattern="/[a-zA-Z0-9_]/",
 *        groups={"Registration", "Profile"}
 * )
 */
protected $username;

更新2

问题在于你的正则表达式 - 它的匹配数字......但是它并不限制输入只是在输入中的任何地方对其匹配数字进行编号...你需要使用类似下面的内容:

^\d{1,10}$
  • ^从一开始就意味着
  • \d仅表示数字
  • {1,10}表示1到10次出现,即将长度限制为10
  • $意味着到最后

使用此正则表达式意味着您不再需要maxlength断言