/** * Validate a value according to the validation rules. * @param String $fieldValue The value to validate. */ protected function validateValueUsingRules($fieldValue) { $validationType = self::getArrayValue($this->validationRules, 'type'); $this->errorMessage = self::getArrayValue($this->validationRules, 'error'); $isValid = true; // Handle maximum and minimum length fields. switch ($validationType) { // Standard strings, where length is important. case 'string': case 'email': case 'url': // 1st stage, are there enough characters? $minlen = $this->validationRules['minlen'] + 0; if ($minlen > 0) { $isValid = strlen($fieldValue) >= $minlen; } // 2nd stage, are there too many characters? $maxlen = $this->validationRules['maxlen'] + 0; if ($maxlen > 0 && $isValid) { $isValid = strlen($fieldValue) <= $maxlen; } // Length validation failed. if (!$isValid) { return false; } break; // Don't bother doing length for these fields, as they have a different // measure of what's valid based on their structure. // Don't bother doing length for these fields, as they have a different // measure of what's valid based on their structure. case 'telephone': case 'postcode': case 'number': case 'decimal': case 'count': break; // Unknown validation type. // Unknown validation type. default: error_log('validateValueUsingRules(): Unknown validation type.'); return false; break; } // More complex validation happens now. switch ($validationType) { // ### Lists - counting items case 'count': if (isset($this->validationRules['max'])) { $maxcount = $this->validationRules['max'] + 0; // Unlimited if ($maxcount == -1) { $isValid = true; } else { if ($maxcount == 0) { HL_debug_showArray($this->value); $isValid = empty($this->value) || count($this->value) == 0; } else { if (!empty($this->value) && is_array($this->value)) { $isValid = count($this->value) <= $maxcount; } } } } break; // ### Generic number /* 'validate' => array( 'type' => 'number', 'max' => 50, 'min' => 1, 'error' => 'Please choose a number between 1 and 50.' ) */ // ### Generic number /* 'validate' => array( 'type' => 'number', 'max' => 50, 'min' => 1, 'error' => 'Please choose a number between 1 and 50.' ) */ case 'number': // 1st stage, is it a number $isValid = is_numeric($fieldValue); // 2nd stage, do we have any ranges? if ($isValid) { $fieldValue += 0; // Do we have a minimum value? if (isset($this->validationRules['min'])) { $isValid = $fieldValue >= $this->validationRules['min']; } // Do we have a maximum value? if ($isValid && isset($this->validationRules['max'])) { $isValid = $fieldValue <= $this->validationRules['max']; } } break; // ### Special Numbers // Decimal /* 'validate' => array( 'type' => 'decimal', 'max' => 9999.99, 'min' => 0.01, 'error' => 'Please choose a starting price between 0.01 and 9999.99.' ) */ // ### Special Numbers // Decimal /* 'validate' => array( 'type' => 'decimal', 'max' => 9999.99, 'min' => 0.01, 'error' => 'Please choose a starting price between 0.01 and 9999.99.' ) */ case 'decimal': // 1st stage, is it a decimal number? $isValid = preg_match('/^[0-9]+(\\.[0-9]{1,2})?$/', $fieldValue); // 2nd stage, do we have any ranges? if ($isValid) { $fieldValue += 0; // Do we have a minimum value? if (isset($this->validationRules['min'])) { $isValid = $fieldValue >= $this->validationRules['min']; } // Do we have a maximum value? if ($isValid && isset($this->validationRules['max'])) { $isValid = $fieldValue <= $this->validationRules['max']; } } break; // ### Generic string /* 'validate' => array( 'type' => 'string', 'maxlen' => 100, // (optional) The maximum length of the string 'minlen' => 1, // (optional) The minimum length of the string 'regexp' => '/^[A-Za-z0-9\'\-\ ]+$/', // (optional) A normal regular-expression of what's permitted in the string. 'error' => 'Explain what's valid.' // (optional) The error message if the string doesn't validate. ) */ // ### Generic string /* 'validate' => array( 'type' => 'string', 'maxlen' => 100, // (optional) The maximum length of the string 'minlen' => 1, // (optional) The minimum length of the string 'regexp' => '/^[A-Za-z0-9\'\-\ ]+$/', // (optional) A normal regular-expression of what's permitted in the string. 'error' => 'Explain what's valid.' // (optional) The error message if the string doesn't validate. ) */ case 'string': // Validate against a regular expression $regexp = FormElement::getArrayValue($this->validationRules, 'regexp'); if ($regexp) { $isValid = preg_match($regexp, $fieldValue, $matches); } break; // ### Special strings // Valid Telephone Number (type = telephone) // ### Special strings // Valid Telephone Number (type = telephone) case 'telephone': // Examples of valid numbers are: // 01234 123345 // +44 12345 123455 $nospaces = str_replace(' ', '', $fieldValue); $isValid = preg_match('/^\\+?([0-9]){9,14}$/', $nospaces); break; // Valid URLs (type = url) // Valid URLs (type = url) case 'url': //$isValid = preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $fieldValue); $isValid = preg_match("/(https?|ftp):\\/\\/(www\\.)?[-a-z0-9+&@#\\/%?=~_|!:,.;]*[-a-z0-9+&@#\\/%=~_|]/i", $fieldValue); break; // Valid Email Addresses (type = email) // Valid Email Addresses (type = email) case 'email': $isValid = preg_match('/^[A-Za-z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/', $fieldValue); break; // Valid UK Postcode (type = postcode) /** Validates against these: A9 9AA M1 1AA B, E, G, L, M, N, S, W postcode areas A99 9AA M60 1NW AA9 9AA CR2 6XH All postcode areas except B, E, G, L, M, N, S, W, WC AA99 9AA DN55 1PT A9A 9AA W1A 1HQ E1W, N1C, N1P, W1 postcode districts (high density areas where codes ran out) AA9A 9AA EC1A 1BB WC postcode area; EC1ÐEC4, NW1W, SE1P, SW1 postcode districts (high density areas where codes ran out */ // Valid UK Postcode (type = postcode) /** Validates against these: A9 9AA M1 1AA B, E, G, L, M, N, S, W postcode areas A99 9AA M60 1NW AA9 9AA CR2 6XH All postcode areas except B, E, G, L, M, N, S, W, WC AA99 9AA DN55 1PT A9A 9AA W1A 1HQ E1W, N1C, N1P, W1 postcode districts (high density areas where codes ran out) AA9A 9AA EC1A 1BB WC postcode area; EC1ÐEC4, NW1W, SE1P, SW1 postcode districts (high density areas where codes ran out */ case 'postcode': $isValid = preg_match('/^([A-Z]([0-9]{1,2}|[A-Z][0-9]{1,2}|[A-Z]?[0-9][A-Z]))\\ ([0-9][A-Z]{2})$/i', $fieldValue); break; } return $isValid; }