mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
BUGFIX: fixed custom rules for radio boxes and checkbox lists
This commit is contained in:
parent
34bfd0f8c6
commit
cf35c706e4
@ -229,39 +229,64 @@ class UserDefinedForm_Controller extends Page_Controller {
|
|||||||
|
|
||||||
// Check for field dependencies / default
|
// Check for field dependencies / default
|
||||||
if($field->Dependencies()) {
|
if($field->Dependencies()) {
|
||||||
|
|
||||||
foreach($field->Dependencies() as $dependency) {
|
foreach($field->Dependencies() as $dependency) {
|
||||||
if(is_array($dependency) && isset($dependency['ConditionField']) && $dependency['ConditionField'] != "") {
|
if(is_array($dependency) && isset($dependency['ConditionField']) && $dependency['ConditionField'] != "") {
|
||||||
// get the field which is effected
|
// get the field which is effected
|
||||||
$formName = Convert::raw2sql($dependency['ConditionField']);
|
$formName = Convert::raw2sql($dependency['ConditionField']);
|
||||||
$formFieldWatch = DataObject::get_one("EditableFormField", "Name = '$formName'");
|
$formFieldWatch = DataObject::get_one("EditableFormField", "Name = '$formName'");
|
||||||
|
|
||||||
if(!$formFieldWatch) break;
|
if(!$formFieldWatch) break;
|
||||||
|
|
||||||
$fieldToWatch = "$(\"#Form_Form_".$dependency['ConditionField']."\")";
|
// watch out for multiselect options - radios and check boxes
|
||||||
|
if(is_a($formFieldWatch, 'EditableDropdownField')) {
|
||||||
|
$fieldToWatch = "$(\"select[name='".$dependency['ConditionField']."]'\")";
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch out for checkboxs as the inputs don't have values but are 'checked
|
||||||
|
else if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) {
|
||||||
|
$fieldToWatch = "$(\"input[name='".$dependency['ConditionField']."[".$dependency['Value']."]']\")";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$fieldToWatch = "$(\"input[name='".$dependency['ConditionField']."']\")";
|
||||||
|
}
|
||||||
|
|
||||||
// show or hide?
|
// show or hide?
|
||||||
$view = (isset($dependency['Display']) && $dependency['Display'] == "Show") ? "show" : "hide";
|
$view = (isset($dependency['Display']) && $dependency['Display'] == "Show") ? "show" : "hide";
|
||||||
$opposite = ($view == "show") ? "hide" : "show";
|
$opposite = ($view == "show") ? "hide" : "show";
|
||||||
|
|
||||||
|
// what action do we need to keep track of
|
||||||
$Action = ($formFieldWatch->ClassName == "EditableTextField") ? "keyup" : "change";
|
$Action = ($formFieldWatch->ClassName == "EditableTextField") ? "keyup" : "change";
|
||||||
|
|
||||||
|
// and what should we evaluate
|
||||||
switch($dependency['ConditionOption']) {
|
switch($dependency['ConditionOption']) {
|
||||||
case 'IsNotBlank':
|
case 'IsNotBlank':
|
||||||
$matches = '!= ""';
|
$expression = '$(this).val() != ""';
|
||||||
|
if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) {
|
||||||
|
$expression = '$(this).attr("checked")';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'IsBlank':
|
case 'IsBlank':
|
||||||
$matches = '== ""';
|
$expression = '$(this).val() == ""';
|
||||||
|
if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) {
|
||||||
|
$expression = '!($(this).attr("checked"))';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'HasValue':
|
case 'HasValue':
|
||||||
$matches = '== "'. $dependency['Value'] .'"';
|
$expression = '$(this).val() == "'. $dependency['Value'] .'"';
|
||||||
|
if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) {
|
||||||
|
$expression = '$(this).attr("checked")';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$matches = '!= "'. $dependency['Value'] .'"';
|
$expression = '$(this).val() != "'. $dependency['Value'] .'"';
|
||||||
|
if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) {
|
||||||
|
$expression = '!($(this).attr("checked"))';
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// put it all together
|
// put it all together
|
||||||
$CustomDisplayRules .= $fieldToWatch.".$Action(function() {
|
$CustomDisplayRules .= $fieldToWatch.".$Action(function() {
|
||||||
if($(this).val() ". $matches ." ) {
|
if(". $expression ." ) {
|
||||||
$(\"#". $field->Name ."\").".$view."();
|
$(\"#". $field->Name ."\").".$view."();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -163,10 +163,10 @@ class EditableFormField extends DataObject {
|
|||||||
'AddableOption' => true,
|
'AddableOption' => true,
|
||||||
'Fields' => $fields
|
'Fields' => $fields
|
||||||
)));
|
)));
|
||||||
|
|
||||||
// check for existing ones
|
// check for existing ones
|
||||||
if($this->CustomRules) {
|
if($this->CustomRules) {
|
||||||
$rules = unserialize($this->CustomRules);
|
$rules = unserialize($this->CustomRules);
|
||||||
|
|
||||||
if($rules) {
|
if($rules) {
|
||||||
foreach($rules as $rule => $data) {
|
foreach($rules as $rule => $data) {
|
||||||
// recreate all the field object to prevent caching
|
// recreate all the field object to prevent caching
|
||||||
@ -256,6 +256,7 @@ class EditableFormField extends DataObject {
|
|||||||
if(isset($data['CustomRules'])) {
|
if(isset($data['CustomRules'])) {
|
||||||
$rules = array();
|
$rules = array();
|
||||||
foreach($data['CustomRules'] as $key => $value) {
|
foreach($data['CustomRules'] as $key => $value) {
|
||||||
|
|
||||||
if(is_array($value)) {
|
if(is_array($value)) {
|
||||||
$fieldValue = (isset($value['Value'])) ? $value['Value'] : '';
|
$fieldValue = (isset($value['Value'])) ? $value['Value'] : '';
|
||||||
if(isset($value['ConditionOption']) && $value['ConditionOption'] == "Blank" || $value['ConditionOption'] == "NotBlank") {
|
if(isset($value['ConditionOption']) && $value['ConditionOption'] == "Blank" || $value['ConditionOption'] == "NotBlank") {
|
||||||
|
@ -66,6 +66,5 @@ class EditableOption extends DataObject {
|
|||||||
$this->readonly = true;
|
$this->readonly = true;
|
||||||
return $this->EditSegment();
|
return $this->EditSegment();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
x
Reference in New Issue
Block a user