verum php 는 배열(파일 지원 포함)을 쉽게 검증할 수 있는 PHP용 서버측 검증 라이브러리입니다. 사용자 정의 오류 메시지, 규칙, 내장 번역 및 종속성 없음이 함께 제공됩니다.
PHP용 서버측 검증 라이브러리
Composer와 함께 verum php 설치
composer require sandromiguel/verum-php
간단한 등록 양식 확인(이름, 이메일, 나이)
use Verum Validator ;
$ rules = [
' name ' => [
' rules ' => [
' required ' ,
],
],
' email ' => [
' rules ' => [
' required ' ,
' email ' ,
],
],
' age ' => [
' rules ' => [
' numeric ' ,
],
],
];
$ validator = new Validator ( $ _POST , $ rules );
echo json_encode (
[
' valid ' => $ validator -> validate (),
' errors ' => $ validator -> getErrors (),
]
);
입력:
[
'name' => 'John Doe',
'email' => '[email protected]',
'age' => '20',
]
산출:
{
"valid" : true ,
"errors" : []
}
입력:
[
'name' => '',
'email' => 'some text',
'age' => 'some text',
]
산출:
{
"valid" : false ,
"errors" : {
"name" : {
"label" : null ,
"rules" : {
"required" : " This field is required. "
}
},
"email" : {
"label" : null ,
"rules" : {
"email" : " This field must be a valid email address. "
}
},
"age" : {
"label" : null ,
"rules" : {
"numeric" : " This field must be numeric. "
}
}
}
}
RuleEnum
클래스 사용 RuleEnum
클래스를 사용하여 모든 규칙 이름에 액세스할 수 있습니다.
use Verum Validator ;
use Verum Enum RuleEnum ;
$ rules = [
' name ' => [
' rules ' => [
RuleEnum:: REQUIRED ,
],
],
...
];
$ rules = [
' name ' => [
' label ' => ' Name ' ,
' rules ' => [
RuleEnum:: REQUIRED ,
],
],
...
];
산출:
{
...
"errors" : {
"name" : {
"label" : " Name " ,
"rules" : {
"required" : 'The "Name" field is required.'
}
},
...
}
}
$ rules = [
' name ' => [
' label ' => [
' en ' => ' Name ' ,
' pt-pt ' => ' Nome ' ,
],
' rules ' => [
RuleEnum:: REQUIRED ,
],
],
...
];
출력(pt-pt):
{
...
"errors" : {
"name" : {
"label" : " Nome " ,
"rules" : {
"required" : 'O campo "Nome" é obrigatório.'
}
},
...
}
}
내장된 번역을 사용할 수 있습니다:
'en'
-> 영어(기본값)'nl-nl'
-> 네덜란드어'pt-pt'
-> 포르투갈어-포르투갈어'pt-br'
-> 포르투갈어-브라질 $ validator = new Validator ( $ _POST , $ rules , ' pt-pt ' );
LangEnum
클래스를 사용하여 메시지 언어 지정 use Verum Validator ;
use Verum Enum LangEnum ;
. . .
$ validator = new Validator ( $ _POST , $ rules , LangEnum:: PT_PT );
. . .
$ validator = new Validator ( $ _POST , $ rules );
$ validator -> addSimpleCustomMessage ( ' min_length ' , ' Min Length rule custom error message ' );
. . .
출력 예:
{
...
"errors" : {
"name" : {
"label" : " Name " ,
"rules" : {
"min_length" : " Min Length rule custom error message "
}
},
...
}
}
. . .
$ validator = new Validator ( $ _POST , $ rules );
$ validator -> addSimpleCustomMessage ( ' min_length ' , ' Number of characters detected: {param:1}. Field name: "{param:2}". ' );
. . .
출력 예:
{
...
"errors" : {
"name" : {
"label" : " Name " ,
"rules" : {
"min_length" : 'Number of characters detected: 5. Field name: "Name".'
}
},
...
}
}
. . .
$ validator = new Validator ( $ _POST , $ rules );
$ validator -> addCustomMessage (
' required ' ,
' Custom error message with label for required rule. Label: {param:1}. ' ,
' Custom error message without label for required rule. '
);
. . .
출력 - 라벨이 있는 필드:
{
...
"errors" : {
"name" : {
"label" : " Name " ,
"rules" : {
"required" : 'Custom error message with label for required rule. Label: Name.'
}
},
...
}
}
출력 - 레이블이 없는 필드:
{
...
"errors" : {
"name" : {
"label" : null ,
"rules" : {
"required" : " Custom error message without label for required rule. "
}
},
...
}
}
. . .
$ validator = new Validator ( $ _POST , $ rules );
$ validator -> addCustomMessages (
[
' min_length ' => ' Custom message for the "min_length" rule. ' ,
' required ' => ' Custom message for the "required" rule. ' ,
// other messages ...
]
);
. . .
. . .
$ validator = new Validator ( $ _POST , $ rules );
$ validator -> addCustomMessages (
[
' numeric ' => [
' withLabel ' => ' Custom message with label for "numeric" rule. Label: {param:1}. ' ,
' withoutLabel ' => ' Custom message without label for "numeric" rule. ' ,
],
' min_length ' => [
' withLabel ' => ' Custom message with label for "min_length" rule. Label: {param:2}, value: {param:1}. ' ,
' withoutLabel ' => ' Custom message without label for "min_length" rule. Value: {param:1}. ' ,
],
// other messages ...
]
);
. . .
verum php 사용하면 다중 이름 필드를 보다 효과적으로 처리할 수 있습니다. 이는 언어 식별자 또는 이름의 기타 변형을 포함하는 필드입니다. 예를 들어 title.en
, title.pt
, description.en
및 description.pt
와 같은 필드가 있는 경우 와일드카드를 사용하여 해당 필드에 대한 규칙을 지정할 수 있습니다.
$ rules = [
' title.* ' => [
' rules ' => [
RuleEnum:: REQUIRED ,
],
],
' description.* ' => [
' rules ' => [
RuleEnum:: REQUIRED ,
RuleEnum:: MIN_LENGTH => 10 ,
],
],
];
$ validator = new Validator ( $ _POST , $ rules );
// ...
출력 예:
{
"valid" : false ,
"errors" : {
"title.en" : {
"label" : null ,
"rules" : {
"required" : " This field is required. "
}
},
"title.pt" : {
"label" : null ,
"rules" : {
"required" : " This field is required. "
}
},
"description.en" : {
"label" : null ,
"rules" : {
"required" : " This field is required. " ,
"min_length" : " This field must be at least 10 characters long. "
}
},
"description.pt" : {
"label" : null ,
"rules" : {
"required" : " This field is required. " ,
"min_length" : " This field must be at least 10 characters long. "
}
}
}
}
사용자 정의 유효성 검사를 사용하고 오류 메시지를 삽입할 수 있습니다.
if ( $ myCustomValidationFail ) {
$ validator -> addError (
' someFieldName ' ,
' Some field name ' ,
[ ' no_duplicate ' => ' A user already exists with that username ' )]
);
// ...
}
값에 영문자만 포함되어 있는지 확인합니다.
$ rules = [
' nickname ' => [
' label ' => ' Nickname ' ,
' rules ' => [
RuleEnum:: ALPHA ,
],
],
];
값 | 알파 | 알파 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ✔️ | ✔️ |
'text with spaces' |
값에 영숫자 문자만 포함되어 있는지 확인합니다.
$ rules = [
' nickname ' => [
' label ' => ' Nickname ' ,
' rules ' => [
RuleEnum:: ALPHA_NUMERIC ,
],
],
];
값 | 영숫자 | 영숫자 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ✔️ | ✔️ |
0 | ✔️ | ✔️ |
false | ||
[] | ||
-1 | ||
1 | ✔️ | ✔️ |
true | ||
'text' | ✔️ | ✔️ |
'text with spaces' |
값이 두 값 사이에 있는지 확인합니다.
$ rules = [
' age ' => [
' label ' => ' Age ' ,
' rules ' => [
RuleEnum:: BETWEEN => [ 12 , 29 ],
],
],
];
값 | [1, 10] 사이 | [1, 10] 사이 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ✔️ | ✔️ |
true | ||
'some text' |
값의 문자 수가 최소값과 최대값 사이에 있는지 확인합니다.
$ rules = [
' nickname ' => [
' label ' => ' Nickname ' ,
' rules ' => [
RuleEnum:: BETWEEN_LENGTH => [ 3 , 15 ],
],
],
];
값 | between_length [5,25] | between_length [5,25] + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
12345 | ✔️ | ✔️ |
true | ||
'text' | ||
'text with 23 characters' | ✔️ | ✔️ |
값이 부울 값인지 확인합니다. 1/0, '1'/'0', 'on'/'off', 'yes'/'no', true/false에 대해 true를 반환합니다.
$ rules = [
' light ' => [
' label ' => ' Light ' ,
' rules ' => [
RuleEnum:: BOOLEAN_VALUE ,
],
],
];
값 | 부울_값 | 부울_값 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ✔️ | ✔️ |
0 | ✔️ | ✔️ |
false | ✔️ | ✔️ |
[] | ||
-1 | ||
'1' | ✔️ | ✔️ |
1 | ✔️ | ✔️ |
true | ✔️ | ✔️ |
'text' | ||
'on' | ✔️ | ✔️ |
'off' | ✔️ | ✔️ |
'yes' | ✔️ | ✔️ |
'no' | ✔️ | ✔️ |
값이 배열에 있는지 확인합니다.
$ rules = [
' priority ' => [
' label ' => ' Priority ' ,
' rules ' => [
RuleEnum:: CONTAINS => [ ' low ' , ' high ' ],
],
],
];
값 | ['낮음','높음'] 포함 | ['낮음','높음'] 포함 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'low' | ✔️ | ✔️ |
'high' | ✔️ | ✔️ |
값이 유효한 날짜(Ymd)인지 또는 사용자 정의 형식인지 확인합니다.
$ rules = [
' dob ' => [
' label ' => ' Date of birth ' ,
' rules ' => [
RuleEnum:: DATE ,
],
],
];
$ rules = [
' dob ' => [
' label ' => ' Date of birth ' ,
' rules ' => [
RuleEnum:: DATE => [ ' d.m.Y ' ],
],
],
];
값 | 날짜 [Ymd] | 날짜 [Ymd] + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'2020-09-30' | ✔️ | ✔️ |
값에 유효한 이메일 형식이 있는지 확인합니다.
$ rules = [
' email ' => [
' label ' => ' Email ' ,
' rules ' => [
RuleEnum:: EMAIL ,
],
],
];
값 | 이메일 | 이메일 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'[email protected]' | ✔️ | ✔️ |
값이 다른 값과 같은지 확인합니다.
$ rules = [
' repeat_password ' => [
' label ' => ' Repeat Password ' ,
' rules ' => [
RuleEnum:: EQUALS => [ ' password ' ],
],
],
];
'text'
와의 비교
값 | 같음 | 같음 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ✔️ | ✔️ |
'another text' |
파일 크기가 지정된 값을 초과하지 않는지 확인합니다.
값을 바이트 단위로 입력하세요.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: FILE_MAX_SIZE => [ 102400 ],
],
],
];
102400
바이트와 비교
값 | file_max_size | file_max_size + 필수 |
---|---|---|
null | ✔️ | |
50000 | ✔️ | ✔️ |
150000 |
파일 형식이 허용되는지 확인합니다.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: FILE_MIME_TYPE => [ ' image/png ' , ' image/jpeg ' ],
],
],
];
값 | file_mime_type | file_mime_type + 필수 |
---|---|---|
null | ✔️ | |
image/png | ✔️ | ✔️ |
text/plain |
값이 부동 소수점 숫자인지 확인합니다.
$ rules = [
' price ' => [
' label ' => ' Price ' ,
' rules ' => [
RuleEnum:: FLOAT_NUMBER ,
],
],
];
값 | float_number | float_number + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
12345 | ||
123.45 | ✔️ | ✔️ |
true | ||
'text' | ||
'text with spaces' |
이미지 높이가 지정된 값을 초과하지 않는지 확인합니다.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: IMAGE_MAX_HEIGHT => [ 600 ],
],
],
];
값 | image_max_height | image_max_height + 필수 |
---|---|---|
null | ✔️ | |
500px | ✔️ | ✔️ |
1000px |
이미지 너비가 지정된 값을 초과하지 않는지 확인합니다.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: IMAGE_MAX_WIDTH => [ 1000 ],
],
],
];
값 | 이미지_최대_너비 | image_max_width + 필수 |
---|---|---|
null | ✔️ | |
500px | ✔️ | ✔️ |
1500px |
이미지 높이가 지정된 값보다 작지 않은지 확인합니다.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: IMAGE_MIN_HEIGHT => [ 300 ],
],
],
];
값 | 이미지_최소_높이 | image_min_height + 필수 |
---|---|---|
null | ✔️ | |
100px | ||
500px | ✔️ | ✔️ |
이미지 너비가 지정된 값보다 작지 않은지 확인합니다.
$ rules = [
' profile_photo ' => [
' label ' => ' Profile Photo ' ,
' rules ' => [
RuleEnum:: IMAGE_MIN_WIDTH => [ 500 ],
],
],
];
값 | 이미지_최소_너비 | image_min_width + 필수 |
---|---|---|
null | ✔️ | |
400px | ||
600px | ✔️ | ✔️ |
값이 정수인지 확인합니다.
$ rules = [
' distance ' => [
' label ' => ' Distance ' ,
' rules ' => [
RuleEnum:: INTEGER ,
],
],
];
값 | 숫자 | 숫자 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ✔️ | ✔️ |
false | ||
[] | ||
-1 | ✔️ | ✔️ |
1 | ✔️ | ✔️ |
true | ||
'text' |
값이 유효한 IP 주소인지 확인합니다.
$ rules = [
' ip ' => [
' label ' => ' IP ' ,
' rules ' => [
RuleEnum:: IP ,
],
],
];
값 | 아이피 | IP + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'10.10.10.10' | ✔️ | ✔️ |
'2607:f0d0:1002:51::4' | ✔️ | ✔️ |
값이 유효한 IPv4 주소인지 확인합니다.
$ rules = [
' ipv4 ' => [
' label ' => ' IPv4 ' ,
' rules ' => [
RuleEnum:: IPV4 ,
],
],
];
값 | IPv4 | IPv4 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'10.10.10.10' | ✔️ | ✔️ |
'2607:f0d0:1002:51::4' |
값이 유효한 IPv6 주소인지 확인합니다.
$ rules = [
' ipv6 ' => [
' label ' => ' IPv6 ' ,
' rules ' => [
RuleEnum:: IPV6 ,
],
],
];
값 | IPv6 | IPv6 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'10.10.10.10' | ||
'2607:f0d0:1002:51::4' | ✔️ | ✔️ |
값이 주어진 값을 초과하지 않는지 확인합니다.
$ rules = [
' people ' => [
' label ' => ' People ' ,
' rules ' => [
RuleEnum:: MAX => [ 5 ],
],
],
];
값 | 최대 | 최대 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ✔️ | ✔️ |
0 | ✔️ | ✔️ |
false | ||
[] | ||
-1 | ✔️ | ✔️ |
1 | ✔️ | ✔️ |
true | ||
'text' | ||
12345 | ||
'12345' |
값의 문자 수가 주어진 값을 초과하지 않는지 확인합니다.
$ rules = [
' nickname ' => [
' label ' => ' Nickname ' ,
' rules ' => [
RuleEnum:: MAX_LENGTH => [ 2 ],
],
],
];
값 | 최대 길이 | max_length + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ✔️ | ✔️ |
0 | ✔️ | ✔️ |
false | ✔️ | ✔️ |
[] | ||
-1 | ✔️ | ✔️ |
1 | ✔️ | ✔️ |
true | ✔️ | ✔️ |
'text' | ||
12345 | ||
'12345' |
값이 주어진 값보다 작지 않은지 확인합니다.
$ rules = [
' people ' => [
' label ' => ' People ' ,
' rules ' => [
RuleEnum:: MIN => [ 2 ],
],
],
];
값 | 분 | 최소 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
12345 | ✔️ | ✔️ |
'12345' | ✔️ | ✔️ |
값의 문자 수가 주어진 값보다 작지 않은지 확인합니다.
$ rules = [
' nickname ' => [
' label ' => ' Nickname ' ,
' rules ' => [
RuleEnum:: MIN_LENGTH => [ 2 ],
],
],
];
값 | 최대 길이 | max_length + 필수 |
---|---|---|
null | ✔️ | |
'' | ||
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ✔️ | ✔️ |
1 | ||
true | ||
'text' | ✔️ | ✔️ |
12345 | ✔️ | ✔️ |
'12345' | ✔️ | ✔️ |
값이 숫자인지 확인합니다.
$ rules = [
' age ' => [
' label ' => ' Age ' ,
' rules ' => [
RuleEnum:: NUMERIC ,
],
],
];
값 | 숫자 | 숫자 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ✔️ | ✔️ |
0 | ✔️ | ✔️ |
false | ||
[] | ||
-1 | ✔️ | ✔️ |
1 | ✔️ | ✔️ |
true | ||
'text' |
값이 주어진 정규식과 일치하는지 확인합니다.
$ rules = [
' path ' => [
' label ' => ' Path ' ,
' rules ' => [
RuleEnum:: REGEX => [ ' //client/[0-9a-f]+$/ ' ],
],
],
];
'//client/[0-9a-f]+$/'
패턴을 사용한 검증
값 | 정규식 | 정규식 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'/client/77c9e105d1f548b29958f0512967de87' | ✔️ | ✔️ |
'/client/invalid-uuid' |
값이 비어 있지 않은지 확인합니다.
$ rules = [
' name ' => [
' label ' => ' Name ' ,
' rules ' => [
RuleEnum:: REQUIRED ,
],
],
];
값 | 필수의 |
---|---|
null | |
'' | |
'0' | ✔️ |
0 | ✔️ |
false | ✔️ |
[] | |
-1 | ✔️ |
1 | ✔️ |
true | ✔️ |
'some text' | ✔️ |
값이 유효한 Slug인지 확인합니다(예: hello-world_123).
$ rules = [
' slug ' => [
' label ' => ' Slug ' ,
' rules ' => [
RuleEnum:: SLUG ,
],
],
];
값 | 강타 | 슬러그 + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ✔️ | ✔️ |
'text with spaces' | ||
'hello-world_123' | ✔️ | ✔️ |
값이 유효한 URL인지 확인합니다.
$ rules = [
' url ' => [
' label ' => ' URL ' ,
' rules ' => [
RuleEnum:: URL ,
],
],
];
값 | URL | URL + 필수 |
---|---|---|
null | ✔️ | |
'' | ✔️ | |
'0' | ||
0 | ||
false | ||
[] | ||
-1 | ||
1 | ||
true | ||
'text' | ||
'http://www.some-domain.com' | ✔️ | ✔️ |
기여하고 싶나요? 모든 기여를 환영합니다. 기여 가이드를 읽어보세요.
질문이 있는 경우 @sandro_m_m으로 저를 트윗하거나 이슈를 열어주세요.
이 프로젝트는 MIT 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 LICENSE 파일을 참조하세요.
***나눔은 배려입니다 ~**