Skip to content

Commit

Permalink
Updated jquery validate plugin (dist files only)
Browse files Browse the repository at this point in the history
Cleanup of validation script. Refactor into template to allow customisation of validation.
Explicitly set error element to 'span' as per new jquery-validate support
[ref: CWPBUG-110]
  • Loading branch information
Damian Mooyman committed May 21, 2014
1 parent 7a77cfb commit 5e0b7fd
Show file tree
Hide file tree
Showing 245 changed files with 3,056 additions and 56,263 deletions.
4 changes: 4 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<?php

if(!defined('USERFORMS_DIR')) {
define('USERFORMS_DIR', basename(__DIR__));
}
82 changes: 21 additions & 61 deletions code/model/UserDefinedForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,20 @@ public function init() {
parent::init();

// load the jquery
$lang = i18n::get_lang_from_locale(i18n::get_locale());
Requirements::javascript(FRAMEWORK_DIR .'/thirdparty/jquery/jquery.js');
Requirements::javascript('userforms/thirdparty/jquery-validate/jquery.validate.js');
Requirements::add_i18n_javascript('userforms/javascript/lang');
Requirements::javascript('userforms/javascript/UserForm_frontend.js');
Requirements::javascript('userforms/thirdparty/jquery-validate/localization/messages_' . i18n::get_lang_from_locale(i18n::get_locale()) . '.js');
Requirements::javascript('userforms/thirdparty/jquery-validate/localization/methods_' . i18n::get_lang_from_locale(i18n::get_locale()) . '.js');
if($this->HideFieldLabels) Requirements::javascript('userforms/thirdparty/Placeholders.js/Placeholders.min.js');
Requirements::javascript(USERFORMS_DIR . '/thirdparty/jquery-validate/jquery.validate.min.js');
Requirements::add_i18n_javascript(USERFORMS_DIR . '/javascript/lang');
Requirements::javascript(USERFORMS_DIR . '/javascript/UserForm_frontend.js');
Requirements::javascript(
USERFORMS_DIR . "/thirdparty/jquery-validate/localization/messages_{$lang}.min.js"
);
Requirements::javascript(
USERFORMS_DIR . "/thirdparty/jquery-validate/localization/methods_{$lang}.min.js"
);
if($this->HideFieldLabels) {
Requirements::javascript(USERFORMS_DIR . '/thirdparty/Placeholders.js/Placeholders.min.js');
}
}

/**
Expand Down Expand Up @@ -592,63 +599,16 @@ public function getFormActions() {
* @return RequiredFields
*/
public function getRequiredFields() {
$required = new RequiredFields();

$rules = array();
$validation = array();
$messages = array();
$onfocusout = "";
$hidelabels = "";

if($this->Fields()) {
foreach($this->Fields() as $field) {
if (!in_array($field->ClassName, array('EditableEmailField', 'EditableNumericField'))) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
}

if($field->Required) {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
$required->addRequiredField($field->Name);
}
}
}

// Enable live validation
if($this->EnableLiveValidation) $onfocusout = ", onfocusout : function(element) { this.element(element); }";

// Hide field labels (use HTML5 placeholder instead)
if($this->HideFieldLabels) $hidelabels = '$("#Form_Form label.left").each(function(){$("#"+$(this).attr("for")).attr("placeholder",$(this).text());$(this).remove();});Placeholders.init();';

// Set the Form Name
$rules = $this->array2json($rules);
$messages = $this->array2json($messages);

// set the custom script for this form
Requirements::customScript(<<<JS
(function($) {
$(document).ready(function() {
$("#Form_Form").validate({
ignore: ':hidden',
errorClass: "required",
errorPlacement: function(error, element) {
if(element.is(":radio")) {
error.insertAfter(element.closest("ul"));
} else {
error.insertAfter(element);
}
},
messages:
$messages
,
rules:
$rules
$onfocusout
});
$hidelabels
});
})(jQuery);
JS
, 'UserFormsValidation');
Requirements::customScript($this->renderWith('ValidationScript'), 'UserFormsValidation');

// Generate required field validator
$requiredNames = $this
->Fields()
->filter('Required', true)
->column('Name');
$required = new RequiredFields($requiredNames);

$this->extend('updateRequiredFields', $required);

Expand Down
2 changes: 1 addition & 1 deletion code/model/formfields/EditableCountryDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function getValueFromData($data) {
}

