diff --git a/code/UserDefinedForm.php b/code/UserDefinedForm.php index 4ccf719..78d60e4 100755 --- a/code/UserDefinedForm.php +++ b/code/UserDefinedForm.php @@ -229,39 +229,64 @@ class UserDefinedForm_Controller extends Page_Controller { // Check for field dependencies / default if($field->Dependencies()) { - foreach($field->Dependencies() as $dependency) { if(is_array($dependency) && isset($dependency['ConditionField']) && $dependency['ConditionField'] != "") { // get the field which is effected $formName = Convert::raw2sql($dependency['ConditionField']); $formFieldWatch = DataObject::get_one("EditableFormField", "Name = '$formName'"); + 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? $view = (isset($dependency['Display']) && $dependency['Display'] == "Show") ? "show" : "hide"; $opposite = ($view == "show") ? "hide" : "show"; - + + // what action do we need to keep track of $Action = ($formFieldWatch->ClassName == "EditableTextField") ? "keyup" : "change"; + // and what should we evaluate switch($dependency['ConditionOption']) { case 'IsNotBlank': - $matches = '!= ""'; + $expression = '$(this).val() != ""'; + if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) { + $expression = '$(this).attr("checked")'; + } break; case 'IsBlank': - $matches = '== ""'; + $expression = '$(this).val() == ""'; + if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) { + $expression = '!($(this).attr("checked"))'; + } break; case 'HasValue': - $matches = '== "'. $dependency['Value'] .'"'; + $expression = '$(this).val() == "'. $dependency['Value'] .'"'; + if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) { + $expression = '$(this).attr("checked")'; + } break; default: - $matches = '!= "'. $dependency['Value'] .'"'; + $expression = '$(this).val() != "'. $dependency['Value'] .'"'; + if(is_a($formFieldWatch, 'EditableCheckboxGroupField')) { + $expression = '!($(this).attr("checked"))'; + } break; } // put it all together $CustomDisplayRules .= $fieldToWatch.".$Action(function() { - if($(this).val() ". $matches ." ) { + if(". $expression ." ) { $(\"#". $field->Name ."\").".$view."(); } else { diff --git a/code/editor/EditableFormField.php b/code/editor/EditableFormField.php index aa4a2df..0347b34 100755 --- a/code/editor/EditableFormField.php +++ b/code/editor/EditableFormField.php @@ -163,10 +163,10 @@ class EditableFormField extends DataObject { 'AddableOption' => true, 'Fields' => $fields ))); - // check for existing ones if($this->CustomRules) { $rules = unserialize($this->CustomRules); + if($rules) { foreach($rules as $rule => $data) { // recreate all the field object to prevent caching @@ -256,6 +256,7 @@ class EditableFormField extends DataObject { if(isset($data['CustomRules'])) { $rules = array(); foreach($data['CustomRules'] as $key => $value) { + if(is_array($value)) { $fieldValue = (isset($value['Value'])) ? $value['Value'] : ''; if(isset($value['ConditionOption']) && $value['ConditionOption'] == "Blank" || $value['ConditionOption'] == "NotBlank") { diff --git a/code/editor/EditableOption.php b/code/editor/EditableOption.php index 97c227c..9b62262 100644 --- a/code/editor/EditableOption.php +++ b/code/editor/EditableOption.php @@ -66,6 +66,5 @@ class EditableOption extends DataObject { $this->readonly = true; return $this->EditSegment(); } - } ?> \ No newline at end of file