Fix CheckboxGroupField validation (Fixes #209)

This commit is contained in:
Will Rossiter 2014-07-25 14:52:56 +12:00
parent 9b245ff3fc
commit 5568ee7875
3 changed files with 37 additions and 12 deletions

View File

@ -0,0 +1,23 @@
<?php
/**
* @package userforms
*/
class UserFormsCheckboxSetField extends CheckboxSetField {
/**
* jQuery validate requires that the value of the option does not contain
* the actual value of the input.
*
* @return ArrayList
*/
public function getOptions() {
$options = parent::getOptions();
foreach($options as $option) {
$option->Name = "{$this->name}[]";
}
return $options;
}
}

View File

@ -19,7 +19,7 @@ class EditableCheckboxGroupField extends EditableMultipleOptionField {
$optionMap = ($optionSet) ? $optionSet->map('EscapedTitle', 'Title') : array();
return new CheckboxSetField($this->Name, $this->Title, $optionMap);
return new UserFormsCheckboxSetField($this->Name, $this->Title, $optionMap);
}
public function getValueFromData($data) {

View File

@ -1,29 +1,31 @@
(function($) {
$(document).ready(function() {
var messages = {<% loop $Fields %><% if $ErrorMessage && not $SetsOwnError %><% if ClassName == EditableCheckboxGroupField %>
'{$Name.JS}[]': '{$ErrorMessage.JS}'<% else %>
'{$Name.JS}': '{$ErrorMessage.JS}',<% end_if %><% end_if %><% end_loop %>
};
$("#Form_Form").validate({
ignore: ':hidden',
errorClass: "required",
errorElement: "span",
errorPlacement: function(error, element) {
error.addClass('message')
if(element.is(":radio")) {
error.addClass('message');
if(element.is(":radio") || element.parents(".checkboxset").length > 0) {
error.insertAfter(element.closest("ul"));
} else {
error.insertAfter(element);
}
},
messages: {
<% loop $Fields %>
<% if $ErrorMessage && not $SetsOwnError %>
'{$Name.JS}': '{$ErrorMessage.JS}',
<% end_if %>
<% end_loop %>
},
messages: messages,
rules: {
<% loop $Fields %>
<% if $Validation %>
<% if $Validation %><% if ClassName == EditableCheckboxGroupField %>
'{$Name.JS}[]': {$ValidationJSON.RAW}
<% else %>
'{$Name.JS}': {$ValidationJSON.RAW},
<% end_if %>
<% end_if %><% end_if %>
<% end_loop %>
},
<% if $EnableLiveValidation %>