Documentation

CRM_Utils_Rule
in package

Class CRM_Utils_Rule

Table of Contents

Methods

alphanumeric()  : bool
Test whether $value is alphanumeric.
asciiFile()  : bool
See how file rules are written in HTML/QuickForm/file.php Checks to make sure the uploaded file is ascii
boolean()  : bool
checkExtensionKeyIsValid()  : bool
cleanMoney()  : string
Strip thousand separator from a money string.
color()  : bool
Strict validation of 6-digit hex color notation per html5 <input type="color">
commaSeparatedIntegers()  : bool
creditCardNumber()  : bool
currencyCode()  : bool
currentDate()  : bool
Check the validity of the date (in qf format) note that only a year is valid, or a mon-year is also valid in addition to day-mon-year. The date specified has to be beyond today. (i.e today or later)
cvv()  : bool
date()  : string|null
dateTime()  : string|null
domain()  : bool
email()  : bool
emailList()  : bool
fileExists()  : bool
integer()  : bool
json()  : bool
Validate json string for xss
longTitle()  : bool
money()  : bool
mysqlColumnNameOrAlias()  : bool
Validate that a string is a valid MySQL column name or alias.
mysqlDate()  : bool
Check the validity of a date or datetime (timestamp) value which is in YYYYMMDD or YYYYMMDDHHMMSS format
mysqlOrderBy()  : bool
Validate that a string is valid order by clause.
mysqlOrderByDirection()  : bool
Validate that a string is ASC or DESC.
numberOfDigit()  : bool
numeric()  : bool
objectExists()  : bool
Check if there is a record with the same name in the db.
optionExists()  : bool
phone()  : bool
positiveInteger()  : bool
postalCode()  : bool
allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or if u disregard the first 0, 4 (thanx excel!) FIXME: we need to figure out how to localize such rules
qfDate()  : bool
Check the validity of the date (in qf format) note that only a year is valid, or a mon-year is also valid in addition to day-mon-year
qfKey()  : bool
qfVariable()  : bool
query()  : bool
settingPath()  : bool
Determine whether the value contains a valid reference to a directory.
string()  : bool
title()  : bool
url()  : bool
urlish()  : bool
utf8File()  : bool
Checks to make sure the uploaded file is in UTF-8, recodes if it's not
validContact()  : bool
validDateRange()  : mixed
Check if the values in the date range are in correct chronological order.
variable()  : bool
wikiURL()  : bool
arrayValue()  : bool
Validate array recursively checking keys and values.
idnToAsci()  : string|false
Convert domain string to ascii.

Methods

alphanumeric()

Test whether $value is alphanumeric.

public static alphanumeric(string $value) : bool

Underscores and dashes are also allowed!

This is the type of string you could expect to see in URL parameters like ?mode=live vs ?mode=test. This function exists so that we can be strict about what we accept for such values, thus mitigating against potential security issues.

Parameters
$value : string
Tags
see
CRM_Utils_RuleTest::alphanumericData

for examples of vales that give TRUE/FALSE here

Return values
bool

asciiFile()

See how file rules are written in HTML/QuickForm/file.php Checks to make sure the uploaded file is ascii

public static asciiFile(string $elementValue) : bool
Parameters
$elementValue : string
Return values
bool

True if file has been uploaded, false otherwise

boolean()

public static boolean(bool|string $value) : bool
Parameters
$value : bool|string
Return values
bool

checkExtensionKeyIsValid()

public static checkExtensionKeyIsValid([string $key = NULL ]) : bool
Parameters
$key : string = NULL

Extension Key to check

Return values
bool

cleanMoney()

Strip thousand separator from a money string.

public static cleanMoney(string $value) : string

Note that this should be done at the form layer. Once we are processing money at the BAO or processor layer we should be working with something that is already in a normalised format.

Parameters
$value : string
Return values
string

color()

Strict validation of 6-digit hex color notation per html5 <input type="color">

public static color(string $value) : bool
Parameters
$value : string
Return values
bool

commaSeparatedIntegers()

