htmlburger / carbon-fields

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

Js Hook: apiLoaded not triggered #691

Open enky00 opened 5 years ago

enky00 commented 5 years ago

Version

Expected Behavior

JS Hook gets triggered

Actual Behavior

JS Hook not triggering

Steps to Reproduce the Problem

  1. Load the admin js with admin_enqueue_scripts
  2. Add the Javascript in the loaded admin.js
  3. navigate through the backend, hooks not triggered

Comments

I added a admin.js and added carbonFields.apiLoaded hook. But this is not triggered.

` function add_cf_hooks_js($hook) {

// jQuery wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', array(), NULL, true); wp_localize_script('jquery', 'themeData', $dataToFrontend); wp_enqueue_script('jquery');

wp_register_script('editjs', get_template_directory_uri() . '/admin/admin.js', array(), NULL, true); wp_enqueue_script('editjs'); } add_action( 'admin_enqueue_scripts', 'add_cf_hooks_js' ); `

(function ($) { "use strict"; $(document).on('carbonFields.apiLoaded', function(e, api) { // Hook to all field changes and log the changed field's name and new value console.log('fooooo!'); // Is triggered. }); })(jQuery);

alessandrotesoro commented 5 years ago

Version 3.0 of CF doesn't only bring support for gutenberg blocks, they've made many more changes to the api and they aren't listed anywhere. The old js api doesn't work anymore because it doesn't exist anymore in v3.0, I've only just found out it myself because my custom validation rules are no longer working after I updated to v3.0

The docs has been partially updated but there's no examples https://docs.carbonfields.net/#/advanced-topics/javascript-hooks

kaleidoscopique commented 4 years ago

Hey!

Anyone found a way to make it work with v3.0 ? :)

Thanks a lot!

red-made commented 3 years ago

Hi, I've found a workaround:

1 register an id on your container:

Container::make('theme_options', 'ID_CONTAINER',  __('Title'))
        ->set_icon('dashicons-dashboard')

2 enable validation if your container is loaded

jQuery( document ).ready(function() {
  if (jQuery('#carbon_fields_container_ID_CONTAINER').length > 0){
    add_specific_validations();
  }
});

3 Use ->set_classes('slugify') on your filed to better target the validation function

4 Validate in real time or on change events

function add_specific_validations() {
  jQuery(document).on("keyup", "#carbon_fields_container_ID_CONTAINER .MYCLASS .cf-text__input", function (e) {
    clean_text = cleanYourValue(e.target.value);
    jQuery(this).val(clean_text);
  });
}

That's all, obviously not safe as a server-side validation but good enough if you want somethings simple

zahardev commented 3 years ago

So it looks like the validation API is still not working?

rmpel commented 3 years ago

carbonFields.apiLoaded is never triggered, yet it is still listed in the 3.0 documentation. So either the software is broken, or the documentation is wrong.

HtmlBurger; please fix!

axshae commented 3 years ago

Hey!

Any update on this?

Thanks

nilsringersma commented 3 years ago

Hi HtmlBurger,

Do you guys have an update about this?

Thanks!

mrdarrengriffin commented 3 years ago

Bump. This is still broken if I am following the v3 docs

siaeb commented 1 year ago

Hi HtmlBurger,

Do you guys have an update about this?