PHP 클래스 Gdn_Validation, vanilla

파일 보기 프로젝트 열기: vanilla/vanilla 1 사용 예제들

보호된 프로퍼티들

프로퍼티 타입 설명
$_FieldRules An associative array of $FieldName => array($RuleName1, $RuleNameN) rules to be applied to fields. These are rules that have been explicitly called with {@link Gdn_Validation::ApplyRule()}.
$_ResetOnValidate Whether or not to reset the validation results on validate.
$_Rules The collection of validation rules in the format of $RuleName => $Rule. This list can be added to with $this->AddRule($RuleName, $Rule).
$_Schema The schema being used to generate validation rules.
$_SchemaRules An associative array of $FieldName => array($RuleName1, $RuleNameN) rules to be applied to fields. These are rules that come from the current schema that have been applied by {@link Gdn_Validation::ApplyRulesBySchema()}.
$_ValidationFields An associative array of fieldname => value pairs that are being validated. In order for a field to become a part of this collection, it must either be present in the defined schema, or have a rule defined in $this->_FieldRules.
$_ValidationResults An array of FieldName => Reason arrays that describe which fields failed validation and which functions/regex caused them to fail.

공개 메소드들

메소드 설명
__construct ( Gdn_Schema | array $Schema = false, $ResetOnValidate = false ) Class constructor. Optionally takes a schema definition to generate validation rules for.
addRule ( string $RuleName, string $Rule ) Adds to the rules collection ($this->_Rules).
addValidationResult ( string $FieldName, string $ErrorCode = '' ) Add a validation result (error) to the validation.
applyRule ( string $FieldName, mixed $RuleName, mixed $CustomError = '' ) Applies a $RuleName to a $FieldName. You can apply as many rules to a field as you like.
applySchema ( array $Schema ) Allows the explicit definition of a schema to use.
getSchemaRules ( ) : array Get all of the rules as defined by the schema.
reset ( ) Reset the validation results to an empty array.
resetOnValidate ( ) : boolean Whether or not the validation results etc should reset whenever {@link Validate()} is called.
results ( boolean $Reset = false ) : array Returns the $this->_ValidationResults array. You must use this method because the array is read-only outside this object.
resultsArray ( ) : array Get the validation results as an array of error messages.
resultsAsArray ( array $results ) : array Expand the validation results into a single-dimension array.
resultsAsText ( array $results ) : string Format an array of validation results as a string.
resultsText ( ) : string Get the validation results as a string of text.
schemaValidationFields ( ) : array Returns an array of field names that are in both $this->_ValidationFields AND $this->_Schema.
setResetOnValidate ( boolean $ResetOnValidate ) : Gdn_Validation Set whether or not the validation results etc should reset whenever {@link Validate()} is called.
setSchema ( Gdn_Schema | array $Schema ) : Gdn_Validation Set the schema for this validation.
setSchemaProperty ( string $FieldName, string $PropertyName, mixed $Value ) Allows you to explicitly set a field property on $this->_Schema. Can be useful when adding rules to fields (ie. a maxlength property on a db's text field).
unapplyRule ( $FieldName, boolean $RuleName = false ) Remove a validation rule that was added with {@link Gdn_Validation::ApplyRule()}.
validate ( array $PostedFields, boolean $Insert = false ) : boolean Examines the posted fields, defines $this->_ValidationFields, and enforces the $this->Rules collection on them.
validateRule ( mixed $Value, string $FieldName, string | array $Rule, string $CustomError = false ) : boolean | string Execute a single validation rule and return its result.
validationFields ( ) : array Returns the an array of fieldnames that are being validated.

보호된 메소드들

메소드 설명
addValidationField ( string $FieldName, array $PostedFields ) Adds a fieldname to the $this->_ValidationFields collection.
applyRuleTo ( array &$Array, string $FieldName, string $RuleName, string $CustomError = '' ) Apply a rule to the given rules array.
applyRulesBySchema ( ) Examine the current schema and fill {@link Gdn_Validation::$_SchemaRules}.
defineValidationFields ( array $PostedFields, boolean $Insert = false ) : array Fills $this->_ValidationFields with field names that exist in the $PostedFields collection.
defineValidationRules ( array $PostedFields, boolean $Insert = false ) : array Get all of the validation rules that apply to a given set of data.

메소드 상세

__construct() 공개 메소드

Class constructor. Optionally takes a schema definition to generate validation rules for.
public __construct ( Gdn_Schema | array $Schema = false, $ResetOnValidate = false )
$Schema Gdn_Schema | array A schema object to generate validation rules for.

addRule() 공개 메소드

If $RuleName already exists, this method will overwrite the existing rule. There are some special cases: 1. If the $Rule begins with "function:", when the rule is evaluated on a field, it will strip the "function:" from the $Rule and execute the remaining string name as a function with the field value passed as the first parameter and the related field properties as the second parameter. ie. "function:MySpecialValidation" will evaluate as MySpecialValidation($FieldValue, $FieldProperties). Any function defined in this way is expected to return boolean TRUE or FALSE. 2. If $Rule begins with "regex:", when the rule is evaluated on a field, it will strip the "regex:" from $Rule and use the remaining string as a regular expression rule. If a match between the regex rule and the field value is made, it will validate as TRUE. 3. Predefined $RuleNames are: RuleName Rule ======================================================================== Required Will not accept a null or empty value. Email Will validate against an email regex. Date Will only accept valid date values in a variety of formats. Integer Will only accept an integer. Boolean Will only accept 1 or 0. Decimal Will only accept a decimal. Time Will only accept a time in HH:MM:SS or HH:MM format. Timestamp Will only accept a valid timestamp. Length Will not accept a value longer than $Schema[$Field]->Length. Enum Will only accept one of the values in the $Schema[$Field]->Enum array.
public addRule ( string $RuleName, string $Rule )
$RuleName string The name of the rule to be added.
$Rule string The rule to be added. These are in the format of "function:FunctionName" or "regex:/regex/". Any function defined here must be included before the rule is enforced or the application will cause a fatal error.

addValidationField() 보호된 메소드

Adds a fieldname to the $this->_ValidationFields collection.
protected addValidationField ( string $FieldName, array $PostedFields )
$FieldName string The name of the field to add to the $this->_ValidationFields collection.
$PostedFields array The associative array collection of field names to examine for the value of $FieldName.

addValidationResult() 공개 메소드

Add a validation result (error) to the validation.
public addValidationResult ( string $FieldName, string $ErrorCode = '' )
$FieldName string The name of the form field that has the error.
$ErrorCode string The translation code of the error. Codes that begin with an '@' symbol are treated as literals and not translated.

applyRule() 공개 메소드

Applies a $RuleName to a $FieldName. You can apply as many rules to a field as you like.
public applyRule ( string $FieldName, mixed $RuleName, mixed $CustomError = '' )
$FieldName string The name of the field to apply rules to.
$RuleName mixed The rule name (or array of rule names) to apply to the field.
$CustomError mixed A custom error message you might want to apply to a field if the rule causes an error to be caught.

applyRuleTo() 보호된 메소드

Apply a rule to the given rules array.
protected applyRuleTo ( array &$Array, string $FieldName, string $RuleName, string $CustomError = '' )
$Array array The rules array to apply the rule to. This should be either `$this->_FieldRules` or `$this->_SchemaRules`.
$FieldName string The name of the field that the rule applies to.
$RuleName string The name of the rule.
$CustomError string A custom error string when the rule is broken.

applyRulesBySchema() 보호된 메소드

The {@link Gdn_Validation::$_SchemaRules} are filled with rules based on the properties of each field in the table schema.
protected applyRulesBySchema ( )

applySchema() 공개 메소드

Allows the explicit definition of a schema to use.
사용 중단: This method has been deprecated in favor of {@link Gdn_Validation::SetSchema()}.
public applySchema ( array $Schema )
$Schema array

defineValidationFields() 보호된 메소드

Fills $this->_ValidationFields with field names that exist in the $PostedFields collection.
protected defineValidationFields ( array $PostedFields, boolean $Insert = false ) : array
$PostedFields array The associative array collection of field names to add.
$Insert boolean A boolean value indicating if the posted fields are to be inserted or updated. If being inserted, the schema's required field rules will be enforced.
리턴 array Returns the subset of {@link $PostedFields} that will be validated.

defineValidationRules() 보호된 메소드

Get all of the validation rules that apply to a given set of data.
protected defineValidationRules ( array $PostedFields, boolean $Insert = false ) : array
$PostedFields array The data that will be validated.
$Insert boolean Whether or not this is an insert.
리턴 array Returns an array of `[$Field => [$Rules, ...]`.

getSchemaRules() 공개 메소드

Get all of the rules as defined by the schema.
public getSchemaRules ( ) : array
리턴 array Returns an array in the form `[$FieldName => [$Rules, ...]`.

reset() 공개 메소드

Reset the validation results to an empty array.
public reset ( )

resetOnValidate() 공개 메소드

Whether or not the validation results etc should reset whenever {@link Validate()} is called.
public resetOnValidate ( ) : boolean
리턴 boolean Returns true if we reset or false otherwise.

results() 공개 메소드

Returns the $this->_ValidationResults array. You must use this method because the array is read-only outside this object.
public results ( boolean $Reset = false ) : array
$Reset boolean Whether or not to clear the validation results.
리턴 array Returns an array of validation results (errors).

resultsArray() 공개 메소드

Get the validation results as an array of error messages.
public resultsArray ( ) : array
리턴 array Returns an array of error messages or an empty array if there are no errors.

resultsAsArray() 공개 정적인 메소드

Expand the validation results into a single-dimension array.
public static resultsAsArray ( array $results ) : array
$results array The validation results to expand.
리턴 array Returns an array of error messages.

resultsAsText() 공개 정적인 메소드

Format an array of validation results as a string.
public static resultsAsText ( array $results ) : string
$results array An array of validation results returned from {@link Gdn_Validation::Results()}.
리턴 string Returns the validation results as a string.

resultsText() 공개 메소드

Get the validation results as a string of text.
public resultsText ( ) : string
리턴 string Returns the validation results.

schemaValidationFields() 공개 메소드

Returns an array of field names that are in both $this->_ValidationFields AND $this->_Schema.
public schemaValidationFields ( ) : array
리턴 array Returns an array of fields and values that were validated and in the schema.

setResetOnValidate() 공개 메소드

Set whether or not the validation results etc should reset whenever {@link Validate()} is called.
public setResetOnValidate ( boolean $ResetOnValidate ) : Gdn_Validation
$ResetOnValidate boolean True to reset or false otherwise.
리턴 Gdn_Validation Returns `$this` for fluent calls.

setSchema() 공개 메소드

Set the schema for this validation.
public setSchema ( Gdn_Schema | array $Schema ) : Gdn_Validation
$Schema Gdn_Schema | array The new schema to set.
리턴 Gdn_Validation Returns `$this` for fluent calls.

setSchemaProperty() 공개 메소드

Allows you to explicitly set a field property on $this->_Schema. Can be useful when adding rules to fields (ie. a maxlength property on a db's text field).
public setSchemaProperty ( string $FieldName, string $PropertyName, mixed $Value )
$FieldName string The name of the field that we are setting a property for.
$PropertyName string The name of the property being set.
$Value mixed The value of the property to set.

unapplyRule() 공개 메소드

Remove a validation rule that was added with {@link Gdn_Validation::ApplyRule()}.
public unapplyRule ( $FieldName, boolean $RuleName = false )
$FieldName
$RuleName boolean

validate() 공개 메소드

Examines the posted fields, defines $this->_ValidationFields, and enforces the $this->Rules collection on them.
public validate ( array $PostedFields, boolean $Insert = false ) : boolean
$PostedFields array An associative array of posted fields to be validated.
$Insert boolean A boolean value indicating if the posted fields are to be inserted or updated. If being inserted, the schema's required field rules will be enforced.
리턴 boolean Whether or not the validation was successful.

validateRule() 공개 정적인 메소드

Execute a single validation rule and return its result.
public static validateRule ( mixed $Value, string $FieldName, string | array $Rule, string $CustomError = false ) : boolean | string
$Value mixed The value to validate.
$FieldName string The name of the field to put into the error result.
$Rule string | array The rule to validate which can be one of the following. - string: The name of a function used to validate the value. - 'regex:': The regular expression used to validate the value. - array: An array with the following keys: - Name: The name of the function used to validate. - Args: An argument to pass to the function after the value.
$CustomError string A custom error message.
리턴 boolean | string One of the following - TRUE: The value passed validation. - string: The error message associated with the error.

validationFields() 공개 메소드

Returns the an array of fieldnames that are being validated.
public validationFields ( ) : array
리턴 array

프로퍼티 상세

$_FieldRules 보호되어 있는 프로퍼티

An associative array of $FieldName => array($RuleName1, $RuleNameN) rules to be applied to fields. These are rules that have been explicitly called with {@link Gdn_Validation::ApplyRule()}.
protected $_FieldRules

$_ResetOnValidate 보호되어 있는 프로퍼티

Whether or not to reset the validation results on validate.
protected $_ResetOnValidate

$_Rules 보호되어 있는 프로퍼티

The collection of validation rules in the format of $RuleName => $Rule. This list can be added to with $this->AddRule($RuleName, $Rule).
protected $_Rules

$_Schema 보호되어 있는 프로퍼티

The schema being used to generate validation rules.
protected $_Schema

$_SchemaRules 보호되어 있는 프로퍼티

An associative array of $FieldName => array($RuleName1, $RuleNameN) rules to be applied to fields. These are rules that come from the current schema that have been applied by {@link Gdn_Validation::ApplyRulesBySchema()}.
protected $_SchemaRules

$_ValidationFields 보호되어 있는 프로퍼티

An associative array of fieldname => value pairs that are being validated. In order for a field to become a part of this collection, it must either be present in the defined schema, or have a rule defined in $this->_FieldRules.
protected $_ValidationFields

$_ValidationResults 보호되어 있는 프로퍼티

An array of FieldName => Reason arrays that describe which fields failed validation and which functions/regex caused them to fail.
protected $_ValidationResults