Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format input value for server side #220

Closed
Mushr0000m opened this issue Apr 23, 2019 · 8 comments
Closed

Format input value for server side #220

Mushr0000m opened this issue Apr 23, 2019 · 8 comments

Comments

@Mushr0000m
Copy link

Hi,

I have an input like this :

<input class="tags" type="text" name="keywords" value='["repellat inventore","magni accusamus"]'>

But when sending the form on the server side I get :

[
    {"value":"repellat inventore"},
    {"value":"magni accusamus"}
]

Is it possible to have it the same way as the initial value ? A JSON format :

["repellat inventore","magni accusamus"]

Thanks

@yairEO
Copy link
Owner

yairEO commented Apr 23, 2019

Why can't you transform the data before sending it to the server?

@Mushr0000m
Copy link
Author

Mushr0000m commented Apr 23, 2019

Thanks for the quick reply.
It's not that I can't it's that I don't know where to do it within the plugin declaration :

new Tagify(element, {
    delimiters: ',',
    duplicates: false,
    autocomplete: true,
    addTagOnBlur: false
});

I use it in vanilla JS, so each elements with the class .foo uses your plugin and the input value shoud be just the values, as the ones passed in. Is there a callback to transform the actual input values ?

@yairEO
Copy link
Owner

yairEO commented Apr 23, 2019

This isn't related to my plugin, but to your place in the code where you send the data to the server. There you need to transform the data to your required format (probably)

@Mushr0000m
Copy link
Author

Ok i was hoping for a way to just leave the input with the actual values separated by the delimiter. I think you can close the issue then.

@yairEO
Copy link
Owner

yairEO commented Apr 24, 2019

I prefer the developer who implements the solution to take care of value format transformation in their end rather than implementing some solution within Tagify to customize the value's format.

@gurnzbot
Copy link

Having the values passed in as a simple array would be great. This package is great, but having to deal with parsing the values server-side after a form submit was lame.

Either way, minor gripe for what this package offers 😸

@douglasmiranda
Copy link

douglasmiranda commented Jan 7, 2021

Example: Form and save when form changes

jQuery(document).ready(function ($) {
  $input = $('#my-form')
    .tagify()
    .on('change', function (e, tagData) {
      if (tagData === undefined) {
        return;
      }
      // string [{"value":"test"}', '{"value":"test2"}] to JSON Object
      var tags = JSON.parse(tagData);
      // Converts into a simple array ["test", "test2"], then convert to string "test,test2"
      var tags_parsed = tags.map(item => item.value).toString();
      
      // Then you can do some ajax post and send the tags_parsed to the server side.
    });
});

https://gist.github.com/douglasmiranda/1c7ddb36eee035687f5f371c77b3ea28

@akz92
Copy link

akz92 commented May 18, 2022

For future reference, this is now officially supported by using the originalInputValueFormat option as explained in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants