简体   繁体   English

Sonata管理员捆绑软件-字符串验证

[英]Sonata Admin Bundle - String Validation

I've some forms in my Sonata Admin Bundle and some fields are set as "required = true". 我的Sonata Admin Bundle中有一些表单,有些字段设置为“required = true”。 Now, you can circumvent this requirement with an empty string, eg with space. 现在,您可以使用空字符串来规避此要求,例如使用空格。 It works even for integer-types.. 它甚至适用于整数类型。

How can I build a validation, which secures from some unallowed entries ? 如何构建验证,从一些不允许的条目中获取?

Thank you 谢谢

You can add a custom callback validator to verify your strings with all cases you want. 您可以添加自定义回调验证程序,以根据所需的所有情况验证字符串。

Just add in your admin class: 只需添加您的管理类:

/**
 * {@inheritdoc}
 */
public function validate(ErrorElement $errorElement, $object)
{
    $errorElement
        ->assertCallback(array('validateMyEntity'))
    ;
}

and in your entity: 在你的实体中:

use Symfony\Component\Validator\ExecutionContext;

/**
 * Validates my entity and throw violations
 */
public function validateMyEntity(ExecutionContext $context)
{
    $title = $this->getTitle();

    if (empty($title)) {
        $context->addViolation('Title can\'t be empty.');
    }
}

Hope it helps. 希望能帮助到你。

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

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