From dbf1fa0e96be73b0c6b68e31a2d64ba72455fef3 Mon Sep 17 00:00:00 2001 From: Anh Tran Date: Wed, 19 Dec 2018 16:09:35 +0700 Subject: [PATCH] Fixed cannot save posts when select_tree has required attribute. Link: https://metabox.io/support/topic/error-in-select-tree-taxonomy/ The taxonomy has some term nested in 3 levels, but it also has other nested terms in only 2 levels. Then the validation of the abligatory field fails because apparently it is waiting for a third level, even though it should not be like that. This fix set/unset the 'required' attribute correspondingly when select is visible/hidden (similar to Conditional Logic). So doesn't affect the valiation when saving. --- js/select-tree.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/js/select-tree.js b/js/select-tree.js index ce6879270..4b67950ff 100644 --- a/js/select-tree.js +++ b/js/select-tree.js @@ -1,19 +1,38 @@ jQuery( function ( $ ) { 'use strict'; + function setInitialRequiredProp() { + var $this = $( this ), + required = $this.prop( 'required' ); + + if ( required ) { + $this.data( 'initial-required', required ); + } + } + + function unsetRequiredProp() { + $( this ).prop( 'required', false ); + } + + function setRequiredProp() { + var $this = $( this ); + + if ( $this.data( 'initial-required' ) ) { + $this.prop( 'required', true ); + } + } + function update() { var $this = $( this ), val = $this.val(), $selected = $this.siblings( "[data-parent-id='" + val + "']" ), - $notSelected = $this.parent().find( '.rwmb-select-tree' ).not( $selected ); + $notSelected = $this.siblings().not( $selected ); - $selected.removeClass( 'hidden' ); - $notSelected - .addClass( 'hidden' ) - .find( 'select' ) - .prop( 'selectedIndex', 0 ); + $selected.removeClass( 'hidden' ).find( 'select' ).each( setRequiredProp ); + $notSelected.addClass( 'hidden' ).find( 'select' ).each( unsetRequiredProp ).prop( 'selectedIndex', 0 ); } + $( '.rwmb-select-tree select' ).each( setInitialRequiredProp ); $( '.rwmb-input' ) .on( 'change', '.rwmb-select-tree select', update ) .on( 'clone', '.rwmb-select-tree select', update );