Changeset View
Changeset View
Standalone View
Standalone View
core/kernel/utility/validator.php
Show First 20 Lines • Show All 196 Lines • ▼ Show 20 Line(s) | |||||
trigger_error('Invalid field type <strong>' . $params['type'] . '</strong> (in ValidateType method), please use <strong>float</strong> instead', E_USER_NOTICE); | trigger_error('Invalid field type <strong>' . $params['type'] . '</strong> (in ValidateType method), please use <strong>float</strong> instead', E_USER_NOTICE); | ||||
$params['type'] = 'float'; | $params['type'] = 'float'; | ||||
} | } | ||||
$res = is_numeric($val); | $res = is_numeric($val); | ||||
if ( $params['type'] == 'string' || $res ) { | if ( $params['type'] == 'string' || $res ) { | ||||
$f = 'is_' . $params['type']; | $f = 'is_' . $params['type']; | ||||
if ( is_scalar($val) ) { | |||||
settype($val, $params['type']); | settype($val, $params['type']); | ||||
} | |||||
$res = $f($val) && ($val == $this->dataSource->GetDBField($field)); | $res = $f($val) && ($val == $this->dataSource->GetDBField($field)); | ||||
} | } | ||||
if ( !$res ) { | if ( !$res ) { | ||||
$this->SetError($field, 'bad_type', null, array('type' => $params['type'])); | $this->SetError($field, 'bad_type', null, array('type' => $params['type'])); | ||||
return false; | return false; | ||||
Show All 9 Lines | |||||
* @param Array $params field options from config | * @param Array $params field options from config | ||||
* @return bool | * @return bool | ||||
* @access private | * @access private | ||||
*/ | */ | ||||
protected function ValidateRange($field, $params) | protected function ValidateRange($field, $params) | ||||
{ | { | ||||
$res = true; | $res = true; | ||||
$val = $this->dataSource->GetDBField($field); | $val = $this->dataSource->GetDBField($field); | ||||
$value_length = is_scalar($val) ? mb_strlen($val) : 0; | |||||
alex: # if this isn't a scalar value or value is empty, then exit immediately
# remove any empty… | |||||
if ( !$value_length ) { | |||||
return true; | |||||
} | |||||
if ( isset($params['type']) && preg_match("#int|integer|double|float|real#", $params['type']) && strlen($val) > 0 ) { | if ( isset($params['type']) && preg_match('#int|integer|double|float|real#', $params['type']) ) { | ||||
Not Done ReplyIn fact change // validate number into // Validate number. and this should fix all CS errors. alex: In fact change `// validate number` into `// Validate number.` and this should fix all CS… | |||||
// validate number | // Validate number. | ||||
if ( isset($params['max_value_inc'])) { | if ( isset($params['max_value_inc']) ) { | ||||
$res = $res && $val <= $params['max_value_inc']; | $res = $res && $val <= $params['max_value_inc']; | ||||
$max_val = $params['max_value_inc'].' (inclusive)'; | $max_val = $params['max_value_inc'].' (inclusive)'; | ||||
} | } | ||||
if ( isset($params['min_value_inc'])) { | if ( isset($params['min_value_inc'])) { | ||||
$res = $res && $val >= $params['min_value_inc']; | $res = $res && $val >= $params['min_value_inc']; | ||||
$min_val = $params['min_value_inc'].' (inclusive)'; | $min_val = $params['min_value_inc'].' (inclusive)'; | ||||
} | } | ||||
if ( isset($params['max_value_exc'])) { | if ( isset($params['max_value_exc'])) { | ||||
$res = $res && $val < $params['max_value_exc']; | $res = $res && $val < $params['max_value_exc']; | ||||
$max_val = $params['max_value_exc'].' (exclusive)'; | $max_val = $params['max_value_exc'].' (exclusive)'; | ||||
} | } | ||||
if ( isset($params['min_value_exc'])) { | if ( isset($params['min_value_exc'])) { | ||||
$res = $res && $val > $params['min_value_exc']; | $res = $res && $val > $params['min_value_exc']; | ||||
$min_val = $params['min_value_exc'].' (exclusive)'; | $min_val = $params['min_value_exc'].' (exclusive)'; | ||||
} | } | ||||
} | |||||
if ( !$res ) { | if ( !$res ) { | ||||
if ( !isset($min_val) ) $min_val = '-∞'; | if ( !isset($min_val) ) { | ||||
if ( !isset($max_val) ) $max_val = '∞'; | $min_val = '-∞'; | ||||
} | |||||
if ( !isset($max_val) ) { | |||||
$max_val = '∞'; | |||||
} | |||||
$this->SetError($field, 'value_out_of_range', null, array( | $this->SetError($field, 'value_out_of_range', null, array( | ||||
'min_value' => $min_val, | 'min_value' => $min_val, | ||||
'max_value' => $max_val | 'max_value' => $max_val, | ||||
)); | )); | ||||
} | |||||
return false; | return $res; | ||||
} | } | ||||
Not Done Reply
prevents string-validation rules to be apply to a valid number alex: # move this inside a previous if statement (when we've detected, that it's a number)
# instead… | |||||
if ( strlen($val) > 0 ) { | |||||
// Validate string. | // Validate string. | ||||
if ( isset($params['max_len']) ) { | if ( isset($params['max_len']) ) { | ||||
$res = $res && mb_strlen($val) <= $params['max_len']; | $res = $res && $value_length <= $params['max_len']; | ||||
} | } | ||||
if ( isset($params['min_len']) ) { | if ( isset($params['min_len']) ) { | ||||
$res = $res && mb_strlen($val) >= $params['min_len']; | $res = $res && $value_length >= $params['min_len']; | ||||
} | |||||
} | } | ||||
if ( !$res ) { | if ( !$res ) { | ||||
$error_params = array( | $error_params = array( | ||||
'min_length' => (int)getArrayValue($params, 'min_len'), | 'min_length' => (int)getArrayValue($params, 'min_len'), | ||||
'max_length' => (int)getArrayValue($params, 'max_len'), | 'max_length' => (int)getArrayValue($params, 'max_len'), | ||||
'value' => mb_strlen($val) | 'value' => $value_length, | ||||
); | ); | ||||
$this->SetError($field, 'length_out_of_range', null, $error_params); | $this->SetError($field, 'length_out_of_range', null, $error_params); | ||||
return false; | |||||
} | } | ||||
Not Done Replyremove see above alex: remove
---
see above | |||||
return true; | return $res; | ||||
Not Done Replyreplace with return $res; see above alex: replace with `return $res;`
---
see above | |||||
} | } | ||||
/** | /** | ||||
* Validates that current record has unique field combination among other table records | * Validates that current record has unique field combination among other table records | ||||
* | * | ||||
* @param string $field field name | * @param string $field field name | ||||
* @param Array $params field options from config | * @param Array $params field options from config | ||||
* @return bool | * @return bool | ||||
▲ Show 20 Lines • Show All 231 Lines • Show Last 20 Lines |
refactoring