field_validation_number_of_selections_validator.inc in Field Validation 7.2
File
plugins/validator/field_validation_number_of_selections_validator.inc
View source
<?php
$plugin = array(
'label' => t('Number of selections'),
'description' => t('Verifies the number of options a user could select, with the option to specify minimum and maximum number of selections.'),
'handler' => array(
'class' => 'field_validation_number_of_selections_validator',
),
);
class field_validation_number_of_selections_validator extends field_validation_validator {
public function validate() {
$settings = $this->rule->settings;
$total_items = count($this->items);
$flag = TRUE;
if (isset($settings['min']) && $settings['min'] != '' && $total_items < $settings['min']) {
$flag = FALSE;
}
if (isset($settings['max']) && $settings['max'] != '' && $total_items > $settings['max']) {
$flag = FALSE;
}
if (!$flag) {
$token = array(
'[min]' => isset($settings['min']) ? $settings['min'] : '',
'[max]' => isset($settings['max']) ? $settings['max'] : '',
'[count]' => $total_items,
);
$this
->set_error($token);
}
$break = TRUE;
return $break;
}
function settings_form(&$form, &$form_state) {
$default_settings = $this
->get_default_settings($form, $form_state);
$form['settings']['min'] = array(
'#title' => t('Minimum number of selections'),
'#description' => t("Optionally specify the minimum number of options a user should select."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['min']) ? $default_settings['min'] : '',
);
$form['settings']['max'] = array(
'#title' => t('Maximum number of selections'),
'#description' => t("Optionally specify the maximum number of options a user could select."),
'#type' => 'textfield',
'#default_value' => isset($default_settings['max']) ? $default_settings['max'] : '',
);
parent::settings_form($form, $form_state);
}
public function get_error_element() {
$error_element = $this->rule->field_name . '][' . $this->langcode;
return $error_element;
}
public function token_help() {
$token_help = parent::token_help();
$token_help += array(
'[min]' => t('Minimum number of selections'),
'[max]' => t('Maximum number of selections'),
'[count]' => t('The real number of selections'),
);
return $token_help;
}
}