Support

Account

Home Forums General Issues Logic between number and relationship fields Reply To: Logic between number and relationship fields

  • Hum, I did try to change the value of the data-max attribute of the field on the fly, but it does not modify the max property in the instance of the ACF field itself. My javascript code looks as follows:

     
    jQuery(document).ready(function($) {
      if (typeof acf === 'undefined') { return; }
    
      var featuredContentExtension = acf.ajax.extend({
        events: {
          'change [data-key="field_5798cf2592b92"]': '_state_change'
        },
    
        _state_change: function(e) {
          var select_field = $('#acf-field_5798ced492b91');
          var number_field = $('#acf-field_5798cf2592b92');
    
          var current_post_number = select_field.attr('data-max');
          var selected_post_number = number_field.val();
    
          select_field.attr('data-max', "[\"" + selected_post_number + "\"]");
        }
      });
    });
    

    I can see that the data-max attribute is properly updated, but the validation is still triggered based on the previous number selected when trying to select additional posts.

    While looking at the implementation in assets/js/acf-input.js, I noticed the following code:

    
    add_item: function( e ){
      // max posts
      if( this.o.max > 0 ) {
        if( this.$values.find('.acf-rel-item').length >= this.o.max ) {
           alert( acf._e('relationship', 'max').replace('{max}', this.o.max) );
    	return;
        }
      }
      // rest of the method...
    

    So, is there an easy way either to override or to hook into the add_item method, or to modify the value of this.o.max ?

    Thanks in advance,