public static commaSeparatedIntegers(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

creditCardNumber()

public static creditCardNumber(string $value, string $type) : bool
Parameters
$value : string
$type : string
Return values
bool

currencyCode()

public static currencyCode(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

currentDate()

Check the validity of the date (in qf format) note that only a year is valid, or a mon-year is also valid in addition to day-mon-year. The date specified has to be beyond today. (i.e today or later)

public static currentDate(array<string|int, mixed> $date[, bool $monthRequired = TRUE ]) : bool
Parameters
$date : array<string|int, mixed>
$monthRequired : bool = TRUE

Check whether month is mandatory.

Return values
bool

true if valid date

cvv()

public static cvv(string $value, string $type) : bool
Parameters
$value : string
$type : string
Return values
bool

date()

public static date(string $value[, string|null $default = NULL ]) : string|null
Parameters
$value : string
$default : string|null = NULL
Return values
string|null

dateTime()

public static dateTime(string $value[, string|null $default = NULL ]) : string|null
Parameters
$value : string
$default : string|null = NULL
Return values
string|null

domain()

public static domain(string $domain) : bool
Parameters
$domain : string
Return values
bool

email()

public static email(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

emailList()

public static emailList(string $list) : bool
Parameters
$list : string
Return values
bool

fileExists()

public static fileExists(string $path) : bool
Parameters
$path : string
Return values
bool

integer()

public static integer(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

json()

Validate json string for xss

public static json(string $value) : bool
Parameters
$value : string
Return values
bool

False if invalid, true if valid / safe.

longTitle()

public static longTitle(string|null $str) : bool
Parameters
$str : string|null
Return values
bool

money()

public static money(string $value[, bool $checkSeparatorOrder = FALSE ]) : bool
Parameters
$value : string
$checkSeparatorOrder : bool = FALSE

Should the order of the separators be checked. ie if the thousand separator is , then it should never be after the decimal separator . so 1.300,23 would be invalid in that case. Honestly I'm amazed this check wasn't being done but in the interest of caution adding as opt in. Note clean money would convert this to 1.30023....

Return values
bool

mysqlColumnNameOrAlias()

Validate that a string is a valid MySQL column name or alias.

public static mysqlColumnNameOrAlias(string|null $str) : bool
Parameters
$str : string|null
Return values
bool

mysqlDate()

Check the validity of a date or datetime (timestamp) value which is in YYYYMMDD or YYYYMMDDHHMMSS format

public static mysqlDate(string $date) : bool

Uses PHP checkdate() - params are ( int $month, int $day, int $year )

Parameters
$date : string
Return values
bool

true if valid date

mysqlOrderBy()

Validate that a string is valid order by clause.

public static mysqlOrderBy(string $str) : bool
Parameters
$str : string
Return values
bool

mysqlOrderByDirection()

Validate that a string is ASC or DESC.

public static mysqlOrderByDirection(string $str) : bool

Empty string should be treated as invalid and ignored => default = ASC.

Parameters
$str : string
Return values
bool

numberOfDigit()

public static numberOfDigit(string $value, int $noOfDigit) : bool
Parameters
$value : string
$noOfDigit : int
Return values
bool

numeric()

public static numeric(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

objectExists()

Check if there is a record with the same name in the db.

public static objectExists(string $value, array<string|int, mixed> $options) : bool
Parameters
$value : string

The value of the field we are checking.

$options : array<string|int, mixed>

The daoName, fieldName (optional) and DomainID (optional).

Return values
bool

true if object exists

optionExists()

public static optionExists(mixed $value, mixed $options) : bool
Parameters
$value : mixed
$options : mixed
Return values
bool

phone()

public static phone(string|null $phone) : bool
Parameters
$phone : string|null
Return values
bool

positiveInteger()

public static positiveInteger(mixed $value) : bool
Parameters
$value : mixed
Return values
bool

postalCode()

allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or if u disregard the first 0, 4 (thanx excel!) FIXME: we need to figure out how to localize such rules

public static postalCode(string $value) : bool
Parameters
$value : string
Return values
bool

qfDate()

Check the validity of the date (in qf format) note that only a year is valid, or a mon-year is also valid in addition to day-mon-year

public static qfDate(array<string|int, mixed> $date) : bool
Parameters
$date : array<string|int, mixed>
Return values
bool

true if valid date

qfKey()

public static qfKey(mixed $key) : bool
Parameters
$key : mixed
Return values
bool

qfVariable()

public static qfVariable(string $str) : bool
Parameters
$str : string
Return values
bool

query()

public static query(string|null $query) : bool
Parameters
$query : string|null
Return values
bool

settingPath()

Determine whether the value contains a valid reference to a directory.

public static settingPath(string $path) : bool

Paths stored in the setting system may be absolute -- or may be relative to the default data directory.

Parameters
$path : string
Return values
bool

string()

public static string(mixed $value[, int $maxLength = 0 ]) : bool
Parameters
$value : mixed
$maxLength : int = 0
Return values
bool

title()

public static title(string|null $str[, int $maxLength = 127 ]) : bool
Parameters
$str : string|null
$maxLength : int = 127
Return values
bool

url()

public static url(string|null $url) : bool
Parameters
$url : string|null
Return values
bool

urlish()

public static urlish(string|null $url) : bool
Parameters
$url : string|null
Return values
bool

utf8File()

Checks to make sure the uploaded file is in UTF-8, recodes if it's not

public static utf8File(array<string|int, mixed> $elementValue) : bool
Parameters
$elementValue : array<string|int, mixed>
Return values
bool

Whether file has been uploaded properly and is now in UTF-8.

validContact()

public static validContact(mixed $value[, mixed $actualElementValue = NULL ]) : bool
Parameters
$value : mixed
$actualElementValue : mixed = NULL
Return values
bool

validDateRange()

Check if the values in the date range are in correct chronological order.

public static validDateRange(array<string|int, mixed> $fields, mixed $fieldName, mixed &$errors, mixed $title) : mixed
Parameters
$fields : array<string|int, mixed>

Fields of the form.

$fieldName : mixed

Name of date range field.

$errors : mixed

The error array.

$title : mixed

Title of the date range to be displayed in the error message.

variable()

public static variable(string|null $str) : bool
Parameters
$str : string|null
Return values
bool

wikiURL()

public static wikiURL(string $string) : bool
Parameters
$string : string
Return values
bool

arrayValue()

Validate array recursively checking keys and values.

protected static arrayValue(array<string|int, mixed> $array) : bool
Parameters
$array : array<string|int, mixed>
Return values
bool

idnToAsci()

Convert domain string to ascii.

private static idnToAsci(string $string) : string|false

See https://lab.civicrm.org/dev/core/-/issues/2769 and also discussion over in guzzle land https://github.com/guzzle/guzzle/pull/2454

Parameters
$string : string
Return values
string|false

        
On this page

Search results