function fapi_validation_rules_execute in Form API Validation 6
Same name and namespace in other branches
- 7.2 fapi_validation.module \fapi_validation_rules_execute()
- 7 fapi_validation.module \fapi_validation_rules_execute()
1 call to fapi_validation_rules_execute()
File
- ./
fapi_validation.module, line 76
Code
function fapi_validation_rules_execute(&$element) {
$data = _fapi_validation_data('rules');
// If element is empty and not required, by pass rule validation.
if (!$element['#required'] && empty($element['#value'])) {
return;
}
foreach ($element['#rules'] as $rule) {
$params = array(
$element['#value'],
);
$error_message = NULL;
// If $rule is an array, use error message if is setted.
if (is_array($rule)) {
if (isset($rule['error'])) {
$error_message = $rule['error'];
}
if (!isset($rule['rule'])) {
drupal_set_message(t('Rule array with wrong structure on %field.', array(
'%field' => $element['#name'],
)), 'error');
continue;
}
$rule = $rule['rule'];
}
preg_match('/^(.*?)(\\[(.*)\\])?$/', $rule, $rs);
$rule = $rs[1];
if (!isset($data[$rule])) {
drupal_set_message(t('Rule %rule not found!', array(
'%rule' => $rule,
)), 'error');
continue;
}
// Parsing parameters.
if (isset($rs[3])) {
if ($rule == 'regexp') {
$params[] = array(
$rs[3],
);
}
else {
$params[] = preg_split('/ *, */', $rs[3]);
}
}
$ret = call_user_func_array($data[$rule]['callback'], $params);
if (!$ret) {
$error = is_null($error_message) ? $data[$rule]['error_msg'] : $error_message;
form_set_error(implode("][", $element['#parents']), t($error, array(
'%field' => $element['#title'],
)));
}
}
}