Custom Fields
Re-Envisioned
Support Meta Box Conditional Logic TypeError: logics is undefined

  • Creator
    Topic
  • #18497
    Resolved Austin Passy
    Participant

    TypeError: logics is undefinedconditional-logic.js:221:18

    You should check for the logics object before sub checks, like:

    var relation = typeof logics !== 'undefined' && typeof logics.relation !== 'undefined' ? logics.relation.toLowerCase() : 'and',
                success = relation === 'and';
Viewing 10 replies - 11 through 20 (of 21 total)
  • Author
    Replies
  • #19317

    Hi all,

    same here. The code used to work fine, a few weeks ago.
    Quite normal setup, but in some cases this ‘logics is undefined’ error appears, making it impossible to use the controls.

    https://pastebin.com/SdksUKCM

    #19318

    P.S. this is the error thrown:

    conditional-logic.js?ver=1.5:221 Uncaught TypeError: Cannot read property 'relation' of undefined
        at isLogicCorrect (conditional-logic.js?ver=1.5:221)
        at HTMLScriptElement.<anonymous> (conditional-logic.js?ver=1.5:182)
        at Function.each (jquery.js?ver=1.12.4-wp:2)
        at jQuery.fn.init.each (jquery.js?ver=1.12.4-wp:2)
        at runConditionalLogic (conditional-logic.js?ver=1.5:177)
        at conditional-logic.js?ver=1.5:595
        at dispatch (jquery.js?ver=1.12.4-wp:3)
        at r.handle (jquery.js?ver=1.12.4-wp:3)
    #19319
    Austin Passy
    Participant

    After spending many hours testing and checking all conditions the only logical issue comes down to the updated conditional-logic.js. If I go in an edit the following two functions, everything works again: isLogicCorrect() & getWatchedElements()

    In isLogicCorrect() check logics before continuing since :

    
    if (typeof logics === 'undefined') {
            return;
    }
    

    In getWatchedElements() wrap logic.when.forEach:

    
    if (typeof logic !== 'undefined') {
        logic.when.forEach(addWatchedElement, this);
    }
    

    We have a lot of conditions there are simple and do not have relation definitions or when conditions (as nothing in the docs said those are required attributes (I see the code adds those if missing).

    #19330
    Anh Tran
    Keymaster

    Hi Brian, Aart and Austin,

    I’ve debugged with your code and still can’t reproduce the error. Although I can add code as Austin suggested, but am not sure the root cause. I also tried Aart’s code above, and Austin case with taxonomy and still see nothing.

    Can you guys provide me a full code that shows the error?

    #19335

    Dear Anh,

    I managed to reproduce the error by entering this code in a WYSIWYG field with a ‘hidden’ condition:
    <header id="entry-header" class="entry__header yr-entry-header" data-rapid-sec="{&quot;entry-header&quot;:&quot;entry-header&quot;}"></header>

    Somehow this makes the value of the data-conditions attribute invalid JSON.

    (It turned out one of my co-workers pasted this messy HTML from another site in a WYSIWYG-field, this is part of the HTML)

    Hope this helps..?

    Best,

    AJ

    #19343
    Austin Passy
    Participant

    Sorry, I am unable to paste my code as it’s 20 post types, and thousands of custom fields.

    But I was able to spot incorrect JSON in the data-conditions attribute which may help. It would seem that double quoted aren’t getting encoded properly.

    When I changed the get_conditional_html (and reverted the JS changes) I no longer see the logics errors.

    
    esc_attr( htmlspecialchars( wp_json_encode( $conditions ), ENT_QUOTES, 'UTF-8' ) )
      
    

    This was the json condition (part of it that didn’t get encoded/escaped properly):

    
    "desc": "Alphanumeric, no special characters except for " - ".",
    

    Hope this helps.

    #19357
    Anh Tran
    Keymaster

    Thanks a lot for your feedback! Looks like the json encode is the problem. I’ll check and fix that.

    #19360
    Anh Tran
    Keymaster

    Hey guys, can you help me to test the fix before releasing a new version? Please try changing parse_conditions function to:

    private function parse_conditions( $conditions ) {
        $output = [];
        if ( ! empty( $conditions['visible'] ) ) {
            $output['visible'] = $this->parse_condition( $conditions['visible'] );
        }
        if ( ! empty( $conditions['hidden'] ) ) {
            $output['hidden'] = $this->parse_condition( $conditions['hidden'] );
        }
        return $output;
    }

    The previous code does JSON-encode all field settings, which contains something like field description and value, which might break the JSON. The new code only encodes the conditions, which eliminate this issue and might improve the performance as well.

    #19377
    Austin Passy
    Participant

    Thanks, I will test this today and report back.

    #19380
    Austin Passy
    Participant

    Validated the updated function is working without issues.

Viewing 10 replies - 11 through 20 (of 21 total)
  • You must be logged in to reply to this topic.