Support

Account

Home Forums Bug Reports acf.registerConditionForFieldType() not working correctly?

Helping

acf.registerConditionForFieldType() not working correctly?

  • I have a custom field I would like to use in a conditional statement of another and have followed the docs by using the acf.registerConditionForFieldType() function to apply conditionals to the custom field. I use the following code:

    (function($){
    	var job_type_field = acf.Field.extend({ type: 'job_type' });
    	acf.registerFieldType(job_type_field);
    	acf.registerConditionForFieldType('equalTo', 'job_type');
    	acf.registerConditionForFieldType('notEqualTo', 'job_type');
    })(jQuery);

    It shows up as expected when setting the conditionals on the “Edit Field Group” page. However, when editing a post, it does not work as expected. Nothing happens when changing the custom field.

    After digging into the code, it appears that acf.registerConditionForFieldType() isn’t correctly adding the custom field to the fieldTypes array on the Condition object.

    To test things, I manually added the “job_type” field to the “EqualTo.fieldTypes” array in the source code (starting on line 9337 of assets/js/acf-input.js):

    var EqualTo = acf.Condition.extend({
    	type: 'equalTo',
    	operator: '==',
    	label: __('Value is equal to'),
    	fieldTypes: [ 'text', 'textarea', 'number', 'range', 'email', 'url', 'password', 'job_type' ],
    	match: function( rule, field ){
    		if( $.isNumeric(rule.value) ) {
    			return isEqualToNumber( rule.value, field.val() );
    		} else {
    			return isEqualTo( rule.value, field.val() );
    		}
    	},
    	choices: function( fieldObject ){
    		return '<input type="text" />';
    	}
    });

    Digging further, it looks like a few fields are added this way as well (gallery, repeater, etc…) in the “pro” folder. Non of these fields seem to work with conditionals either.

  • I’ll leave this up in case anyone else runs into it, but my issue was loading my Javascript in the footer and not the header…

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘acf.registerConditionForFieldType() not working correctly?’ is closed to new replies.