2017-09-15 1 views
1

현재 첫 번째 Symfony 프로젝트를 진행하고 있으며 구현하고자하는 특수 기능으로 조금 어려움을 겪고 있습니다.
특정 서비스를 활성화하기 위해 다른 데이터와 함께 라이센스 키를 입력해야하는 양식이 있습니다.
양식을 제출할 때 모든 데이터의 유효성을 검사해야하며 유효성 검사의 한 부분은 지정된 라이센스 키가 유효한지 (라이센스 키 데이터베이스에 있음) 확인하는 것입니다.Symfony 3 폼 유효성 검사 : 기존 데이터베이스 엔트리 확인

내 양식은 별도의 폼 클래스에 앉아이 (중요한 부분까지 제거)과 같습니다

<?php 

namespace AppBundle\Form; 

use AppBundle\Entity; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Validator\Constraints as Constraint; 
use Symfony\Component\Validator\Context\ExecutionContextInterface; 

class Activation extends AbstractType 
{ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'data_class' => Entity\Customer::class, 
      'translation_domain' => 'forms' 
     ]); 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('activationCode', Type\TextType::class, [ 
       'mapped' => false, 
       'constraints' => [new Constraint\Callback(
        function ($object, ExecutionContextInterface $context, $payload) { 

        } 
       )] 
      ]) 
      ->add('firstName', Type\TextType::class) 
      ->add('lastName', Type\TextType::class) 
      ->add('email', Type\RepeatedType::class, [ 
       'type' => Type\EmailType::class, 
       'invalid_message' => 'The E-Mail fields must match.', 
       'first_options' => ['label' => 'E-Mail'], 
       'second_options' => ['label' => 'Repeat E-Mail'], 
      ]) 
      ->add('activate', Type\SubmitType::class); 
    } 

} 

내 첫번째 생각은 콜백 제약을 사용하는 것이었다보고에서 확인 할 수 있듯이 폐쇄.
적절한 방법일까요?
그렇다면 클로저 내부에서 데이터베이스/독트린 엔티티 관리자에 어떻게 액세스 할 수 있습니까?
아니면 완전히 다른 접근 방식을 권하고 싶습니까?

답변

0

이 사용 사례에 대한 사용자 지정 제약 조건 및 유효성 검사기 클래스를 직접 작성할 수 있습니다. 당신의 필요가 당신이 다른 서비스를 주입 할 수 있도록

https://symfony.com/doc/current/validation/custom_constraint.html

유효성 검사기는 서비스입니다. (i.E. 엔티티 관리자). 이렇게하면 폼이 유효성 검사 로직 자체를 다룰 필요가 없습니다.

+0

서비스가되는 유효성 검사기는 제가 누락 한 정보였습니다. 덕분에 완벽하게 작동합니다. – Hawklan