class field_validation_words_validator in Field Validation 7.2
Hierarchy
Expanded class hierarchy of field_validation_words_validator
2 string references to 'field_validation_words_validator'
- field_validation_default_field_validation_rule in ./
field_validation.default_field_validation_rules.inc - Implementation of hook_default_field_validation_rule().
- field_validation_words_validator.inc in plugins/
validator/ field_validation_words_validator.inc
File
- plugins/
validator/ field_validation_words_validator.inc, line 16
View source
class field_validation_words_validator extends field_validation_validator {
/**
* Validate field.
*/
public function validate() {
$settings = $this->rule->settings;
if ($this->value != '') {
$flag = TRUE;
$count = count(explode(' ', trim(preg_replace('/\\s+/', ' ', str_replace(' ', ' ', strip_tags(str_replace('<', ' <', $this->value)))))));
if (isset($settings['min']) && $settings['min'] != '') {
$min = token_replace($settings['min'], array(
$this
->get_token_type() => $this->entity,
));
if ($count < $min) {
$flag = FALSE;
}
}
if (isset($settings['max']) && $settings['max'] != '') {
$max = token_replace($settings['max'], array(
$this
->get_token_type() => $this->entity,
));
if ($count > $max) {
$flag = FALSE;
}
}
if (!$flag) {
$token = array(
'[min]' => isset($min) ? $min : '',
'[max]' => isset($max) ? $max : '',
'[count]' => $count,
);
$this
->set_error($token);
}
}
}
/**
* Provide settings option.
*/
function settings_form(&$form, &$form_state) {
$default_settings = $this
->get_default_settings($form, $form_state);
// Print debug($default_settings);
$form['settings']['min'] = array(
'#title' => t('Minimum number of words'),
'#description' => t("Optionally specify the minimum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['min']) ? $default_settings['min'] : '',
);
$form['settings']['max'] = array(
'#title' => t('Maximum number of words'),
'#description' => t("Optionally specify the maximum number of words that have to be entered to pass validation. Words are defined as strings of letters separated by spaces."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['max']) ? $default_settings['max'] : '',
);
parent::settings_form($form, $form_state);
}
/**
* Provide token help info for error message.
*/
public function token_help() {
$token_help = parent::token_help();
$token_help += array(
'[min]' => t('Minimum number of words'),
'[max]' => t('Maximum number of words'),
'[count]' => t('The real number of words'),
);
return $token_help;
}
}