Skip to content

Commit

Permalink
Fixed cannot save posts when select_tree has required attribute.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rilwis committed Dec 19, 2018
1 parent 6ec525a commit dbf1fa0
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions 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 );
Expand Down

0 comments on commit dbf1fa0

Please sign in to comment.