mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-09-28 20:29:27 +02:00
FIX Resolve issue with multiple dependent rules
Implementation of the rules required that a dependent field could only rely on one implementation. This patch joins all those dependencies together in one function to exclude. Tests / a rewrite of this behavior is on the horizon.
This commit is contained in:
parent
ef56577e66
commit
527b63786d
@ -549,7 +549,10 @@ JS
|
|||||||
public function generateConditionalJavascript() {
|
public function generateConditionalJavascript() {
|
||||||
$default = "";
|
$default = "";
|
||||||
$rules = "";
|
$rules = "";
|
||||||
|
|
||||||
|
$watch = array();
|
||||||
|
$watchLoad = array();
|
||||||
|
|
||||||
if($this->Fields()) {
|
if($this->Fields()) {
|
||||||
foreach($this->Fields() as $field) {
|
foreach($this->Fields() as $field) {
|
||||||
$fieldId = $field->Name;
|
$fieldId = $field->Name;
|
||||||
@ -668,36 +671,64 @@ JS
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!isset($watch[$fieldToWatch])) {
|
||||||
|
$watch[$fieldToWatch] = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$watch[$fieldToWatch][] = array(
|
||||||
|
'expression' => $expression,
|
||||||
|
'field_id' => $fieldId,
|
||||||
|
'view' => $view,
|
||||||
|
'opposite' => $opposite
|
||||||
|
);
|
||||||
|
|
||||||
// Register conditional behaviour with an element, so it can be triggered from many places.
|
$watchLoad[$fieldToWatchOnLoad] = true;
|
||||||
$rules .= $fieldToWatch.".each(function() {
|
|
||||||
$(this).data('userformConditions', function() {
|
|
||||||
if(". $expression ." ) {
|
|
||||||
$(\"#". $fieldId ."\").".$view."();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$(\"#". $fieldId ."\").".$opposite."();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});";
|
|
||||||
|
|
||||||
// Trigger update on element changes.
|
|
||||||
$rules .= $fieldToWatch.".$action(function() {
|
|
||||||
$(this).data('userformConditions').call(this);
|
|
||||||
});\n";
|
|
||||||
|
|
||||||
// Trigger update on load (if server-side validation fails some fields will have different values than defaults).
|
|
||||||
$rules .= $fieldToWatchOnLoad.".each(function() {
|
|
||||||
$(this).data('userformConditions').call(this);
|
|
||||||
});\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($watch) {
|
||||||
|
foreach($watch as $key => $values) {
|
||||||
|
$logic = array();
|
||||||
|
|
||||||
|
foreach($values as $rule) {
|
||||||
|
// Register conditional behaviour with an element, so it can be triggered from many places.
|
||||||
|
$logic[] = sprintf(
|
||||||
|
'if(%s) { $("#%s").%s(); } else { $("#%2$s").%s(); }',
|
||||||
|
$rule['expression'],
|
||||||
|
$rule['field_id'],
|
||||||
|
$rule['view'],
|
||||||
|
$rule['opposite']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
$logic = implode("\n", $logic);
|
||||||
|
$rules .= $key.".each(function() {\n
|
||||||
|
$(this).data('userformConditions', function() {\n
|
||||||
|
$logic\n
|
||||||
|
}); \n
|
||||||
|
});\n";
|
||||||
|
|
||||||
|
$rules .= $key.".$action(function() {
|
||||||
|
$(this).data('userformConditions').call(this);\n
|
||||||
|
});\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($watchLoad) {
|
||||||
|
foreach($watchLoad as $key => $value) {
|
||||||
|
$rules .= $key.".each(function() {
|
||||||
|
$(this).data('userformConditions').call(this);\n
|
||||||
|
});\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Only add customScript if $default or $rules is defined
|
// Only add customScript if $default or $rules is defined
|
||||||
if($default || $rules) {
|
if($default || $rules) {
|
||||||
Requirements::customScript(<<<JS
|
Requirements::customScript(<<<JS
|
||||||
(function($) {
|
(function($) {
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user