Skip to content

Commit

Permalink
Merge pull request #3 from cliffordp/fix-non-required-fields
Browse files Browse the repository at this point in the history
v2.0.1
  • Loading branch information
cliffordp authored Oct 27, 2018
2 parents b7ef92d + 8d9f6a9 commit d3b27db
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# GF_GW_Req_Char_Length

Require a minimum and/or maximum character length for specific Gravity Forms fields, displaying an informative Gravity Form validation error to the user for disallowed entries.

## Example Usage of this Class

***See https://gist.github.com/cliffordp/551eb4f67b8db8e19d3d59a0f2b7a6f9 for several examples.***

### Example Composer Usage

Here's an example of what you might want to put in your own project's `composer.php`:

```
{
"require": {
"gf-gw-req-char-length/gf-gw-req-char-length": ">=2.0.1 <3.0"
},
"minimum-stability": "stable"
}
```

In plain English:
* use version 2.0.1 or greater, as long as it is below version 3.0
* only use versions that are official Releases in the project's repo

*Pro tip: Installing this class via Composer allows you to do autoloading.*

Visit https://getcomposer.org/ to learn more about Composer. It's great!

### Credits

Of course, a big hat tip to Gravity Wiz:
* This repo began as a forked from the May 30, 2014, version of https://gist.github.com/spivurno/8220561
* Its accompanying article: https://gravitywiz.com/require-minimum-character-limit-gravity-forms/

## Changelog

#### Version 2.0.1: October 26, 2018

* Fix to avoid applying character length minimums to non-required fields that have no input.

#### Version 2.0.0: October 26, 2018

* Pretty much fully rewritten.
* Now requires Gravity Forms version 2.3+ (an arbitrarily-chosen recent version where `GFForms::$version` is used instead of the now-deprecated `GFCommon::$version`). Current GF version at time of this release is 2.3.6.
* Now requires PHP 5.4+ (uses array short syntax).
* Changed license from GPLv2+ to GPLv3+.
* Renamed class to better describe actual functionality, as it can be used for minimum and/or maximum.
* `'field_id'` argument now supports array type fields, such as Name and Address fields. (e.g. require Address Line 1 to be 5-30 characters long)
* `'field_id'` argument now supports passing an array to apply the same rules and messaging to multiple fields at once. (e.g. same length and messaging to First Name and Last Name)
* Added `composer.php`
* Added multiple new examples to demonstrate available functionality, but removed them from this file in case this class is added to your project via Composer. **See them at https://gist.github.com/cliffordp/551eb4f67b8db8e19d3d59a0f2b7a6f9**

#### Version 1.0.0: May 30, 2014

* Started by setting the May 30, 2014, version of https://gist.github.com/spivurno/8220561 as Version 1.0.0.
35 changes: 11 additions & 24 deletions gf-gw-req-char-length.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,13 @@
* See $this->set_defaults() for allowed/required input values. Optionally add your own text domain to messages.
* See Changelog for required Gravity Forms and PHP versions.
*
* @version 2.0.0
* @version 2.0.1
* @author TourKick LLC (Clifford Paulick)
* @license GPL version 3 or any later version
* @link https://github.com/cliffordp/gf-gw-req-char-length This new/forked version of this class, by Clifford.
* @link https://gist.github.com/cliffordp/551eb4f67b8db8e19d3d59a0f2b7a6f9 See this for multiple code usage examples.
* @link https://gravitywiz.com/require-minimum-character-limit-gravity-forms/ The accompanying Gravity Wiz article.
* @link https://github.com/cliffordp/gf-gw-req-char-length This class' repository. See its README.md for
* changelog, credits, links, and instructions.
*/
class GF_GW_Req_Char_Length {
/**
* Changelog:
*
* Version 2.0.0: October 26, 2018
* - Pretty much fully rewritten from https://gist.github.com/spivurno/8220561 (considered as Version 1.0.0 from
* May 30, 2014)
* - Now requires Gravity Forms version 2.3+ (an arbitrarily-chosen recent version where GFForms::$version is
* used instead of the now-deprecated GFCommon::$version). Current GF version at time of this release is 2.3.6.
* - Now requires PHP 5.4+ (uses array short syntax).
* - Changed license from GPLv2+ to GPLv3+.
* - Renamed class to better describe actual functionality, as it can be used for minimum and/or maximum.
* - 'field_id' argument now supports array type fields, such as Name and Address fields.
* (e.g. require Address Line 1 to be 5-30 characters long)
* - 'field_id' argument now supports passing an array to apply the same rules and messaging to multiple fields
* at once. (e.g. same length and messaging to First Name and Last Name)
* - Added composer.php
* - Added multiple new examples to demonstrate available functionality, but removed them from this file in case
* this class is added to your project via Composer.
*/

/**
* The array of default values, set at runtime.
*
Expand Down Expand Up @@ -287,6 +266,14 @@ public function validate_character_count( $result, $value, $form, $field ) {
$is_min_reached = false;
}

// if field is not required and value is empty, do not trigger validation failure
if (
empty( $field['isRequired'] )
&& empty( $char_count )
) {
return $result;
}

if ( - 1 === $this->args['max_chars'] ) {
$is_max_exceeded = false;
} else {
Expand Down

0 comments on commit d3b27db

Please sign in to comment.