Support

Account

Home Forums Feedback Validation Code Example Turns non-required field into required

Helping

Validation Code Example Turns non-required field into required

    • Shines

    • June 2, 2017 at 5:01 am

    Appreciate the validator example code. However, as it is currently written , it means a non-required field will become essentially required, as an empty field won’t pass validation.

    Simple enough fix (add after the !valid check), but didn’t notice it was an issue until I passed it off to someone else to enter content:

    if( !$value ) {
    	return $valid;
    }

    Alternatively, if you modified the core code (granted I didn’t poke through to see if this would work as it’s currently written), you could have the validation not check a non-required and empty field.

    Thanks!
    Sarah

  • You could also do

    
    if (!$valid || !$value) {
      return $valid;
    }
    

    Most of the examples that the developer has given are supposed to be pretty basic, only including the necessary parts. There are many cases where if you use the code as given, for example many of the pre_get_post filter examples, that you’ll likely mess up other queries that need to be done because no checks are given to make sure the correct queries are altered.

    As far as altering the code, no validation filter knows what the other filters are validating. ACF just does apply_filters(...... There isn’t even a guarantee that the internal ACF required field filter will actually run before your custom filter runs. It just runs through them all until one of them fails.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.