@@ -2306,6 +2306,88 @@ function set_field_limits($field_limits)
2306
2306
'max ' => 6
2307
2307
)
2308
2308
));
2309
+ /**
2310
+ * Gravity Wiz // Require Minimum Character Limit for Gravity Forms
2311
+ *
2312
+ * Adds support for requiring a minimum number of characters for text-based Gravity Form fields.
2313
+ *
2314
+ * @version 1.0
2315
+ * @author David Smith <david@gravitywiz.com>
2316
+ * @license GPL-2.0+
2317
+ * @link http://gravitywiz.com/...
2318
+ * @copyright 2013 Gravity Wiz
2319
+ */
2320
+ class GW_Minimum_Characters {
2321
+
2322
+ public function __construct ( $ args = array () ) {
2323
+
2324
+ // make sure we're running the required minimum version of Gravity Forms
2325
+ if ( ! property_exists ( 'GFCommon ' , 'version ' ) || ! version_compare ( GFCommon::$ version , '1.7 ' , '>= ' ) )
2326
+ return ;
2327
+
2328
+ // set our default arguments, parse against the provided arguments, and store for use throughout the class
2329
+ $ this ->_args = wp_parse_args ( $ args , array (
2330
+ 'form_id ' => false ,
2331
+ 'field_id ' => false ,
2332
+ 'min_chars ' => 0 ,
2333
+ 'max_chars ' => false ,
2334
+ 'validation_message ' => false ,
2335
+ 'min_validation_message ' => __ ( 'Please enter at least %s characters. ' ),
2336
+ 'max_validation_message ' => __ ( 'You may only enter %s characters. ' )
2337
+ ) );
2338
+
2339
+ extract ( $ this ->_args );
2340
+
2341
+ if ( ! $ form_id || ! $ field_id || ! $ min_chars )
2342
+ return ;
2343
+
2344
+ // time for hooks
2345
+ add_filter ( "gform_field_validation_ {$ form_id }_ {$ field_id }" , array ( $ this , 'validate_character_count ' ), 10 , 4 );
2346
+
2347
+ }
2348
+
2349
+ public function validate_character_count ( $ result , $ value , $ form , $ field ) {
2350
+
2351
+ $ char_count = strlen ( $ value );
2352
+ $ is_min_reached = $ this ->_args ['min_chars ' ] !== false && $ char_count >= $ this ->_args ['min_chars ' ];
2353
+ $ is_max_exceeded = $ this ->_args ['max_chars ' ] !== false && $ char_count > $ this ->_args ['max_chars ' ];
2354
+
2355
+ if ( ! $ is_min_reached ) {
2356
+
2357
+ $ message = $ this ->_args ['validation_message ' ];
2358
+ if ( ! $ message )
2359
+ $ message = $ this ->_args ['min_validation_message ' ];
2360
+
2361
+ $ result ['is_valid ' ] = false ;
2362
+ $ result ['message ' ] = sprintf ( $ message , $ this ->_args ['min_chars ' ] );
2363
+
2364
+ } else if ( $ is_max_exceeded ) {
2365
+
2366
+ $ message = $ this ->_args ['max_validation_message ' ];
2367
+ if ( ! $ message )
2368
+ $ message = $ this ->_args ['validation_message ' ];
2369
+
2370
+ $ result ['is_valid ' ] = false ;
2371
+ $ result ['message ' ] = sprintf ( $ message , $ this ->_args ['max_chars ' ] );
2372
+
2373
+ }
2374
+
2375
+ return $ result ;
2376
+ }
2377
+
2378
+ }
2379
+ new GW_Minimum_Characters ( array (
2380
+ 'form_id ' => 49 ,
2381
+ 'field_id ' => 2 ,
2382
+ 'min_chars ' => 240 ,
2383
+ 'min_validation_message ' => __ ( 'Oops! You need to enter at least %s characters. ' )
2384
+ ) );
2385
+ new GW_Minimum_Characters ( array (
2386
+ 'form_id ' => 49 ,
2387
+ 'field_id ' => 3 ,
2388
+ 'min_chars ' => 240 ,
2389
+ 'min_validation_message ' => __ ( 'Oops! You need to enter at least %s characters. ' )
2390
+ ) );
2309
2391
add_filter ('gform_entries_field_value ' , 'ccgn_gf_display_name_instead_login ' , 10 , 4 );
2310
2392
function ccgn_gf_display_name_instead_login ($ value , $ form_id , $ field_id , $ entry )
2311
2393
{
0 commit comments