htmlburger / carbon-fields

WordPress Custom Fields Library ✨
https://carbonfields.net/
Other
1.36k stars 245 forks source link

Javascript Hooks documentation needs updating #1089

Open thegreentimtam opened 2 years ago

thegreentimtam commented 2 years ago

I'm trying to do additional validation on my fields using Javascript.

While I have had luck following @jorostoyanov 's comments on Issue #979 using the undocumented carbon-fields.init hook, using the same method for the documented hooks is not working.

(function () {
    const { addAction } = window.cf.hooks;

    addAction('carbon-fields.init', 'carbon-fields/metaboxes', () => {
        console.log('carbon fields metaboxes'); // This works
    });

    addAction( 'carbon-fields.select.validate', 'carbon-fields/validate', () => {
        console.log('validating'); // This does not work
    });

    addAction( 'carbon-fields.field-edit', 'carbon-fields/edit', () => {
        console.log('editing'); // This does not work
    });
})();

Version

Expected Behavior

Events to trigger when validating select fields and editing fields.

Actual Behavior

Nothing happens.

Steps to Reproduce the Problem

  1. Enqueue a JS file
  2. Copy paste the above JS code inside the file and save
  3. Load a page that is using Carbon Fields
dylanhut42 commented 2 years ago

Can we get a comment on this? Will there be updated documentation to reflect how to use hooks in 3.x.x?

VictorPennington commented 1 year ago

Hello there,

Amazing library... but I'm a bit stuck now having the same issue over here, any progress in this?

Best,

gnikolopoulos commented 1 year ago

For anyone looking into validation, I managed to make it work using the following method

$(function() {
    const { addFilter } = window.cf.hooks;

    addFilter('carbon-fields.text.validate', 'some_namespace', (field, value) => {
        console.log(field);
        console.log(value);
    });
});

The abode function should return a string with the validation error or null if there is no validation errors.

opicron commented 3 weeks ago

@thegreentimtam no matter what I try, I cannot make this work:

addAction('carbon-fields.init', 'carbon-fields/metaboxes', () => {
    console.log('carbon fields metaboxes'); // This works
});

Are you sure your code is working?

EDIT: by adding the code to the footer finally made it work.