本文整理汇总了PHP中Doctrine_Validator::validateLength方法的典型用法代码示例。如果您正苦于以下问题:PHP Doctrine_Validator::validateLength方法的具体用法?PHP Doctrine_Validator::validateLength怎么用?PHP Doctrine_Validator::validateLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine_Validator
的用法示例。
在下文中一共展示了Doctrine_Validator::validateLength方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testIsValidLength
public function testIsValidLength()
{
$binaryValue = fread(fopen(__FILE__, 'r'), filesize(__FILE__));
//Should pass with size the same size as maximum size
$this->assertTrue(Doctrine_Validator::validateLength($binaryValue, "blob", filesize(__FILE__)));
//Should fail with maximum size 1 less than actual file size
$this->assertFalse(Doctrine_Validator::validateLength($binaryValue, "blob", filesize(__FILE__) - 1));
}
示例2: validateField
/**
* validateField
*
* @param string $name
* @param string $value
* @param Doctrine_Record $record
* @return Doctrine_Validator_ErrorStack $errorStack
*/
public function validateField($fieldName, $value, Doctrine_Record $record = null)
{
if ($record instanceof Doctrine_Record) {
$errorStack = $record->getErrorStack();
} else {
$record = $this->create();
$errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
}
if ($value === self::$_null) {
$value = null;
} else {
if ($value instanceof Doctrine_Record && $value->exists()) {
$value = $value->getIncremented();
} else {
if ($value instanceof Doctrine_Record && !$value->exists()) {
foreach ($this->getRelations() as $relation) {
if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
return $errorStack;
}
}
}
}
}
$dataType = $this->getTypeOf($fieldName);
// Validate field type, if type validation is enabled
if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_TYPES) {
if (!Doctrine_Validator::isValidType($value, $dataType)) {
$errorStack->add($fieldName, 'type');
}
if ($dataType == 'enum') {
$enumIndex = $this->enumIndex($fieldName, $value);
if ($enumIndex === false && $value !== null) {
$errorStack->add($fieldName, 'enum');
}
}
}
// Validate field length, if length validation is enabled
if ($this->getAttribute(Doctrine::ATTR_VALIDATE) & Doctrine::VALIDATE_LENGTHS) {
if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
$errorStack->add($fieldName, 'length');
}
}
// Run all custom validators
foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
if (!is_string($validatorName)) {
$validatorName = $args;
$args = array();
}
$validator = Doctrine_Validator::getValidator($validatorName);
$validator->invoker = $record;
$validator->field = $fieldName;
$validator->args = $args;
if (!$validator->validate($value)) {
$errorStack->add($fieldName, $validator);
}
}
return $errorStack;
}
示例3: validateField
/**
* Validates a given field using table ATTR_VALIDATE rules.
* @see Doctrine_Core::ATTR_VALIDATE
*
* @param string $fieldName
* @param string $value
* @param Doctrine_Record $record record to consider; if it does not exists, it is created
* @return Doctrine_Validator_ErrorStack $errorStack
*/
public function validateField($fieldName, $value, Doctrine_Record $record = null)
{
if ($record instanceof Doctrine_Record) {
$errorStack = $record->getErrorStack();
} else {
$record = $this->create();
$errorStack = new Doctrine_Validator_ErrorStack($this->getOption('name'));
}
if ($value === self::$_null) {
$value = null;
} else {
if ($value instanceof Doctrine_Record && $value->exists()) {
$value = $value->getIncremented();
} else {
if ($value instanceof Doctrine_Record && !$value->exists()) {
foreach ($this->getRelations() as $relation) {
if ($fieldName == $relation->getLocalFieldName() && (get_class($value) == $relation->getClass() || is_subclass_of($value, $relation->getClass()))) {
return $errorStack;
}
}
} elseif (array_key_exists($fieldName, $this->getRelationIdentifiers()) && $record !== null) {
$r = $this->_relationIdentifiers[$fieldName];
// Related record is not saved yet
if (!$record->hasReference($r) || !$record->{$r}->exists()) {
return $errorStack;
}
}
}
}
$dataType = $this->getTypeOf($fieldName);
// Validate field type, if type validation is enabled
if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_TYPES) {
if (!Doctrine_Validator::isValidType($value, $dataType)) {
$errorStack->add($fieldName, 'type');
}
if ($dataType == 'enum') {
$enumIndex = $this->enumIndex($fieldName, $value);
if ($enumIndex === false && $value !== null) {
$errorStack->add($fieldName, 'enum');
}
}
if ($dataType == 'set') {
$values = $this->_columns[$fieldName]['values'];
// Convert string to array
if (is_string($value)) {
$value = explode(',', $value);
foreach ($value as &$v) {
$v = trim($v);
}
$record->set($fieldName, $value);
}
// Make sure each set value is valid
foreach ($value as $k => $v) {
if (!in_array($v, $values)) {
$errorStack->add($fieldName, 'set');
}
}
}
}
// Validate field length, if length validation is enabled
if ($this->getAttribute(Doctrine_Core::ATTR_VALIDATE) & Doctrine_Core::VALIDATE_LENGTHS) {
if (!Doctrine_Validator::validateLength($value, $dataType, $this->getFieldLength($fieldName))) {
$errorStack->add($fieldName, 'length');
}
}
// Run all custom validators
foreach ($this->getFieldValidators($fieldName) as $validatorName => $args) {
if (!is_string($validatorName)) {
$validatorName = $args;
$args = array();
}
$validator = Doctrine_Validator::getValidator($validatorName);
$validator->invoker = $record;
$validator->field = $fieldName;
$validator->args = $args;
if (!$validator->validate($value)) {
$errorStack->add($fieldName, $validator);
}
}
return $errorStack;
}