简体   繁体   English

Symfony 2.7-如何加载验证器?

[英]Symfony 2.7 - How to load validator?

I'm having a hard time loading the validator in symfony based on their example in the documentation. 根据文档中的示例,我很难在symfony中加载验证器。

When I do this: 当我这样做时:

 $validator = $this->get('validator');
 $errors = $validator->validate($teacher);

I get this: 我得到这个:

 Attempted to call an undefined method named "get" of class
   "DBViewerBundle\Repository\SchoolRepository".
    Did you mean to call e.g. "getSchoolInfo" or "getSchools"?

I assumed when I got that error it was because this repository class doesn't have 'extend Controller' or something that may include the validator. 我以为我收到该错误时是因为此存储库类没有“扩展控制器”或可能包含验证器的东西。 Tried it with extend Controller, with no luck. 尝试使用扩展控制器,没有运气。 Even tried doing it standalone and still get errors. 即使尝试独立执行此操作,仍然会出错。 I'm assuming there is something else I have to enable somewhere? 我假设我必须在某个地方启用其他功能?

Standalone way, I followed this document: http://blog.chrisworfolk.com/2012/06/20/using-the-symfony2-validator-component-outside-the-framework/ 独立运行时,我遵循此文档: http : //blog.chrisworfolk.com/2012/06/20/using-the-symfony2-validator-component-outside-the-framework/

I get this: 我得到这个:

Attempted to load class "ValidatorFactory" from namespace "Symfony\Component\Validator".
Did you forget a "use" statement for another namespace?

I definitely have the use there for the Symfony\\Component\\Validator. 我肯定在那里使用Symfony \\ Component \\ Validator。

The document you are using is for an outdated version of Symfony. 您正在使用的文档适用于Symfony的过时版本。 You need to use the Validation class. 您需要使用Validation类。 Check out their github page for examples: https://github.com/symfony/Validator 查看他们的github页面以获取示例: https : //github.com/symfony/Validator

All you need is to use the Validation::createValidator() method or Validation::createValidatorBuilder() method. 您所需要的只是使用Validation::createValidator()方法或Validation::createValidatorBuilder()方法。

A repository should not contain anything other than a query which is responsible of retrieving some data from somewhere. 除了查询负责从某处检索某些数据的查询外,存储库不应包含任何其他内容。 The validator is only available in a Controller or in a Service (by passing it as a variable when specifying it as a service in services.yml ). 验证器仅在ControllerService可用(通过在services.yml中将其指定为服务时将其作为变量传递)。

Furthermore, you can use Annotations to make specific requirements for your fields. 此外,您可以使用Annotations对您的字段提出特定要求。 You can, for example, require a field not to be empty and the string to be withing specific length: 例如,您可以要求一个字段不能为空,并且字符串必须具有特定的长度:

// User.php (Entity)

use Symfony\Component\Validator\Constraints as Assert;

class User {

    /**
     * @Assert\NotBlank()
     * @Assert\Length(min="6", max="12")
     */
    private $username;

}

Now, you just need to have annotation validation enabled (which is usually set to true by default) in config.yml : 现在,您只需要在config.yml启用注释验证(通常默认情况下将其设置为true):

framework:
    validation:      { enable_annotations: true }

In your controller you would only have to do 在您的控制器中,您只需要做

public function yourActionName(Request $request) {
    $form = $this->createForm(....);
    $form->handleRequest($request);

    if($form->isValid()) {
        // perform the action you want when the form is valid.
    }
}

Here's the official documentation . 这是官方文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM