Home Forums Backend Issues (wp-admin) Live validate and populate fields. Not before/after post/save. Reply To: Live validate and populate fields. Not before/after post/save.

  • Now, that’s a good starting point and learning material. That bit of code explained a lot to me.

    Tweaked it a bit to get it working with acf 5. Also added an if because it threw an error when var date was not populated. And added a delete end date if start date is not populated.

    var start_date_key = 'field_5bc62ed0bbcd1'; // the field key of the start date
    var end_date_key = 'field_5bc62eeabbcd2'; // the field key of the end date
    if ( acf.get('acf_version') != 'undefined') {
        // add an action for all datepicker fields
        acf.addAction('date_time_picker_init', function( $input, args, field ){
            // get the field key for this field
            var key = $input.closest('.acf-field').data('key');
            // see if it's the start date field
            if (key == start_date_key) {
                // add action to start date field datepicker
                $input.datepicker().on('input change select', function(e) {
                    // get the selected date
                    var date = jQuery(this).datepicker('getDate');
                    // check if date has a value
                    if ( date != null ) {
                        // add 5 days to date
                        date.setDate(date.getDate() + 5);
                        // set end date
                        jQuery('[data-key="' + end_date_key + '"] input.hasDatepicker').datepicker('setDate', date);
                    // if start date field datepicker has no value, delete end date field datepicker
                    else if ( date == null ) {
                        jQuery('[data-key="' + end_date_key + '"] input.hasDatepicker').datepicker('setDate', null);

    Later I’ll tweak some things. For example, I would just like to populate end date IF it has no value. And my actual date fields are on a nested repeater field 🙂 Hmm plus how to throw an error warning when the value is wrong. Anyway, things for tomorrow.

    Many thanks @hube2 ! This was very informative.