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();
|
||||
|
||||
return new CheckboxSetField($this->Name, $this->Title, $optionMap);
|
||||
return new UserFormsCheckboxSetField($this->Name, $this->Title, $optionMap);
|
||||
}
|
||||
|
||||
public function getValueFromData($data) {
|
||||
|
@ -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 %>
|
||||
|
Loading…
x
Reference in New Issue
Block a user