public function getIcon() {
return 'userforms/images/editabledropdown.png';
return USERFORMS_DIR . '/images/editabledropdown.png';
}
}
4 changes: 2 additions & 2 deletions code/model/formfields/EditableDateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public function getFormField() {
* @return Array
*/
public function getValidation() {
return array(
return array_merge(parent::getValidation(), array(
'date' => true
);
));
}
}
8 changes: 6 additions & 2 deletions code/model/formfields/EditableEmailField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class EditableEmailField extends EditableFormField {

private static $plural_name = 'Email Fields';

public function getSetsOwnError() {
return true;
}

public function getFormField() {
if ($this->Required) {
// Required and Email validation can conflict so add the Required validation messages
Expand All @@ -35,8 +39,8 @@ public function getFormField() {
* @return Array
*/
public function getValidation() {
return array(
return array_merge(parent::getValidation(), array(
'email' => true
);
));
}
}
19 changes: 17 additions & 2 deletions code/model/formfields/EditableFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public function EditSegment() {
return $this->renderWith('EditableFormField');
}

/**
* Flag indicating that this field will set its own error message via data-msg='' attributes
*
* @return bool
*/
public function getSetsOwnError() {
return false;
}

/**
* Return whether a user can delete this form field
* based on whether they can edit the page
Expand Down Expand Up @@ -212,7 +221,7 @@ public function getSetting($setting) {
* @return string
*/
public function getIcon() {
return 'userforms/images/' . strtolower($this->class) . '.png';
return USERFORMS_DIR . '/images/' . strtolower($this->class) . '.png';
}

/**
Expand Down Expand Up @@ -486,7 +495,13 @@ public function showInReports() {
* @return Array
*/
public function getValidation() {
return array();
return $this->Required
? array('required' => true)
: array();
}

public function getValidationJSON() {
return Convert::raw2json($this->getValidation());
}

/**
Expand Down
4 changes: 4 additions & 0 deletions code/model/formfields/EditableNumericField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class EditableNumericField extends EditableTextField {

private static $plural_name = 'Numeric Fields';

public function getSetsOwnError() {
return true;
}


/**
* @return TextareaField|TextField
Expand Down
8 changes: 5 additions & 3 deletions code/model/formfields/EditableTextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ public function getFormField() {
* @return array
*/
public function getValidation() {
$options = array();
$options = parent::getValidation();

if($this->getSetting('MinLength'))
if($this->getSetting('MinLength')) {
$options['minlength'] = $this->getSetting('MinLength');
}

if($this->getSetting('MaxLength'))
if($this->getSetting('MaxLength')) {
$options['maxlength'] = $this->getSetting('MaxLength');
}

return $options;
}
Expand Down
44 changes: 44 additions & 0 deletions templates/ValidationScript.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(function($) {
$(document).ready(function() {
$("#Form_Form").validate({
ignore: ':hidden',
errorClass: "required",
errorElement: "span",
errorPlacement: function(error, element) {
error.addClass('message')
if(element.is(":radio")) {
error.insertAfter(element.closest("ul"));
} else {
error.insertAfter(element);
}
},
messages: {
<% loop $Fields %>
<% if $ErrorMessage && not $SetsOwnError %>
'{$Name.JS}': '{$ErrorMessage.JS}',
<% end_if %>
<% end_loop %>
},
rules: {
<% loop $Fields %>
<% if $Validation %>
'{$Name.JS}': {$ValidationJSON.RAW},
<% end_if %>
<% end_loop %>
},
<% if $EnableLiveValidation %>
// Enable live validation
onfocusout : function(element) { this.element(element); }
<% end_if %>
});
<% if $HideFieldLabels %>
// Hide field labels (use HTML5 placeholder instead)
$("#Form_Form label.left").each(function() {
$("#"+$(this).attr("for"))
.attr("placeholder", $(this).text());
$(this).remove();
});
Placeholders.init();
<% end_if %>
});
})(jQuery);
2 changes: 0 additions & 2 deletions thirdparty/jquery-validate/.gitattributes

This file was deleted.

9 changes: 0 additions & 9 deletions thirdparty/jquery-validate/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions thirdparty/jquery-validate/.travis.yml

This file was deleted.

16 changes: 0 additions & 16 deletions thirdparty/jquery-validate/CONTRIBUTING.md

This file was deleted.

Loading

0 comments on commit 5e0b7fd

Please sign in to comment.