donglu5041 2015-01-05 14:15
浏览 42

Symfony2自定义验证与数据库连接但没有实体

I'm simply trying to validate a form (with no entity attached) with one field "username". I want to check if the username exists or not. If it doesn't a proper message has to be displayed at form errors level (or form field errors level, I don't care that much).

Here is the relevant part of my controller:

$formGlobalSearch=$this->createFormBuilder()
->add('username', 'text', array('constraints' => new UsernameExists()))
->add('role', 'choice', array('choices' => $rolesListForForm,'required' => false, 'placeholder' => 'Choose a role', 'label' => false, ))
->add('submitUsername', 'submit', array('label' => 'Search username globally'))
->getForm();

$formGlobalSearch->handleRequest($request);

if ($formGlobalSearch->isValid())
    {
    // do something in the DB and refresh current page
    }

return $this->render(//all the stuff needed to render my page//);
}

Here is the relevant part of service.yml

validator.unique.UsernameExists:
    class: AppBundle\Validator\Constraints\UsernameExists
    tags:
        - { name: validator.constraint_validator, alias: UsernameExists }

Here is the validator class:

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class UsernameExists extends Constraint
{
public $message = 'The username "%string%" does not exist.';

public function validatedBy()
{
    return 'UsernameExists';
}
}

Here is the validator:

<?php
namespace AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class UsernameExistsValidator extends ConstraintValidator
{
public function validate($value)
    {
    $globalUserToAdd = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy(
        array('username' => $value, 'enabled' => true)
        );

    if ($globalUserToAdd == null) //I did not find the user
        {
        $this->context->buildViolation($constraint->message)
            ->setParameter('%string%', $value)
            ->addViolation();
        }
    }
}

The class and the validator are in the directory "AppBundle\Validator\Constraints"

I'm getting the following error:

Expected argument of type "Symfony\Component\Validator\ConstraintValidatorInterface", "AppBundle\Validator\Constraints\UsernameExists" given

I of course have the following on top of my controller:

use AppBundle\Validator\Constraints\UsernameExists;

If I add the following to service.yml

arguments: ["string"]

I get the error:

No default option is configured for constraint AppBundle\Validator\Constraints\UsernameExists

  • 写回答

2条回答 默认 最新

  • doumeinuoye81969 2015-01-05 14:53
    关注

    try changing:

    public function validatedBy()
    {
        return 'UsernameExists';
    }
    
    validator.unique.UsernameExists:
        class: AppBundle\Validator\Constraints\UsernameExists
    

    to:

    public function validatedBy()
    {
        return 'UsernameExistsValidator';
    }
    
    validator.unique.UsernameExists:
        class: AppBundle\Validator\Constraints\UsernameExistsValidator
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集