Require All Columns of List Field
When you mark a List field as required, only one input from any column within that List is required. If you want to make all inputs required, we have a free solution.
April 26, 2022: Fixed issue where requiring specific columns did not apply to subsequent rows (again).
December 12, 2020: Fixed an issue where require list columns would fail validation if '0' is submitted.
July 14, 2015: Fixed issue where requiring specific columns did not apply to subsequent rows.
January 7, 2015: Added support any field with an input type of List field; this provides support for using this snippet with Post Custom Fields with an input type of List.
September 13th, 2012: Updated to use a class format to allow you to apply this functionality to a specific form and/or specific fields.
October 22, 2013: Updated to support a $require_columns parameter. Allows requiring specific columns rather than all columns on a list field.
When you mark a List field as required, only a single input from any column is required. What if you want every input in every column to be required? Here is a snippet that let’s you do it.
How do I install this snippet?
Just copy and paste the code above in your theme’s ”functions.php” file.
Do I need to configure this snippet to work with my form(s)?
Require All Columns on All Forms
Only if you want to apply this functionality to specific forms and/or specific fields. A default instantiation of this class (with no parameters) will apply this functionality to all list fields on all forms.
new GWRequireListColumns();
Require All Columns on a Specific Form
To apply this to a specific form, you would simply pass the form ID as the first parameter:
new GWRequireListColumns(4);
Require All Columns on a Specific Field
To apply this functionality to a specific form and a specific List field on that form, you would specify the form ID as the first parameter and the field ID as the second:
new GWRequireListColumns(4, 2);
Require All Columns on a Group of Fields
To apply this functionality to a specific form and multiple List fields on that form, you would specify the form ID as the first parameter and an array of fields IDs as the second parameter:
new GWRequireListColumns(4, array(2,3) );
Require Specific Columns on a Group of Fields
To require specific columns on a specific field, you would specific the form ID, the field ID and the specific columns (by index, starting with 1) you would like to require:
new GWRequireListColumns( 240, 1, array( 2, 3 ) );
Note: If you have used new GWRequireListColumns()
to require all columns on all fields, you can override which columns are required for a specific field by passing the $require_columns parameter in a new instance of this class.
Did you know that you can automatically add new List field rows to your forms based on the numbers entered in another field? Gravity Forms Auto List Field auto-syncs the number of rows in a List field with a Number or Quantity field value.
It can also count the number of List field rows and use that value in Gravity Forms List field calculations!
Summary
This can be conveniently used in combination with the Set Number of List Field Rows by Field Value snippet.
One thing – how to use these sub-fields in the system? Let’s say I want to collect up to 3 names + emails in the list How can I use these list-submitted emails to send email notifications to?
In general the use is {Field-label:field-id}, right? So how do I read the values inside the list field, please?
Hi Adam,
You can use this snippet from the Gravity Forms site to add a merge tag modifier, which allows you to output a comma separated string containing the values for a specific column of a list field.
You can then use this merge tag in the notification “to” field. You will need to select “Enter Email”, which will then allow you to free-type, and enter the merge tag.
Best,
Has this been tested with GravityKit’s GravityView plugin? This works fine when submitting a form however we’ve found it doesn’t enforce the required setting when the user chooses to edit the entry in GravityView.
Hey Natalie, I confirmed that this is working in GravityView:
It’s possible something else is conflicting?
Sorry, this just isn’t working for me, but maybe I’m not using it correctly. Do I need to add a “GWRequireListColumns” class to the form or the fields themselves? Or do I need a GravityWiz plugin or account before this works? Just putting this code in my functions.php file has no effect.
sorry, you can remove my comment. i missed a key component. All works as expected. Thanks!
Hi Dan,
Did you also include the full code snippet in your functions.php file? You need both that and
GWRequireListColumns
class.May I suggest you put this “Leave a Reply” section at the top of the comments? And shouldn’t it be named “Leave a Comment”? Reply to what? And your submit button says “Post Comment”. I didn’t think I could post a new comment because I couldn’t imagine having to scroll to the very bottom of what could have been an extremely long list of comments.
Or at least post a link to this section at the top of the comments.
Also, I have checked to receive emails from followup comments but I’ve never received one. There’s nothing in my Spam folder either.
Thanks.
Hi John,
Thanks for the feedback. I’m going to share it with our developers.
I just tested the comment emails, and emails are arriving in my inbox. I suggest reaching out to your email provider about this.
Hi Vinod,
This looks like a question that will require some digging.
If you have an active Gravity Perks License, can you please get in touch with us via our support form with your account email address so we can assist you further.
Best,
Hi there It’s not working in gravity flow steps!!
Hello ! Very nice snippet but the validation works only on the first row. If i add another row i can go to the next step of my form without filling fields of the second row while it’s required.
Do you have an idea of what could be the problem ?
Thank’s a lot for your snippet !
Hi Alexis, I’m not able to recreate this issue on the demo: https://gravitywiz.com/require-all-columns-of-list-field/
If you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
The Demo doesn’t work properly either. The Player’s field only needs the first row of required fields filled. Aside from all columns instances, this only works on the first row for me as well.
Hi John,
You sent in multiple comments with the same issue so we approved one. That said, I’m unable to recreate the issue you’re reporting. I just tested the demo and it works correctly. The required columns work on every row and not just the first row. The player list field has the first, second and fourth column as required so the configuration will be something like so: new GWRequireListColumns( 123, 4, array( 1, 2, 4 ) );
You can get in touch with us via our support form if you have an active Gravity Perks license, so we can take a look at your setup and assist you to set it up.
Best,
I’ve replied multiple times yet none of them show up. Are they being rejected?
The Demo doesn’t work the same way the snippet doesn’t work for anything but all columns. For a specific column(s) it only works for the first row. Any response?
Hi,
We’ve already replied to the comments you sent earlier. You can check it out here.
Best,
I apologize for submitting another duplicate reply. I only see my replies after I submit a reply. Even after I refresh the page I don’t see them so I post another.
Did you fill out the demo as follows? All Coach fields in all rows filled in. It only works if all fields filled in. Only 1st row of multiple Player rows are filled in for fields 1, 2, 4.
If the first Player row is not filled in as described then i correctly get the validation error. But I can fill in that row and leave all the other rows empty and submit successfully.
This mirrors what happens on my site. How do you track multiple rows of data? Do you get/need the length of the list?
It’s easy to validate everything. It seems there’s a bug when there are specific fields in multiple rows.
How can the Demo work differently for different people?
Hi John,
It seems to work on the demo page when testing with the data you provide and when testing locally. You can get in touch with us via our support form if you have an active Gravity Perks license, so we can take a look at your setup and assist you to set it up.
Cheers,
Dario, that’s the point: it should NOT work with the data I provided. Shouldn’t it fail validation if ALL Player rows are not filled in for inputs 1, 2, 4?
What do you mean by “it works”, that it submits successfully with empty Player rows or fails validation with empty Player rows?
Thanks.
Hi John,
We’ve finally been able to recreate the issue with the demo and snippet. I’ll pass this over to our developers to dig into it. We’ll update the comment section when this is resolved.
Best,
The release note at the top of this article says:
“July 14, 2015: Fixed issue where requiring specific columns did not apply to subsequent rows.”
That was 7 years ago. This seems to be the very issue we’re discussing. Was this fix undone by futures fixes or did this fix ever actually work?
Thanks.
Hi John,
Yes, it appears to be the same issue that was fixed but that was in 2015 and over the years, Gravity Forms has evolved. I’m guessing one of the recent Gravity Forms versions may have broken the code. This has been forwarded to our developers to look into. We’ll update the comment and release note to let everyone know when the issue is resolved.
Best,
This issue has been resolved.
Hi,
We tried to implement this code on our website but it only checks the first row. Whenever I add a new row it doesn’t validate it. See issue on: http://tandarts.100test.nl/coming-soon/. The list field is placed on the third page ‘Opmerkingen’. Could you please help us out?
Thanks in advance!
This is very similar to a recent issue reported by one of our Gravity Perks customers with this snippet. Here is our response to him:
Works like magic, but i have two limitations or issues with it.
The code is too long, i prefer something shorter and simple.
The error message highlights all the whole lists, instead of highlighting the affected rows, or colums only. And it can be very tasking for a list of over 250 rows.
Thanks for the feedback, Chris. The code is as long as it needs to be to meet the requirements. As for the highlighting the affected rows/columns, this is feasible but would require some considerable customization as Gravity Forms only provides field-level validation but not row-level. We’ll monitor demand for such an enhancement. 🙂
Hi! I am using this on a website and it had been working fine to my knowledge but the client requested that one column not be required so I used the code to require specific columns, but it doesn’t seem to be working. Users are now able to submit with an entirely blank row. Any ideas? Here is my code:
new GWRequireListColumns(2, 7, array( 1, 2, 3 ) );
and here is an example form:
http://www.scltap.org/event-registration/?e=5336
Thank you!
Joel
Are you able to recreate on the demo?
I see that it is working in the demo and I believe I resolved the issue. I believe the code on the website was an older version of this. I updated that and I believe that fixed it. Thanks David!
Glad to hear it, Joel. ?
how to build a list dynamically that you have both text boxes and drop dow?
colum1 colum2 colum3 input drop down input
We recommend GF Nested Forms for collecting advanced, repeatable data: https://gravitywiz.com/documentation/gravity-forms-nested-forms/
Can you please help sort out this issue? I’m getting an error after submitting my form:
Warning: Invalid argument supplied for foreach() in /home/jcres/public_html/processes/wp-content/plugins/process-functionality-plugin/functionality-plugin.php on line 852
The code on line 852 is:
foreach($values as $value) { if(empty($value)) { $new_validation_error = true; $field[‘failed_validation’] = true; $field[‘validation_message’] = $field[‘errorMessage’] ? $field[‘errorMessage’] : ‘All inputs must be filled out.’; } }
This snippet seems not working after upgrading to Gravity Forms 2.3.1, please check it.
Hi Edi, drop us a line via support and we’ll be happy to dig in.
Hello David, We have a list that has almost 200 rows; is there a way to have it actually highlight the row that is actually failing validation?
We don’t currently have a solution for this one, Jeremy.
Thanks, I tackled from the client side after all. Glad to see you added support for the List Fields.
Hello and thanks I copy this code to functions.php of my theme and use new GWRequireListColumns(); but it doesn’t work. None of the fields are required. please help me
Hi Moshtaghi, the demo is still working so there is mostly likely a configuration issue or conflict on your end. You can start with a theme/plugin conflict test. If that doesn’t turn anything up and you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
Hello,
I am trying this snippet on my website but it seems to break the website. I pasted the code in functions.php and the site goes blank. Appreciate any help.
Hi Brijesh, try this: https://gravitywiz.com/documentation/snippet-troubleshooting/
thank you! it worked in the first try!
Glad to help, Alejandro. :)
Thanks for this code! Seems to trip up when there is more than one list on the form and the required-rules are different for each.
Can you help me make this work?
eg – List 1 needs columns 1, 2 & 3 as mandatory – List 2 needs columns 1, 4 & 5 as mandatory
Thanks!
Hey Joe, can you send me an export of your form and your configuration of the snippet?
Hi there,
I’ve installed the code from the download link to the functions file, and am utilizing the “Require Specific Columns on a Group of Fields”. It’s not working for me. Do I need to comment out the other classes or how can I get this to work?
It’s on the “select product type” field and first checkbox “roller shade”
http://shadesbymatiss.com/advanced-order-form
password is: matiss.
Not to worry this is only a test form. THanks!
Hi James, you’ll need to enable the Required setting for this field in the form editor field settings as well.
thank worked! thank you!
Hi David, This doesn’t seem to work when the Save & Continue Button is used. If you access the form via the Resume link the fields are no longer mandatory.
Thanks
Hi Justin, I’ve just tested this on my end and the requirement is still enforced. It sounds like this might be specific to configuration. I’d try a theme/plugin conflict as a next step.
Hi,
Thank you for providing the code to accomplish this! One issue I’m running into is whenever I add this code and submit a form without all the list columns filled in, I get a “jQuery is not defined” console error, which messes up some jQuery code I have on the form. This console error is not there before submitting the form.
The form validation works perfectly, just curious what could be causing this console error to appear after submitting the form.
Any idea of why this may be happening?
Thanks again!!
Do you have a URL and/or form export I could see with this issue intact?
Hey David,
Here is a link to the form: http://corprisk.staging.wpengine.com/products/nerc-cip-compliance-guide/
If you scroll down, choose the “PDF Version” option then “Click Here to Order”. After that, click the “I have read and agree to the terms and conditions.” and the Checkout button should appear.
If you try to submit the form without filling out all the fields, the Terms and Conditions opens up and I’m getting the “jQuery is not defined”. I’m using another plugin called Collapse-O-Matic (https://wordpress.org/plugins/jquery-collapse-o-matic/) for the Terms and Conditions accordion function.
If the issue is the Collapse-O-Matic plugin, I can figure out a different route, I just wanted to make sure it was nothing with the code I added to require all list fields.
Thanks for looking into this!
Hi Blake, sorry for the long wait. I see the issue and yes this is generated by the snippet + AJAX enabled forms. I’m going to hold off on addressing this for now but if more people are attempting to use this in the same way, I will revisit.
I am having an issue where when I use this snippet along with the “Set Number of List Field Rows by Field Value”.
When I change the quantity field to 0, there is still one list item showing. The problem is that it is showing a validation error because the field is marked as required.
I have uploaded an example of the issue I am having: http://tinypic.com/r/2s6a6tc/9
How can I get this scenario handled? Any help would be appreciated.
Hi Sulayman, I would use conditional logic to hide the field if quantity is less than 1. If the Quantity field is not available for selection in conditional logic, you can add a Number field, enable calculations and set the merge tag for the quantity field as the formula. This will essentially “copy” the value to the Number field and then you can base your conditional logic rule on the Number field.
is it possible to get a sample form that you use in the demo?
Wow, thanks for the code!
But, I have one question: how would you validate the email fields in the demo page?
Again, good work :-)
Hi Eduardo, they actually aren’t validated to ensure they’re actually email addresses. Just that they are not empty.
Fantastic! This has long been a bugbear of Gravity Forms for me. One addition I would like to see is custom validation feedback messages. For example, if I have set only certain columns as required, I would prefer to say customise the feedback rather than ‘All inputs must be filled out.’, as this is not the case in this instance.
Thank you! :)
Good suggestion and duly noted. I will tackle this at some point. :)
Hello David
I recently tried to require certain columns only and it only works on the first row. The ‘require’ rules fail to apply on subsequent rows
I looked through the comments and it seemed this had been addressed.
however here’s the address http://gravityform.banorg.tk
Below is the code at the end of snippet: // require specific field columns on a specific form new GWRequireListColumns( 1, 1, array( 1, 3, 4, 5 ) );
I appreciate your help on this
Regards
Ore B
Thanks for letting me know, Ore. I’ve just updated the snippet to fix this error. You can get grab the latest code above.
Thanks David,
It works fine now.
How do I translate the validation message?
I am using WPML, but as far as I see in the code above, the validation message string won’t get recognized by WPML’s string translation module?
Thanks.
Hm, not sure on this one Mikkel. It might be possible to add this to WPML but I don’t believe WPML will automatically pick it up. They’re support would have a much better answer on this. If you could do me a favor and let me know, I’d be interested in any solution you come up with.
I will try to reach out to WPML team and see if they have a quick solution.
In the meantime I noticed something strange happening when I have the code in my functions.php. It breaks the grid view of the media manager. I have no idea why or how, but no images are being loaded in the media manager when the library_mode is set to grid. This also means I cannot add images to my posts/pages as the insert media window apparently uses the grid view.
Removing your code from functions.php solves the problem.
Could you investigate?
Hi Mikkel, this code is running on my local server and on the demo server. I checked and I’m able to access the media manager without issue. I think there may be some other issue or conflict causing this on your end.
I would try two things:
Hi,
Thank you for this great snippet. It seems to do the trick just fine, but only if all columns of the list have regular single line text inputs.
In my case, I have one of the columns as a dropdown select menu (you can override using gform_column_input filter), and it validates OK even if nothing is selected in the dropdown.
Any ideas how to fix this?
Thanks again.
Hi Andrei, I’ve added a drop down input to the List field on the demo and it appears to limit correctly. Could you make sure you’re getting the code via the “Download Code” button? I’ve also cleared the cache on this post in case there was any out-of-date code being displayed.
How can I use this in combination with the Set number of list field rows by value field snippet, so that all columns and all rows are required?
Does it not work by just including both snippets? I wouldn’t think you’d need to do anything fancy.
I thought it would work as well. I looked on the net and it seems that input fields that are added dynamically after the page load are not added to the $_POST variable. Otherwise the 2nd, 3rd, etc. rows have the same name attribute as the original first row input field. One solution I found was that I would need to add my own submit handler that uses jquery $.post to send to the php script manually.
But I’m not sure exactly about how to go about doing that as my knowledge of gravity forms internals is somewhat limited.
I tried on the Set List Rows by Field Value demo and this snippet does work with that: https://demos.gravitywiz.com/set-number-of-list-field-rows-by-field-value/
Yes, you were right. I had modified your code somewhat and it turned out that modification was causing the problem. It now works as intended, which is great. Thank you!
Glad that worked for you. :)
Hi, it’s possible to add this to a custom field with field type set to List ? thanks
Hi Wilfrid, I’ve just updated the snippet to support these types of fields as well.
Thank you David
Hi, Using the snippet above along with snippet “Set Number of List Field Rows by Field Value”, I have:
The snippet for “Set Number of List Field Rows by Field Value” seems to be working perfectly.
However, the snippet to validate only specific columns seems to have an issue whereby only the first row seems to be being validated; basically, testing on row 1 is perfect, but for rows 2, 3, 4 etc., there does not seem to be any validation and I am able to go on to the next page of the form.
In my functions file, I’ve called the snippet above as: new GWRequireListColumns( 13, 246, array( 1, 2 ) );
Unfortunately, my site is currently in ‘coming soon mode’, but I would be happy to PM you a username.
Many thanks in advance for any help you can offer! Angela
Found a small issue. When using a Custom Field that acts as a List, I needed to change the is_applicable_field function to look at the InputType and not the Type. Here is my new function.
Hi David, thanks for a great snippet! I have a problem with using the class for making a single column required. It seems that the form validates by just entering data in this column for the first row. Is it possible to make only the first column required on each row of the list?
I am sure that the error is on me but I just can’t figure this out. I have copy/pasted the class into my functions.php. I have not change anything (I do plan on limiting to specific fields but just for testing left it alone) and I am not seeing any effect at all. I have tried adding the call to the class (new GWRequireListColumns();) within the page template that the form is displayed on. I am not sure what I am missing here. Any suggestion are greatly appreciated.
Nothing like a check box to halt all development. My apologies my guys said they tested that way as well. Your snippet is great.
David, Very cool! I’m looking for almost this exaclty. I think I can modify it to do what I need, which is to specifically designate particular columns as required or optional. I’m thinking if I just add a parameter to the class construct (array for the indexes of the required colums) that when it comes time to loop through the values, I can instead check only the indexes passed (if any were). That seems like it should work aye? Am I missing anything? I do that sometimes :P
Hey Eddy, if I were doing this, I’d do exactly that. If you end up modifying this snippet to handle it, I’ve love to update the code here as well. :)
David, Ok, I modified your awesome class to accept a 3rd parameter in the construct. Just like the form field parameter, a single value or array will do. If this parameter is detected instead of looping through all the field values, require_list_columns() loops through only the values who’s indexes are in the array. I’ve tested it pretty thoroughly and the logic seems sound. If any of my syntax is goofy or inefficient I’m sure you can whip it into shape. Here it is.. and thanks again for the huge head start on this!
http://pastie.org/8421008
Awesome stuff, Eddy! I’ve modified the code just a bit to support being able to apply the GWRequireListColumns class globally and then overriding with required columns for specific fields. You can see the latest here:
https://gist.github.com/spivurno/3718075
Do you have a website or twitter I can give a shoutout too?
EDIT: Also, I updated it to allow users to enter “1” for column “0”, “2” for “1” etc as I thought this would be less confusing for non-developer users.
Not working with woothemes
Hi ahmet, could you provide a link to a URL where this is not working?
I have tried this code and i found one thing missing, this code gives error when there is list field on second page break or third page break of the form.
For example, if you have multi page form and your list field is on first page of the multi page form then it works fine, but if list field is on second page of multi page form then it starts generating error of values not filled out.
Is there any solution to that?
thanks
Hi Waqas, I’ve updated the snippet to fix this issue. Grab the latest code and let me know if you run into anything else. :)
Hi David, Thanks for quick help. yes really it solved the issue. Now the snippet is working fine on multi page form.
thanks
Does it work with latest version of gravity form as it is not working, i tried it.
thanks
Hi Shahzad, one thing that might be confusing is that this snippet simply enhances the “Required” field setting (http://grab.by/icRw) for list fields to require all columns rather than just the first input. You will still need to check the “Required” option to require this field after installing the snippet.
YOU ARE MY HERO!!!
Works PERFECT!!
Thanks so much!!!
Hi! That’s nice!!! I found just one problem….. In my form I have a Conditional to show the List Field. Even if the List FIeld do not show in the forms is asking to input something. Could you help me please?
Hi Raoni, I’ve updated the snippet to include support for conditional logic. List fields hidden by conditional logic will no longer be validated by this functionality. :)
Hi, that’s nice!
How could I target only a specific List Fields in a specific Form?? Any idea?
Thanks!!
Hi Raoni, I’ve just updated the snippet to include support for this. The code changed pretty significantly so I would delete what you copied before and grab the latest snippet above. :)