Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Isses with gravityforms-repeater #94

Open
MenashayAu opened this issue Apr 22, 2017 · 9 comments
Open

Isses with gravityforms-repeater #94

MenashayAu opened this issue Apr 22, 2017 · 9 comments

Comments

@MenashayAu
Copy link

MenashayAu commented Apr 22, 2017

I found three issues with this plugin (version 1.1.0-dev14) -
Issue 1:
'Name' field always outputs only the last field in the 'name' (if you activate the 'first', and the 'last' for instance, you will only get data from 'last'. If you activate in the 'name' field 'first', 'last' and 'suffix' you will only get data from 'suffix'). By 'output' I mean when you look at 'Entries' from the Gravity Forms dashboard you will see data just for a one field from the 'name' field (it is always the last field that is activated in the 'name' field)

Issue 2:
Same as issue 1 with address field, again only the last field is outputting the data.

Issue 3:
Fields do not output data when you put conditional logic.
Put conditional logic in fields that are inside the repeater (between the repeater start and the repeater end). Now submit your form and look at 'Entries' from the Gravity Forms dashboard. No field from the repeater is shown.
Gravity Forms version: 2.2.1

@MenashayAu
Copy link
Author

I found the bug for you.
Have a closer look at your php file 'class-gf-field-repeater.php'
Look at line 421, it reads '$inputData[1] = $getInputData;
You hard-coded the array to always populate only a one element (1).
The fix: $inputData[] = $getInputData; (without the 1 in the brackets).
Regards,

@kodie kodie reopened this Apr 25, 2017
@MenashayAu
Copy link
Author

MenashayAu commented Apr 27, 2017

You have a really nasty nasty bug. And I mean nasty.
Symptom-
Put a radio button in a repeater, call it say, 'Gender' and populate the options with 'Female' then 'Male.'
Outside the repeater put a field say a text field. Make both fields (the radio button and the text field) mandatory.
Preview your form and select 'Female' (the first option). Do not populate the text field. Submit your form and as expected you will get error saying the text field is required. So far so good. However, have a closer look at the gender radio button You will notice that 'Male' is now populated which is incorrect. In fact if you change your radio button to have say three options, whatever, 'Yes', 'No' and 'Undecided' you will see that the last option of the radio button is always populated following this error (when a field is mandatory but you did not populate it).

If you have a long form, chances are your users will simply populate the missing fields and re-submit the form sending you incorrect radio button selections.

I am not a PHP developer but I am a JavaScript developer, so after debugging this nasty bug for the last few days I managed to find the bugs and also provide a fix.
Your repeater.js is in error and here is the fix.
May not be the most elegant way but it does work -

function gfRepeater_getInputValue(inputElement) {
	// Menashay Givoni (http://customiseit.com.au) 27-Apr-2017, process radio and checkbox separately
	// iterate through all radio options to find if any option is checked.
	// The way it was, returned 'false' when a radio option is not checked (and hence displays error 'field is required' for required fields)
	// but in a radio button only a one option can be checked, other options are not checked. When a radio option is not checked it does not mean error
	if(inputElement.is(':radio')) {
	   var parent = jQuery(inputElement).parent().parent();
	   var isChecked = false;
		   var r = parent.find('input:radio');		   
		   for(var k=0;k<r.length;k++) {
		   	if(jQuery(r[k]).is(':checked')) {
		   	  	isChecked = true;
		   	  	// break the loop. Only a one radio option can be checked.
		   	  	continue;
		   	  } 
		   }	  
	   	   return isChecked;
	   	   	   
	} else if (inputElement.is(':checkbox')) {
		if (inputElement.prop('checked') == true) { return true; } else { return false; }
	} else {
		return inputElement.val();
	}
}

/*
	gfRepeater_setInputValue(inputElement, value)
		Sets the value of an input.

		inputElement	The input element.
		inputValue	The value to set to the input.
*/
function gfRepeater_setInputValue(inputElement, inputValue) {
	// Menashay Givoni (http://customiseit.com.au) 27-Apr-2017, process radio and checkbox separately
	// for radio button, get the radio element from the parent element. Then iterate through all radio options and apply the 'checked' property only one time.
	// The way it was each element of the radio button was processed separately. The property 'checked' would then be applied twice (or more)
	// instead of to only to a one radio option.
	if (inputElement.is(':radio')) {	
		var parent = jQuery(inputElement).parent().parent()
		   var r = parent.find('input:radio');		   
		   for(var k=0;k<r.length;k++){		   
		   	if(jQuery(r[k]).val() === inputValue){
		   	  jQuery(r[k]).prop('checked',true);
			  //  do not break the loop in case there are more radio buttons 
			  // in the repeater with the same radio button options, such as yes/no buttons
		   	  } else {
		   	  	jQuery(r[k]).prop('checked', false);
		   	  }
		   }	  
	} else if(inputElement.is(':checkbox')) {
		if (inputValue) { inputElement.prop('checked', true) } else { inputElement.prop('checked', false) }
	} else {
		inputElement.val(inputValue);
	}
}

Regards,
Menashay

@MenashayAu
Copy link
Author

MenashayAu commented Apr 28, 2017

Kodie,
You have more issues in your js (repeater.js)
Symptom:
Put two fields in the repeater and use the Gravity Forms predefined CSS style such as gf_left_half and gf_right_half
Now put a radio button so that the fields in the repeater are either hidden or shown.
You will notice that when you show the fields in the repeater the DOM ignores the CSS styles.
In this case instead of being 50% width each on the same row they will be vertically one under the other.

Looking at the DOM you will notice that -

  • the CSS style is correctly in the HTML tag
  • the CSS display value is incorrect

Here is the fix (staring from line 1146 of your code) -

jQuery(document).bind('gform_post_conditional_logic', function(e, formId, fields, isInit){
				if (fields) {
					if (jQuery.inArray(startIdNum, fields) !== -1) {
						var startElement = repeater['controllers']['start'];
						var endElement = repeater['controllers']['end'];
						var fieldDisplay = startElement.css('display');
						var repeaterChildren = gfRepeater_select(formId, repeaterId);
						// M.G (http://customiseit.com.au) 28-Apr-2017
						// when css is 'none' enforce this css so that child elements are not shown (hidden)
						// when however the parent element (startElement) is populated with a value
						// do not populate the child elements with  this same css value. 
						// Child elements may have different css (such as display: inline-block in the case of gf_left_half, gf_right_half and other
						// predefined css styles).
						// It used to force all child elements to the same css as the parent resulting in a loss of the individual css style
						if(fieldDisplay === 'none') {
							repeaterChildren.css('display', fieldDisplay);
						} else {
							// clear the previously set display css
							repeaterChildren.css('display', '');
						}
						endElement.css('display', fieldDisplay);
					}
				}
			});

@MenashayAu
Copy link
Author

I've made more changes to this plugin and it seems to finally work as expected (though I acknowledge not testing extensively).
This time not bugs but a few changes to enable this plugin to work with the popular Gravity Forms PDF plugin. Instead of showing each and every change that I've made, I am uploading the file here.
Note however that changes are also required to the Gravity Forms PDF plugin so that it works with the HTMl styles and the repeater.
class-gf-field-repeater.php.zip

Regads,
Menasahy

@garretthyder
Copy link

Hey @MenashayAu
These are awesome changes, I know the plugin dev doesn't work too much with WP these days but he does accept pull requests so if you're up for it I'm sure you'll see your changes make their way into the plugin faster if you supply code/fixes via pull requests on this repo.
Just a thought as I'd like to see your changes make their way into the core as well.
All the best

@elton-ot
Copy link

@MenashayAu How do i make this plugin work with Gravity PDF ?
i have used your code, but what changes have to be done in gravity PDF
Thank you.

@MenashayAu
Copy link
Author

MenashayAu commented Nov 23, 2017

@elton-ot It's been awhile. I will check and get back to you.

@elton-ot
Copy link

@MenashayAu Hey, i created a new form again and now its working..
Thank you so much for the code :)

@MenashayAu
Copy link
Author

Hi @elton-ot,
Good to know it is working for you.
If you still need the changes to the Gravity PDF here they are (compare to the latest brunch though before you apply these changes).
The changes are to file 'View_PDF.php' that is in 'gravity-forms-pdf-extended/src/view'
(from line 483) -
`

   /* Try and display our HTML */
    try {
   	   
   	    /*
   	    * Menashay Givoni (http://customiseit.com.au) 29-Apr-2017
   	    * Process repeater field separately
   	    */ 	
        if($field -> type === 'repeater') {
        echo($field -> get_value_export($entry, '', 'pdf', false));
        } else {
    	
        /* Only load our HTML if the field is NOT empty, or the $empty config option is true */
        if ( ! $class->is_empty() || $show_empty_fields === true ) {
            /* Load our legacy CSS class names */
            if ( $load_legacy_css === true ) {
             ...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants