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.
October 22, 2013: Updated to support a $require_columns parameter. Allows requiring specific columns rather than all columns on a list field.
September 13th, 2012: Updated to use a class format to allow you to apply this functionality to a specific form and/or specific fields.
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.
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.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 47+ essential add-ons for Gravity Forms with support you can count on.
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. :)