Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Required as "any" or "all" #53

Open
dovy opened this issue Aug 5, 2019 · 0 comments
Open

Required as "any" or "all" #53

dovy opened this issue Aug 5, 2019 · 0 comments

Comments

@dovy
Copy link
Contributor

dovy commented Aug 5, 2019

Suggestion by @chrishoward on slack.

I have writing a function to handle it. I’m sharing it in case it’s useful to others. It still depends on knowing all possible values, but they’re saved once in an array rather than being coded at each call. So now only the OR vals are coded at each call.

     function redux_required( $compare_field = '', $all_values = array(), $exclude_values = array(), $comparison = '!=', $inc_empty = TRUE ) {
      /*
       * When $comparison is != this effectively works as an OR for the $exclude_values
       */
      if ( empty( $compare_field ) || empty( $all_values ) ) {
        $return = array();
      } else {
        if ( $inc_empty ) {
          $return = array( array( $compare_field, $comparison, '' ) );
          $i      = 1;
        } else {
          $return = array();
          $i      = 0;
        }
        foreach ( $all_values as $k => $v ) {
          if ( ! in_array( $k, $exclude_values ) ) {
            $return[ $i ++ ] = array( $compare_field, $comparison, $k );
          }
        }
      }
      return $return;
    }

And the call looks something like this…

 'required' => redux_required( 'myfield', $field_types, array( 'link', 'email' ), '!=' )

In Redux, I’d suggest keeping it simple. Just adding an ANY (i.e. OR) parameter.

'required' => array(
    array('layout','equals','1'),
    array('parent','!=','Testing'),
    true
)

Then you just check if one of the values in the ‘required’ array is boolean true, if it is change your lookup logic to seek any of the values being matched.

ok @dovy. Looked at the code (v3). I think it’s pretty easy.
Where you’ve got
$return = $this->compareValueDependencies(...
You just need to track and pass the existing state and whether it’s an “ANY” case or “ALL” case. Thus you could do something like…

if ($any_all=='ANY') {
    $return = $current_state || $this->compareValueDependencies(...;
} else {
   $return = $current_state  && $this->compareValueDependencies(...; // Just did it this way for consistency
}

More details hopefully to be provided by @chrishoward.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants