Permalink
Browse files

MINOR FEATURE Not entirely clean, but this prevents conditional field…

…s that are not visible, to be still required. But will only trigger a warning when visible.
  • Loading branch information...
1 parent 9020ce8 commit 62f26f58e4ff3ef3babda3af438ef9c14acad2c2 Simon Erkelens committed Oct 6, 2011
Showing with 70 additions and 4 deletions.
  1. +70 −4 code/UserDefinedForm.php
@@ -455,14 +455,75 @@ function getRequiredFields() {
if($this->Fields()) {
foreach($this->Fields() as $field) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
-
+ /**
+ * Dependency defaults to a:0:{}
+ * So, escape it temporary solution, to have the custom rule not fubar the validation.
+ *
+ * @todo implement that will more gracefully drive the user nuts.
+ */
if($field->Required) {
+ if($field->CustomRules != 'a:0:{}'){
+ $Rules = unserialize($field->CustomRules);
+ foreach($Rules as $rule){
+ // is this field a special option field
+ $formFieldWatch = DataObject::get_one("EditableFormField", "\"Name\" = '".$rule['ConditionField']."'");
+ $checkboxField = false;
+ if(in_array($formFieldWatch->ClassName, array('EditableCheckboxGroupField', 'EditableCheckbox'))) {
+ $action = "click";
+ $checkboxField = true;
+ }
+ switch($rule['ConditionOption']) {
+ case 'IsNotBlank':
+ $expression = ($checkboxField) ? '$("#Form_Form_'.$rule['ConditionField'].'").attr("checked")' : '$("#Form_Form_'.$rule['ConditionField'].'").val() != ""';
+
+ break;
+ case 'IsBlank':
+ $expression = ($checkboxField) ? '!($("#Form_Form_'.$rule['ConditionField'].'").attr("checked"))' : '$("#Form_Form_'.$rule['ConditionField'].'").val() == ""';
+
+ break;
+ case 'HasValue':
+ $expression = ($checkboxField) ? '$("#Form_Form_'.$rule['ConditionField'].'").attr("checked")' : '$("#Form_Form_'.$rule['ConditionField'].'").val() == "'. $dependency['Value'] .'"';
+
+ break;
+ case 'ValueLessThan':
+ $expression = '$("#Form_Form_'.$rule['ConditionField'].'").val() < parseFloat("'. $dependency['Value'] .'")';
+
+ break;
+ case 'ValueLessThanEqual':
+ $expression = '$("#Form_Form_'.$rule['ConditionField'].'").val() <= parseFloat("'. $dependency['Value'] .'")';
+
+ break;
+ case 'ValueGreaterThan':
+ $expression = '$("#Form_Form_'.$rule['ConditionField'].'").val() > parseFloat("'. $dependency['Value'] .'")';
+
+ break;
+ case 'ValueGreaterThanEqual':
+ $expression = '$("#Form_Form_'.$rule['ConditionField'].'").val() >= parseFloat("'. $dependency['Value'] .'")';
+
+ break;
+ default:
+ $expression = ($checkboxField) ? '!($("#Form_Form_'.$rule['ConditionField'].'").attr("checked"))' : '$("#Form_Form_'.$rule['ConditionField'].'").val() != "'. $dependency['Value'] .'"';
+
+ break;
+ }
+ $rules[$field->Name] = array_merge(
+ array(
+ 'required' => 'function(element){ return ('. $expression .');}',
+ ),
+ $field->getValidation()
+ );
+ }
+ }
+ else {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
$required->addRequiredField($field->Name);
+ }
}
+ else{
+ $field->Required = 0;
+ }
}
- }
-
+ }
// Set the Form Name
$rules = $this->array2json($rules);
$messages = $this->array2json($messages);
@@ -628,8 +689,13 @@ function array2json($array) {
if(is_array( $value )) {
$result[] = "$key:" . $this->array2json($value);
} else {
+ /**
+ * Little 'ugly' fix to skip functions to be quoted
+ */
+ if(!(substr($value, 0, 8) == 'function')){
$value = (is_bool($value)) ? $value : "\"$value\"";
- $result[] = "$key:$value";
+ }
+ $result[] = "$key:$value";
}
return (isset($result)) ? "{\n".implode( ', ', $result ) ."\n}\n": '{}';
}

0 comments on commit 62f26f5

Please sign in to comment.