Commit a21aa141 authored by Jelle's avatar Jelle
Browse files

coder cleanup

parent 99d2a3e9
<?php
// $Id:
// $Id$
/**
* @file
* Add client side validation to a webform.
......@@ -44,7 +44,7 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
case 'max_length':
foreach ($webform_validation_rule['components'] as $component) {
$message = t('!name field has a maximum length of !maxl characters.', array('!name' => $component['name'], '!maxl' => $webform_validation_rule['data']));
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '',$webform_validation_rule['data'], $js_rules, $message);
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules, $message);
}
break;
......@@ -86,17 +86,17 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
break;
case 'select_min':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength ('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules);
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], '', $js_rules);
}
break;
case 'select_max':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength ('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules);
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], '', $webform_validation_rule['data'], $js_rules);
}
break;
case 'select_exact':
foreach ($webform_validation_rule['components'] as $component) {
_clientside_validation_set_minmaxlength ('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules);
_clientside_validation_set_minmaxlength('submitted[' . $component['form_key'] . ']', $component['name'], $webform_validation_rule['data'], $webform_validation_rule['data'], $js_rules);
}
break;
case 'validEAN':
......@@ -109,7 +109,7 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
}
}
if (!empty($js_rules)) {
$settings['clientsideValidation']['general'] = array (
$settings['clientsideValidation']['general'] = array(
"errorClass" => "error",
"wrapper" => "li",
);
......@@ -117,7 +117,7 @@ function clientside_validation_webform_after_build(&$form, &$form_state) {
"errorContainer" => "#formerrors-" . $form['#id'],
"errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
);
foreach($js_rules as $key => $rule) {
foreach ($js_rules as $key => $rule) {
$settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
}
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
......@@ -139,7 +139,7 @@ function clientside_validation_webform_after_build_recurse($form_id, &$form, &$f
$message = t('Minute in !name field is required.', array('!name' => $element['#title']));
_clientside_validation_set_required($element['minute']['#name'], $element['#title'], TRUE, $js_rules, $message);
}
else if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'date') {
elseif (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'date') {
$message = t('Month in !name field is required.', array('!name' => $element['#title']));
_clientside_validation_set_required($element['#name'] . '[month]', $element['#title'], TRUE, $js_rules, $message);
$message = t('Day in !name field is required.', array('!name' => $element['#title']));
......@@ -151,17 +151,17 @@ function clientside_validation_webform_after_build_recurse($form_id, &$form, &$f
_clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
}
}
else if ($element['#type'] == 'checkboxes') {
elseif ($element['#type'] == 'checkboxes') {
$count = 0;
foreach ($element['#options'] as $key => $value) {
_clientside_validation_set_checkboxgroup_minmax($element[$key]['#name'], $element['#title'], '#webform-component-' . end($element['#parents']), $count, $js_rules);
$count++;
}
}
else if ($element['#type'] == 'select' && $element['#multiple']) {
elseif ($element['#type'] == 'select' && $element['#multiple']) {
_clientside_validation_set_required($element['#name'] . '[]', $element['#title'], TRUE, $js_rules);
}
else if (isset($element['#type'])) {
elseif (isset($element['#type'])) {
_clientside_validation_set_required($element['#name'], $element['#title'], TRUE, $js_rules);
}
}
......@@ -209,7 +209,7 @@ function clientside_validation_form_after_build(&$form, &$form_state) {
static $js_rules = array();
clientside_validation_form_after_build_recurse($form['#id'], $form, $form_state, $js_rules);
if (!empty($js_rules)) {
$settings['clientsideValidation']['general'] = array (
$settings['clientsideValidation']['general'] = array(
"errorClass" => "error",
"wrapper" => "li",
);
......@@ -217,7 +217,7 @@ function clientside_validation_form_after_build(&$form, &$form_state) {
"errorContainer" => "#formerrors-" . $form['#id'],
"errorLabelContainer" => "#formerrors-" . $form['#id'] . " ul",
);
foreach($js_rules as $key => $rule) {
foreach ($js_rules as $key => $rule) {
$settings['clientsideValidation']['forms'][$form['#id']]['rules'][$key] = $rule;
}
drupal_add_js(drupal_get_path('module', 'clientside_validation') . '/jquery-validate/jquery.validate.js');
......@@ -236,14 +236,14 @@ function clientside_validation_form_after_build_recurse($form_id, &$form, &$form
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
);
if (isset($element['#type']) && in_array($element['#type'], $types)) {
clientside_validation_regular ($form_id, $element, $js_rules);
clientside_validation_regular($form_id, $element, $js_rules);
}
clientside_validation_form_after_build_recurse($form_id, $element, $form_state, $js_rules);
}
}
}
function clientside_validation_regular ($form_id, $element, &$js_rules) {
function clientside_validation_regular($form_id, $element, &$js_rules) {
static $multiples = array();
if (isset($element['#name']) && !isset($js_rules[$element['#name']])) {
$el_name = $element['#name'];
......@@ -263,46 +263,44 @@ function clientside_validation_regular ($form_id, $element, &$js_rules) {
if ($is_multiple) {
// Only first field is required
if (!isset($multiples[$form_id][$el_name])) {
_clientside_validation_set_required ($el_name, $el_title, $element['#required'], $js_rules);
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
$multiples[$form_id][$el_name] = 1;
}
}
else {
_clientside_validation_set_required ($el_name, $el_title, $element['#required'], $js_rules);
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
}
if (isset($element['max_length']) && $element['max_length'] > 0) {
_clientside_validation_set_minmaxlength ($el_name, $el_title, '', $element['max_length'], $js_rules);
_clientside_validation_set_minmaxlength($el_name, $el_title, '', $element['max_length'], $js_rules);
}
break;
case 'select':
if ($is_multiple) {
$el_name .= '[]';
if(!isset($element['#minlength'])){
if (!isset($element['#minlength'])) {
$element['#minlength'] = 0;
}
_clientside_validation_set_minmaxlength ($el_name, $el_title, $element['#minlength'], $is_multiple, $js_rules);
_clientside_validation_set_minmaxlength($el_name, $el_title, $element['#minlength'], $is_multiple, $js_rules);
}
_clientside_validation_set_required ($el_name, $el_title, $element['#required'], $js_rules);
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
break;
case 'radio':
case 'radios':
_clientside_validation_set_required ($el_name, $el_title, $element['#required'], $js_rules);
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
break;
case 'checkbox':
case 'checkboxes':
$is_multiple = (isset($element['#options']) && count($element['#options']>1));
if ($is_multiple) {
// We don't have a parent element to connect to, so no go, outer div has only a class
// The checkboxes element has the unique name, but this isn't added to the outer div
$count = 0;
foreach ($element['#options'] as $key => $value) {
$id = '#edit-' . str_replace('_', '-', $element['#parents'][0]);
_clientside_validation_set_checkboxgroup_minmax($element[$key]['#name'], $element['#title'], $id, $count, $js_rules);
_clientside_validation_set_checkboxgroup_minmax($element[$key]['#name'], $el_title, $id, $count, $js_rules);
$count++;
}
}
else {
_clientside_validation_set_required ($el_name, $el_title, $element['#required'], $js_rules);
_clientside_validation_set_required($el_name, $el_title, $element['#required'], $js_rules);
}
break;
}
......@@ -312,7 +310,7 @@ function clientside_validation_regular ($form_id, $element, &$js_rules) {
/**
* Set validation rule for required fields.
*/
function _clientside_validation_set_required ($name, $title, $required, &$js_rules, $message = '') {
function _clientside_validation_set_required($name, $title, $required, &$js_rules, $message = '') {
if ($required) {
$js_rules[$name]['required'] = TRUE;
$js_rules[$name]['messages']['required'] = (empty($message)) ? t('!name field is required.', array('!name' => $title)) : $message;
......@@ -322,7 +320,7 @@ function _clientside_validation_set_required ($name, $title, $required, &$js_rul
/**
* Set validation rule for number fields.
*/
function _clientside_validation_set_number ($name, $title, $decimalpoint, &$js_rules) {
function _clientside_validation_set_number($name, $title, $decimalpoint, &$js_rules) {
$js_rules[$name]['digits_negative'] = TRUE;
$js_rules[$name]['messages']['digits_negative'] = t('!name field accepts only numbers.', array('!name' => $title));
}
......@@ -330,7 +328,7 @@ function _clientside_validation_set_number ($name, $title, $decimalpoint, &$js_r
/**
* Set validation rule for decimal fields.
*/
function _clientside_validation_set_number_decimal ($name, $title, $decimalpoint, &$js_rules) {
function _clientside_validation_set_number_decimal($name, $title, $decimalpoint, &$js_rules) {
if ($decimalpoint == '.') {
$js_rules[$name]['number'] = TRUE;
$js_rules[$name]['messages']['number'] = t('!name field accepts only numbers (use a \'.\' as decimal point).', array('!name' => $title));
......@@ -344,16 +342,16 @@ function _clientside_validation_set_number_decimal ($name, $title, $decimalpoint
/**
* Set validation rule for fields with a minimum and/or a maximum value.
*/
function _clientside_validation_set_minmax ($name, $title, $min, $max, &$js_rules, $message = '') {
function _clientside_validation_set_minmax($name, $title, $min, $max, &$js_rules, $message = '') {
if (isset($min) && $min != '' && isset($max) && $max != '') {
$js_rules[$name]['range'] = array($min, $max);
$js_rules[$name]['messages']['range'] = (empty($message)) ? t('!name field has to be between !min and !max.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
}
else if (isset($min) && $min != '') {
elseif (isset($min) && $min != '') {
$js_rules[$name]['min'] = $min;
$js_rules[$name]['messages']['min'] = (empty($message)) ? t('!name field has to be greater than !min.', array('!name' => $title, '!min' => $min)) : $message;
}
else if (isset($max) && $max != '') {
elseif (isset($max) && $max != '') {
$js_rules[$name]['max'] = $max;
$js_rules[$name]['messages']['max'] = (empty($message)) ? t('!name field has to be smaller than !max.', array('!name' => $title, '!max' => $max)) : $message;
}
......@@ -362,16 +360,16 @@ function _clientside_validation_set_minmax ($name, $title, $min, $max, &$js_rule
/**
* Set validation rule for fields with a minimum and/or maximum length.
*/
function _clientside_validation_set_minmaxlength ($name, $title, $min, $max, &$js_rules, $message = '') {
function _clientside_validation_set_minmaxlength($name, $title, $min, $max, &$js_rules, $message = '') {
if (isset($min) && $min != '' && isset($max) && $max != '') {
$js_rules[$name]['rangelength'] = array($min, $max);
$js_rules[$name]['messages']['rangelength'] = (empty($message)) ? t('!name field has to have between !min and !max values.', array('!name' => $title, '!min' => $min, '!max' => $max)) : $message;
}
else if (isset($min) && $min != '') {
elseif (isset($min) && $min != '') {
$js_rules[$name]['minlength'] = $min;
$js_rules[$name]['messages']['minlength'] = (empty($message)) ? t('!name field has to have minimal !min values.', array('!name' => $title, '!min' => $min)) : $message;
}
else if (isset($max) && $max != '') {
elseif (isset($max) && $max != '') {
$js_rules[$name]['maxlength'] = $max;
$js_rules[$name]['messages']['maxlength'] = (empty($message)) ? t('!name field has to have maximum !max values.', array('!name' => $title, '!max' => $max)) : $message;
}
......@@ -380,7 +378,7 @@ function _clientside_validation_set_minmaxlength ($name, $title, $min, $max, &$j
/**
* Set validation rule for required fields that must equal a value from an other field.
*/
function _clientside_validation_set_equal($name, $title, $value, &$js_rules){
function _clientside_validation_set_equal($name, $title, $value, &$js_rules) {
$js_rules[$name]['equalTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
$js_rules[$name]['messages']['equalTo'] = t('!name field has to be equal to !firstone.', array('!name' => $title, '!firstone' => $value['name']));
}
......@@ -388,7 +386,7 @@ function _clientside_validation_set_equal($name, $title, $value, &$js_rules){
/**
* Set validation rule for fields that can not be equal to a value from an other field.
*/
function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules, $message = ''){
function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules, $message = '') {
$js_rules[$name]['notEqualTo'] = ':input[name=\'submitted[' . $value['form_key'] . ']\']';
$message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
$js_rules[$name]['messages']['notEqualTo'] = $message;
......@@ -397,7 +395,7 @@ function _clientside_validation_set_not_equal($name, $title, $value, &$js_rules,
/**
* Set validation rule for fields that must be equal to a specific value.
*/
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = ''){
function _clientside_validation_set_specific_value($name, $title, $value, &$js_rules, $message = '') {
$js_rules[$name]['oneOf'] = $value;
$message = empty($message) ? t('!name field has to different from !firstone.', array('!name' => $title, '!firstone' => $value['name'])) : $message;
$js_rules[$name]['messages']['oneOf'] = $message;
......@@ -406,7 +404,7 @@ function _clientside_validation_set_specific_value($name, $title, $value, &$js_r
/**
* Set validation rule for ean number fields.
*/
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = ''){
function _clientside_validation_set_ean($name, $title, &$js_rules, $message = '') {
$message = empty($message) ? t('!name field is not a valid EAN number.', array('!name' => $title)) : $message;
$js_rules[$name]['validEAN'] = TRUE;
}
......@@ -414,7 +412,7 @@ function _clientside_validation_set_ean($name, $title, &$js_rules, $message = ''
/**
* Set validation rule for file fields that must have a certain extension.
*/
function _clientside_validation_set_extensions($name, $extensions, &$js_rules){
function _clientside_validation_set_extensions($name, $extensions, &$js_rules) {
$extension_list = preg_replace('#,(?![^,]+,)#', ' or', implode(', ', $extensions));
$js_rules[$name]['accept'] = implode('|', $extensions);
$js_rules[$name]['messages']['accept'] = t("Only files with a %exts extension are allowed.", array('%exts' => $extension_list));
......@@ -423,20 +421,20 @@ function _clientside_validation_set_extensions($name, $extensions, &$js_rules){
/**
* Set validation rule for checkboxes.
*/
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, $count, &$js_rules, $min = 1, $max = 99){
function _clientside_validation_set_checkboxgroup_minmax($name, $title, $id, $count, &$js_rules, $min = 1, $max = 99) {
$js_rules[$name]['checkboxgroupminmax'] = array($min, $max, $id);
if ($count == 0) {
$js_rules[$name]['messages']['checkboxgroupminmax'] = t('!name field is required.', array('!name' => $title));
}
else {
$js_rules[$name]['messages']['checkboxgroupminmax'] = false;
$js_rules[$name]['messages']['checkboxgroupminmax'] = FALSE;
}
}
/**
* Set validation rule for email fields.
*/
function _clientside_validation_set_email($name, $title, &$js_rules){
function _clientside_validation_set_email($name, $title, &$js_rules) {
$js_rules[$name]['email'] = TRUE;
$js_rules[$name]['messages']['email'] = t('The value in !name is not a valid email address.', array('!name' => $title));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment