簡體   English   中英

Symfony 3中的自定義驗證器約束

[英]Custom validator constraint in Symfony 3

我正在嘗試在Symfony 3中創建自定義驗證器約束。

當然,我遵循以下文檔結尾:

#AppBundle\Validator\Constraint\Device.php

namespace AppBundle\Validator\Constraint;
/**
 * @Annotation
 */
class Device extends \Symfony\Component\Validator\Constraint
{
    public $message = 'Device "%device%" is not valid';
    public $shortMessage = 'Device ID is too short';
    public $chunkMessage = 'Device ID doesn\'t have a correct number of parts';
 }

約束

#AppBundle\Validator\DeviceValidator.php
namespace AppBundle\Validator;

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

class DeviceValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint) : void
{
    if (!$myconstraint) {
        //A lot of code here
        $this->context->buildViolation($constraint->shortMessage)->addViolation();
    }
}
}

和FormType

#AppBundle\Form\Type\LoginType.php

namespace AppBundle\Form\Type;

use AppBundle\Validator\Constraint\Device;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;

class LoginType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('username', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3, 'minMessage' => 'Username is too short'])]])
        ->add('password', TextType::class, ['required' => true, 'constraints' => [new Length(['min' => 3, 'minMessage' => 'Password is too short'])]])
        ->add('device', TextType::class, ['required' => true, 'constraints' => [new Device()]]);
}
}

表單是這樣創建的:

$loginForm = Forms::createFormFactoryBuilder()->addExtension(new HttpFoundationExtension())->getFormFactory()->create(LoginType::class);

但是,無論我做什么,我都會得到以下答復:

{“代碼”:500,“消息”:“選項\\” constraints \\“不存在。已定義的選項包括:\\” action \\“,\\” attr \\“,\\” auto_initialize \\“,\\” block_name \\“ ,\\“ by_reference \\”,\\“ compound \\”,\\“ data \\”,\\“ data_class \\”,\\“ disabled \\”,\\“ empty_data \\”,\\“ error_bubbling \\”,\\“ inherit_data \\”,\\ “ label \\”,\\“ label_attr \\”,\\“ label_format \\”,\\“ mapped \\”,\\“ method \\”,\\“ post_max_size_message \\”,\\“ property_path \\”,\\“ required \\”,\\“ translation_domain \\“,\\”修剪\\“,\\” upload_max_size_message \\“。 }

根據文檔,“約束”在我放置的位置允許,我真的不知道為什么Symfony在這里哭。

任何想法? 非常感謝。

您在AppBundle \\ Validator \\ Constraint \\ Device.php中的名稱空間不太正確。 刪除結尾的“約束”:

namespace AppBundle\Validator;

我認為您缺少ValidatorExtension。

use Symfony\Component\Validator\Validation;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
$validator = Validation::createValidator();

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new HttpFoundationExtension())
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory()
    ->create(LoginType::class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM