function webform_validate_structured_text in Webform Structured Text 7
Same name and namespace in other branches
- 6 structured_text.inc \webform_validate_structured_text()
Validation function for structured text. Ensure that user input conforms to the mask, and that no portions are left empty.
Parameters
array $element The the structured text element, including values.:
array $form_state The state of the form.:
1 string reference to 'webform_validate_structured_text'
- _webform_render_structured_text in ./
structured_text.inc - Implements _webform_render_component().
File
- ./
structured_text.inc, line 513
Code
function webform_validate_structured_text($element, $form_state) {
// Gather the actual input.
$chunks = array();
$had_pattern_errors = FALSE;
$unique_test_array = array();
foreach ($element['#mask'] as $part => $details) {
if ($details['type'] != 'markup') {
$chunks[$part] = $unique_test_array["part-{$part}"] = $element["part-{$part}"]['#value'];
}
}
// Determine if any input fields are missing. If they're all empty, then they're not missing.
$numb_chunks = count($chunks);
$numb_not_empty = count(array_filter($chunks, 'trim'));
$numb_empty = $numb_chunks - $numb_not_empty;
$missing_chunks = $numb_chunks != $numb_not_empty && $numb_chunks != $numb_empty;
// Now evaluate each text field.
$mask_regex = array();
if (!empty($element['#mask_regex'])) {
$mask_regex_lines = array_filter(explode("\n", $element['#mask_regex']));
foreach ($mask_regex_lines as $line) {
list($chunk, $message, $regex) = explode('|', $line, 3);
$mask_regex[$chunk] = array(
'message' => $message,
'regex' => $regex,
);
}
}
$component = 0;
// Used to point the user to the correct chunk of the whole input field in error messages.
foreach ($chunks as $part => $value) {
$component++;
$error_1 = $error_n = '';
switch ($element['#mask'][$part]['type']) {
case '9':
if (($element['#required'] || $missing_chunks) && $value == '' || $value != '' && (preg_match('/[^0-9]/', $value) || drupal_strlen($value) < $element['#mask'][$part]['length'])) {
$error_1 = t('digit');
$error_n = t('digits');
}
break;
case 'a':
if (($element['#required'] || $missing_chunks) && $value == '' || $value != '' && (preg_match('/[^a-z]/i', $value) || drupal_strlen($value) < $element['#mask'][$part]['length'])) {
$error_1 = t('alpha character (a-z)');
$error_n = t('alpha characters (a-z)');
}
break;
case 'x':
if (($element['#required'] || $missing_chunks) && $value == '' || $value != '' && drupal_strlen($value) < $element['#mask'][$part]['length']) {
$error_1 = t('character');
$error_n = t('characters');
}
break;
case 'r':
// r types are completely evaluated by the regex expression for validity
break;
}
if ($error_1) {
$had_pattern_errors = TRUE;
$component_field = implode('][', array_merge($element['#parents'], array(
'part-' . $part,
)));
form_set_error($component_field, format_plural($element['#mask'][$part]['length'], '%element field !part part must be 1 !singular.', '%element field !part part must be @count !plural.', array(
'%element' => $element['#title'],
'!part' => t($component . _webform_structured_text_ordinal_suffix($component)),
'!singular' => $error_1,
'!plural' => $error_n,
)));
}
elseif (($element['#required'] || !$missing_chunks && $numb_not_empty != 0 || $element['#mask'][$part]['type'] == 'r') && !empty($mask_regex[$component]['regex'])) {
// no errors so far, and we have a regex, so test it
if (!preg_match(trim($mask_regex[$component]['regex']), $value)) {
$had_pattern_errors = TRUE;
$component_field = implode('][', array_merge($element['#parents'], array(
'part-' . $part,
)));
form_set_error($component_field, strip_tags(_webform_structured_text_t($element['#webform_component']['nid'] . ':' . $element['#webform_component']['cid'] . ":regex:{$component}:message", $mask_regex[$component]['message'])));
}
}
}
if (!$had_pattern_errors && $element['#unique']) {
$element['#value'] = _webform_structured_text_create_store_value($element['#mask'], $unique_test_array);
if ($element['#value'] !== '') {
$nid = $form_state['values']['details']['nid'];
$sid = $form_state['values']['details']['sid'];
$query = db_select('webform_submitted_data')
->fields('webform_submitted_data', array(
'sid',
))
->condition('nid', $nid)
->condition('cid', $element['#webform_component']['cid'])
->condition('data', $element['#value'])
->range(0, 1);
// More efficient than using countQuery() for data checks.
if ($sid) {
$query
->condition('sid', $sid, '<>');
}
$count = $query
->execute()
->fetchField();
if ($count) {
form_error($element, t('The value %value has already been submitted once for the %title field.
You may have already submitted this form, or you need to use a different value.', array(
'%value' => webform_structured_text_format_value($element['#mask'], $element['#value'], $element['#webform_component']),
'%title' => $element['#title'],
)));
}
}
}
}