2017-11-28 1 views
1

심포니 검사기에 도움이 필요합니다. 배열의 특정 값만 유효성을 검사 할 수 있습니까?symfony 유효성 검사 - 배열의 일부인

'0' => [ 
    'interestidKey' => true, 
    'anotherInterestedKey' => 'foo' 
], 
'error' => [ 
    'errorMsg => 'not interest for me' 
] 

나는 검증이 배열을 검증 할 필요가 주로 0 가치를 예를 들어 내가 배열을 가지고있다. 배열에 '0'키가 있고 내부에 부울 값이있는 interestidKey 인 경우 알아야합니다. 나는 항상 배열을 위해 콜렉션을 사용한다. 왜냐하면 ofc는 errorinterestidKey을 포함하지 않는 에러를 보여주기 때문에이 경우에는 작동하지 않는다.

어떻게 해결할 수 있습니까?

+0

일반적으로 어레이는 어떻게 생겼을까요? 0과 'error'키가 맞습니까? 또는 숫자 키와 오류 키를 임의로 선택합니까? –

+0

숫자 키 및 오류 키입니다. – KKKK

답변

0

상자 제약 조건에 따라 원하는 것을 수행 할 수 있을지 확신하지 못합니다. 그러나 자신 만의 글을 써서 원하는 것을 할 수 있어야합니다. https://symfony.com/doc/current/validation/custom_constraint.html을보고 도움이되는지 확인하십시오.

당신은 당신이 할 수있는 검증 인 경우 :

if(array_key_exists(0, $array)) { 
    if(array_key_exists("interestid", $array[0])) { 
     return true; 
    } 
} else { 
    // do the error stuffs 
} 
0

그냥 키를 확인, 배열을 통해 루프를 구축 할 수는 숫자 키의 경우 (또는하지 오류 키)를 적용하여 아이들에 대한 검증. 그러면 다음과 같이 보입니다.

use Symfony\Component\Validator\Constraints as Assert; 

... 

$constraint = new Assert\Collection([ 
    'fields' => [ 
     // put any constraints for your objects here, keyed by field name 
     'interestidKey' => new Assert\Type('bool') 
    ], 
    'allowExtraFields' => true // remove if you don't want to allow other fields than specified above 
]); 

$violations = []; 
foreach($data as $key => $item) { 
    if ($key != 'error') { 
     $violations[$key] = $validator->validate($item, $constraint); 
    } 
} 
관련 문제