2017-10-09 1 views
0

Dears,Symfony : ParamFetcher를 사용하여 전자 메일 매개 변수 유효성 확인

RequestParam에서 전자 메일의 유효성을 직접 확인하고 싶습니다. 나는 REGEX 이런 종류의 사용하고 있습니다 :

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])? 

을하지만 파서는

The parameter \"&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$\" must be defined. 

가 어떻게 그런 이메일 것을 확인할 수 있습니다 요구 사항 옵션에 정규식을로드하지 못했습니다 :

/** 
* @Rest\Patch(
*  "v1/customer/update", 
*  name="Update a customer" 
*) 
* @RequestParam(
*  name="email", 
*  requirements="email", 
*  description="The email of the user", 
*  strict=true 
* ) 
*/ 

미리 감사드립니다.

+0

특수 문자를 이스케이프 처리 할 수 ​​있습니다. 작동하지 않으면 사용자 정의 유효성 검사기를 등록 할 수 있습니다. –

답변

0

나는 arameter를 Value Object에 연결하면 생성되는 동안 유효성을 검사합니다. RequestParam 유효성 검사에서 제공 할 수있는 것보다 훨씬 많은 제어가 가능하다는 것을 알았습니다. 거기로

# In the same way a string is converted to a Entity, we can convert to a Vo\Username 
username_converter: 
    class: Ca\Api\Request\ParamConverter\UsernameConverter 
    tags: 
     - { name: request.param_converter, priority: -2, converter: username_converter } 

UsernameConverter 클래스는 example와 내장 DoctrineParamConverter뿐만 아니라 체크 객체로 값을 넣어, 더욱 쉽게되었다 다음뿐만 아니라 생성하는 것은 매우 간단했다 데이터베이스 액세스가 필요하지 않습니다.

값 Object 생성자의 유효성 검사가 실패하면이 (이 이메일을 것이다)처럼 :

if (!filter_var($address, FILTER_VALIDATE_EMAIL)) { 
    throw new \InvalidArgumentException(sprintf('"%s" is not a valid email', $address)); 
} 

다음 예외가 프레임 워크에 의해 체포된다.

관련 문제