Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

validate_field()

Description

Validate the given field.

If the field validates, this function will store it in the provided configuration
module and key.

Parameters

ColumnTypeDefaultDescription
$fieldname string Name of field (provided to the render functions)
$modulename string Module name to store the field in.
$modulekey string Module key
$type string Validation patthern: (bool,safe,float,int,email,regex)
$required string true Optional required flag. Defaults to true.
$pattern string '' If $type is 'regex' the regex pattern to use.

Return

bool Returns true if the field was stored in the config database.

Location

include/config_functions.php lines 23 to 79

Definition

 
function validate_field($fieldname$modulename$modulekey$type$required=true$pattern=''){
    global 
$errorfields$lang;
    
$value getvalescaped($fieldname'');
    if (
$value=='' && $required==true){
        
$errorfields[$fieldname]=$lang['cfg-err-fieldrequired'];
        return 
false;
    }
    elseif (
$value=='' && $required==false){
        
set_module_config_key($modulename$modulekey$value); 
    }
    else {
        switch (
$type){
            case 
'safe':
                if (!
preg_match('/^.+$/'$value)){
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldsafe'];
                    return 
false;
                }
                break;
            case 
'float':
                if (!
preg_match('/^[\d]+(\.[\d]*)?$/'$value)){
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldnumeric'];
                    return 
false;
                }
                break;
            case 
'int':
                if (!
preg_match('/^[\d]+$/'$value)){
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldnumeric'];
                    return 
false;
                }
                break;
            case 
'email':
                if (!
preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i'$value)){
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldemail'];
                    return 
false;
                }
                break;
            case 
'regex':
                if (!
preg_match($pattern$value)){
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldsafe'];
                    return 
false;
                }
                break;
            case 
'bool':
                if (
strtolower($value)=='true')
                    
$value=true;
                elseif (
strtolower($value)=='false')
                    
$value=false;
                else {
                    
$errorfields[$fieldname] = $lang['cfg-err-fieldsafe'];
                    return 
false;
                }
                break;
        }
       
set_module_config_key($modulename$modulekey$value);
       return 
true;             
    }
}

This article was last updated 8th July 2020 11:35 Europe/London time based on the source file dated 18th May 2020 17:14 Europe/London time.