Skip to content

Instantly share code, notes, and snippets.

@richardW8k
Last active August 29, 2015 14:08
Show Gist options
  • Save richardW8k/b19548dcbb28db6649f4 to your computer and use it in GitHub Desktop.
Save richardW8k/b19548dcbb28db6649f4 to your computer and use it in GitHub Desktop.
adding support for multi-input fields like the address field.
/*
Trigger conditional logic by adding an onclick or onchange event to your custom field input or by using something like the following
jQuery('#input_8_1 :input').on('change', function () {
gf_apply_rules(8, [2, 3, 4]);
});
*/
add_filter( 'gform_pre_render', 'extend_conditional_logic_frontend' );
function extend_conditional_logic_frontend( $form ) {
?>
<script>
gform.addFilter('gform_is_value_match', function (isMatch, formId, rule) {
if (!isMatch) {
// adds support for multi-input fields
var inputId = rule.fieldId.replace('.', '_'),
input = jQuery('#input_' + formId + '_' + inputId);
if (input.length == 1) {
var val = input.val();
isMatch = gf_matches_operation(val, rule.value, rule.operator);
}
}
return isMatch;
});
</script>
<?php
return $form;
}
add_filter( 'gform_admin_pre_render', 'extend_conditional_logic_backend' );
function extend_conditional_logic_backend( $form ) {
if ( GFCommon::is_entry_detail() ) {
return $form;
}
?>
<script>
gform.addFilter('gform_is_conditional_logic_field', function (isConditionalLogicField, field) {
return field.type == 'address' ? true : isConditionalLogicField;
});
gform.addFilter('gform_conditional_logic_fields', function (options, form, selectedFieldId) {
options = [];
var currentField = jQuery('.field_selected'),
currentFieldId = currentField.length == 1 ? currentField[0].id.substr(6) : 0;
for (var f = 0; f < form.fields.length; f++) {
if (objectType == 'field' && form.fields[f].id != currentFieldId) {
continue;
}
if (IsConditionalLogicField(form.fields[f])) {
var fieldInputs = form.fields[f].inputs,
inputType = GetInputType(form.fields[f]);
if (inputType != 'checkbox' && fieldInputs) {
for (var i = 0; i < fieldInputs.length; i++) {
options.push({
label: fieldInputs[i].label,
value: fieldInputs[i].id
});
}
} else {
options.push({
label: form.fields[f].adminLabel ? form.fields[f].adminLabel : form.fields[f].label,
value: form.fields[f].id
});
}
}
}
// get entry meta fields and append to existing fields
jQuery.merge(options, GetEntryMetaFields(selectedFieldId));
return options;
});
gform.addFilter('gform_conditional_logic_values_input', function (str, objectType, ruleIndex, selectedFieldId, selectedValue) {
var clField = GetFieldById(selectedFieldId),
fieldInput = GetInput(clField, selectedFieldId),
obj = GetConditionalObject(objectType),
rule = obj['conditionalLogic']['rules'][ruleIndex],
operator = rule.operator;
if (clField && fieldInput && jQuery.inArray(operator, ['is', 'isnot']) > -1) {
console.log('field has input');
var inputId = selectedFieldId.toString().replace('.', '_'),
input = jQuery('#input_' + inputId);
if (typeof fieldInput.choices != 'undefined') {
console.log('field has choices');
str = GetRuleValuesDropDown(fieldInput.choices, objectType, ruleIndex, selectedValue, false);
} else if (input.length == 1 && input.is('select')) {
var dropdown_id = objectType + '_rule_value_' + ruleIndex,
options = input.html();
options = options.replace('value="' + selectedValue + '"', 'value="' + selectedValue + '" selected="selected"');
str = "<select id='" + dropdown_id + "' class='gfield_rule_select gfield_rule_value_dropdown'>" + options + "</select>";
}
}
return str;
});
</script>
<?php
return $form;
}
@tareqhi
Copy link

tareqhi commented Apr 18, 2015

Hi Richard,thanks for all of your nice code and hooks.
Basically I'm a GF user. recently I faced few problem with file upload. I wanted to upload photos with file upload fields. And at the same time I needed to show the photos preview besides of upload field. Like in a picture box with thumbnail view.
The second things I needed is file upload on list field. I have the code to make a list field column as a upload field. But when I submitted forms the files are not uploading or lost.
Can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment