mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
Fix CheckboxGroupField validation (Fixes #209)
This commit is contained in:
parent
9b245ff3fc
commit
5568ee7875
23
code/formfields/UserFormsCheckboxSetField.php
Normal file
23
code/formfields/UserFormsCheckboxSetField.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@ class EditableCheckboxGroupField extends EditableMultipleOptionField {
|
|||||||
|
|
||||||
$optionMap = ($optionSet) ? $optionSet->map('EscapedTitle', 'Title') : array();
|
$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) {
|
public function getValueFromData($data) {
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
(function($) {
|
(function($) {
|
||||||
$(document).ready(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({
|
$("#Form_Form").validate({
|
||||||
ignore: ':hidden',
|
ignore: ':hidden',
|
||||||
errorClass: "required",
|
errorClass: "required",
|
||||||
errorElement: "span",
|
errorElement: "span",
|
||||||
errorPlacement: function(error, element) {
|
errorPlacement: function(error, element) {
|
||||||
error.addClass('message')
|
error.addClass('message');
|
||||||
if(element.is(":radio")) {
|
|
||||||
|
if(element.is(":radio") || element.parents(".checkboxset").length > 0) {
|
||||||
error.insertAfter(element.closest("ul"));
|
error.insertAfter(element.closest("ul"));
|
||||||
} else {
|
} else {
|
||||||
error.insertAfter(element);
|
error.insertAfter(element);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
messages: {
|
messages: messages,
|
||||||
<% loop $Fields %>
|
|
||||||
<% if $ErrorMessage && not $SetsOwnError %>
|
|
||||||
'{$Name.JS}': '{$ErrorMessage.JS}',
|
|
||||||
<% end_if %>
|
|
||||||
<% end_loop %>
|
|
||||||
},
|
|
||||||
rules: {
|
rules: {
|
||||||
<% loop $Fields %>
|
<% loop $Fields %>
|
||||||
<% if $Validation %>
|
<% if $Validation %><% if ClassName == EditableCheckboxGroupField %>
|
||||||
|
'{$Name.JS}[]': {$ValidationJSON.RAW}
|
||||||
|
<% else %>
|
||||||
'{$Name.JS}': {$ValidationJSON.RAW},
|
'{$Name.JS}': {$ValidationJSON.RAW},
|
||||||
<% end_if %>
|
<% end_if %><% end_if %>
|
||||||
<% end_loop %>
|
<% end_loop %>
|
||||||
},
|
},
|
||||||
<% if $EnableLiveValidation %>
|
<% if $EnableLiveValidation %>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user