2 개의 텍스트 필드가있는 양식, 최소 금액은 & 최대 금액입니다. 텍스트 필드 "최소 금액"의 값을 텍스트 필드 "최대 값"의 값과 비교할 수 있는지, 그리고 Zendform의 유효성 검사기를 사용하여 그 값을 비교할 수 있는지 알고 싶습니다.젠드 양식을 사용하여 두 텍스트 필드 사이의 값을 비교하는 방법은 무엇입니까?
0
A
답변
1
그냥 유효성 검사기의 isValid 메서드는 유효성 검사기가 연결된 필드의 값과 다른 모든 양식 필드의 값 배열 인 양식의 전체 컨텍스트 값을 가져옵니다. 추가적으로 컨텍스트 필드를 설정하는 검사기에 기능을 추가하고, 심지어 더 적은 같 더 이상되어야 하는지를 비교하는 기능 ... 여기
(안 시험) 일부 예 코드
<?php
namespace My\Validator;
class MinMaxComp extends AbstractValidator
{
const ERROR_NOT_SMALLER = 'not_smaller';
const ERROR_NOT_GREATER = 'not_greater';
const ERROR_CONFIG = 'wrong_config';
const TYPE_SMALLER = 0;
const TYPE_GREATER = 1;
protected $messageTemplates = array(
self::ERROR_NOT_SMALLER => "Blah",
self::ERROR_NOT_GREATER => "Blah",
self::WRONG_CONFIG => "Blah",
);
protected $type;
protected $contextField;
public function setType($type)
{
$this->type = $type;
}
public function setContextField($fieldName)
{
$this->contextField = $fieldName;
}
public function isValid($value, $context = null)
{
$this->setValue($value);
if (!is_array($context) || !isset($context[$this->contextField])) {
return false;
}
if ($this->type === self::TYPE_SMALLER) {
if (!$result = ($value < $context[$this->contextField])) {
$this->error(self::ERROR_NOT_SMALLER);
}
} else if ($this->type === self::TYPE_GREATER) {
if (!$result = ($value > $context[$this->contextField])) {
$this->error(self::ERROR_NOT_GREATER);
}
} else {
$result = false;
$this->error(self::ERROR_CONFIG);
}
return $result;
}
}
전달할 수
3
사용자 지정 유효성 검사를 만들어야합니다.
이 게시물의 기능은 My_Validate_FieldCompare에 도움이 될 것이라고 생각합니다.