diff --git a/cmb2-field-ajax-search.php b/cmb2-field-ajax-search.php
index 514e542..ec0ffc4 100644
--- a/cmb2-field-ajax-search.php
+++ b/cmb2-field-ajax-search.php
@@ -46,19 +46,23 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
add_filter( 'cmb2_pre_field_display_term_ajax_search', array( $this, 'display' ), 10, 3 );
// Sanitize
- add_action( 'cmb2_sanitize_post_ajax_search', array( $this, 'sanitize' ), 10, 4 );
- add_action( 'cmb2_sanitize_user_ajax_search', array( $this, 'sanitize' ), 10, 4 );
- add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 );
+ //add_action( 'cmb2_sanitize_post_ajax_search', array( $this, 'sanitize' ), 10, 4 );
+ //add_action( 'cmb2_sanitize_user_ajax_search', array( $this, 'sanitize' ), 10, 4 );
+ //add_action( 'cmb2_sanitize_term_ajax_search', array( $this, 'sanitize' ), 10, 4 );
// Ajax request
add_action( 'wp_ajax_cmb_ajax_search_get_results', array( $this, 'get_results' ) );
}
+ public function convert_as_id_css( $name ) {
+ return str_replace( '__', '_', str_replace( '[', '_', str_replace( ']', '_', $name ) ) );
+ }
+
/**
* Render field
*/
public function render( $field, $value, $object_id, $object_type, $field_type ) {
- $field_name = $field->_name();
+ $field_name = $this->convert_as_id_css($field->_name());
$default_limit = 1;
// Current filter is cmb2_render_{$object_to_search}_ajax_search ( post, user or term )
@@ -78,9 +82,9 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
?>
args( 'sortable' ) ) : ?>
-
-
- object_text( $field_name, $val, $object_to_search ); ?>
+
+
+ object_text( $field->_name(), $val, $object_to_search ); ?>
@@ -108,7 +112,7 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
echo $field_type->input( array(
'type' => 'text',
- 'name' => $field_name . '_input',
+ 'name' => $field->_name() . '_input',
'id' => $field_name . '_input',
'class' => 'cmb-ajax-search cmb-' . $object_to_search . '-ajax-search',
'value' => $input_value,
@@ -167,12 +171,10 @@ if( ! class_exists( 'CMB2_Field_Ajax_Search' ) ) {
* Optionally save the latitude/longitude values into two custom fields
*/
public function sanitize( $override_value, $value, $object_id, $field_args ) {
- $fid = $field_args['id'];
+ $value = false;
- if($field_args['render_row_cb'][0]->data_to_save[$field_args['id']]) {
+ if( isset( $field_args['render_row_cb'][0]->data_to_save[$field_args['id']] ) ) {
$value = $field_args['render_row_cb'][0]->data_to_save[$field_args['id']];
- } else {
- $value = false;
}
return $value;
diff --git a/js/ajax-search.js b/js/ajax-search.js
index 47d6dce..ad63f40 100644
--- a/js/ajax-search.js
+++ b/js/ajax-search.js
@@ -4,7 +4,7 @@
$(this).attr('data-ajax-search', true);
var input_id = $(this).attr('id'); // Field id with '_input' sufix (the searchable field)
- var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ); // Field id, the true one field
+ var field_id = $(this).attr('id').replace( new RegExp('_input$'), '' ).replace(/[\[\]']+/g, '_'); // Field id, the true one field
var object_type = $(this).attr('data-object-type');
var query_args = $(this).attr('data-query-args');
@@ -50,15 +50,16 @@
},
onSelect: function ( suggestion ) {
$(this).devbridgeAutocomplete('clearCache');
-
- var field_name = $(this).attr('id').replace( new RegExp('_input$'), '' );
+
+ var field_name = $(this).attr('name').replace( new RegExp('_input$'), '' );
var multiple = $(this).attr('data-multiple');
var limit = parseInt( $(this).attr('data-limit') );
var sortable = $(this).attr('data-sortable');
-
+ var field_name_temp = field_name.replace( /[\[\]']+/g, '_' );
+
if( multiple == 1 ) {
// Multiple
- $('#' + field_name + '_results' ).append( '' +
+ $('#' + field_name_temp + '_results' ).append( '' +
( ( sortable == 1 ) ? '' : '' ) +
'' +
'' + suggestion.value + '' +
@@ -68,7 +69,7 @@
$(this).val( '' );
// Checks if there is the max allowed results, limit < 0 means unlimited
- if( limit > 0 && limit == $('#' + field_name + '_results li').length ) {
+ if( limit > 0 && limit == $('#' + field_name_temp + '_results li').length ) {
$(this).prop( 'disabled', 'disabled' );
} else {
$(this).focus();