How to escape exotic characters on a TV field?

Hi,

I have build a website with geolocalisation : the customer can add resources with severals fields and geolocation coordonates.
A json file is generated and it’s working well.

But sometimes, my customer fill a TV field with forbidden characters such as " of invisible spaces… which are invalidate the json file.

It’s need my manual intervention to determine where the issue is and I wonder if there is a easy and flexible way to avoid this usecase.

Any ideas ?

What about running a plugin when a resource is saved, grab the template variable value that’s submitted and parse it to remove any weird characters?

How is the JSON file being generated? The native PHP function json_encode should take care of the escaping for you.

Hummmm I’m using GeoTV addon but finally I’m building the json with several GetResources

My template for this stuff looks like :


        {
          "type": "Feature",
          "geometry": { "type": "Point", "coordinates": [ [[+longitude]], [[+latitude]] ] },
          "properties": {
            "category": {
               "categorie": "[[+tv.Pro_Categorie]]",
               "color": "#[[!getResourceField? &id=`[[+tv.Pro_Categorie]]` &field=`Cat_Color` &processTV=`1`]]",
               "icon": "[[!getResourceField? &id=`[[+tv.Pro_Categorie]]` &field=`Cat_Icon` &processTV=`1`]]",
               "svg": "[[!getResourceField? &id=`[[+tv.Pro_Categorie]]` &field=`Cat_SVG` &processTV=`1`]]"
            },
            "type": "[[!#[[+parent]].pagetitle]]",
            "name": "[[+pagetitle]]",
            "description": "[[+tv.Pro_Description]]",
            "adresse": "<i class='icon-location-pin icons'></i> [[+tv.Pro_Adresse]]",
            "cp": "[[+tv.Pro_CP]]",
            "ville": "[[+tv.Pro_Ville]]<br />",
            "ouverture": "[[!If? subject=`[[+tv.Pro_Lieux_Jours_ouverture]]` &operator=`notempty` &then=`<i class='icon-clock icons icon-fw'></i> [[+tv.Pro_Lieux_Jours_ouverture]]<br />`]]",
            "telfixe": "[[!If? subject=`[[+tv.Pro_Tel-fixe]]` &operator=`notempty` &then=`<i class='icon-phone icons icon-fw'></i> [[+tv.Pro_Tel-fixe]]<br />`]]",
            "telmobile": "[[!If? subject=`[[+tv.Pro_Tel-mobile]]` &operator=`notempty` &then=`<i class='icon-screen-smartphone icons icon-fw'></i> [[+tv.Pro_Tel-mobile]]<br />`]]",
            "email": "[[!If? subject=`[[+tv.Pro_Mail]]` &operator=`notempty` &then=`<i class='icon-envelope icons icon-fw'></i> <a href='mailto:[[+tv.Pro_Mail]]'>[[+tv.Pro_Mail]]</a><br />`]]",
            "web": "[[!If? subject=`[[+tv.Pro_Web]]` &operator=`notempty` &then=`<i class='icon-globe icons icon-fw'></i> <a href='[[+tv.Pro_Web]]'>[[+tv.Pro_Web]]</a>`]]"
          }
        }

So I don’t know how to use json_encode within.

And about your suggestion digitalpenguin, can you please give me en exemple about a plugin for doing this ?

Think you’d be better off using the API to grab the value from the TV, assuming there’s only one or two, stick them in an associative array and then json_encode that. If you didn’t want to do all that work converting your template to that approach, you could just make a snippet that would do it for the problematic fields and then call them as tags in your template.

You could go down the plugin route and do a search and replace on the TV data during a save but there are a lot of characters that could potentially cause problems in your JSON so unless you can guarantee coverage for all of them and proper entity conversion etc, this doesn’t seem like a very robust solution.

1 Like

Another option would be to use JavaScript to limit the characters that can be typed in an input field.

1 Like