Formidable API - Good or bad use?

By: Julius hernández | Asked: 08/29/2023
ForumsCategory: Code HelpFormidable API - Good or bad use?
Julius hernándezJulius hernández asked 11 months ago

I've succesfully used this code to update entries in a form:

jQuery(document).ready(function($){
var apiKey = 'MY-API-KEY'; // Replace with your API key

$('.pickupCateg').click(function() {
var spanText = $(this).find('span').text(); // Get the text from the span
var parts = spanText.split('-'); // Split the text by the hyphen
var value = parts[0]; // The first part is the value
var entryId = parts[1]; // The second part is the entry ID
var label = $(this).clone().children().remove().end().text().trim(); // Get the label from the div excluding span

updateFormidableEntry(entryId, value, label);
});

function updateFormidableEntry(entryId, value, label) {
$.ajax({
beforeSend: function(request) {
request.setRequestHeader('Authorization', 'Basic '+ btoa( apiKey +':x' ));
},
dataType: "json",
method: "POST",
data: { "1019": value, "1022": label }, // Update the field values
url: 'https://mysite.com/wp-json/frm/v2/entries/' + entryId, // Update the URL with the form and entry ID
success: function(response){
console.log('Updated entry!');
// Reload the page with a delay of 200ms
setTimeout(function() {
location.reload();
}, 200);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log('Error updating entry');
}
});
}
});

The problem is that this only works for administrator role.

My question is: is this a good use of Formidable API to update entries from a javascript code? Is correct that I use my API key there or it isn't needed at all? I'm not done understanding the function of the API and the API key.

How can I make this code to work with all roles? If it's not possible, what are the best alternatives to manipulate data from Javascript?

Julius hernándezJulius hernández replied 11 months ago

I forgot to mention that this code is assigned to an element displayed in a View, because I need that when the element is clicked, the current displayed entry change its value to the one that was clicked. Something like the link to update a field.

1 Answers
Victor Font answered 11 months ago
Edit in place is meant to work with a view. What made you decide to use the API? It’s more than you need. 
Julius hernándezJulius hernández replied 11 months ago

What I really need is to make some operations in Javascript, then the result of those operations, goes directly to the field and the entry.

For example, a View shows a list of entries, with four or five fields. When user clicks an element (as in cell in Excel) a view is opened in a modal, showing the other options to choose for that element. Then when the user clicks one option in the View (as if they are choosing a radio button in a form), the clicked option is set in that entry.

The reason I'm not using the form is that is difficult for me to work in a Modal with cascade dynamic fields, because if there are more than one of the same form in the page, it produces javascropt errors and dynamic fields don't work as expected.

Second reason (less important) is that formatting the View is easier than the form and somehow looks more elegant.

Looking in the docs, I saw the Updating an entry section in the Formidable API docs page, and I learned that I can send the exact data I need to just one entry.

I 'm not using the [frm-entry-update-field] shortcode because it's difficult to call it from a button, or to call any shortcode from a button. Unless I'm missing something crucial.

What do you recommend? What other options do I have to use Javascript to change a given field of a given entry?

Julius hernándezJulius hernández replied 11 months ago

Oh, and other reason is that my boss/client likes better this method, he saw the result and we both liked it. But only works for admin. Again, unless I'm missing something crucial.

Once I learn how to arbitrarily change any field in any view with JS, a lot of possibilities open for me. 🙂

Julius hernándezJulius hernández replied 11 months ago

Victor, sorry for the multiple comments, but I tried to not use the Formidable API in the View and substituted it with [frm-entry-update-field] shortcode, in this way:

[frm-entry-update-field id=[get param=entrypass] field_id=1019 value=[4rkbn]]

Entrypass is getting the entry value of the parent view (the one listing the entry), field_id is the field to change and value is the value to set.

Some problems arise: if the entry value is equal to the update value, the link appears empty, and I need to change two fields at one click.

Formidable provides a PHP alternative but I don't know how to call it from Javascript.

Can you give me some guidance/advice, please?

Victor Font replied 11 months ago

The thing that concerns me the most about this approach, whether the boss/client likes it better or not, is that you’re publishing the API key in plain text for the entire world to see. It’s like giving a hacker a key to your database and daring them to poke around to see what they can find.

Julius hernándezJulius hernández replied 11 months ago

I understand, that's why I'm posting this.

Indeed, I asked the Support because in the docs says the API key must be "inserted somewhere safer", but they tell me that custom code is outside of their scope and told me to ask here.

Anyway, I don't insist in neccesarily using the API key, my question then turns to something with the same result:

1) Keep using the API key, putting it somewhere safer and let all roles to use the link, or

2) To create/edit an entry from a link with Javascript with any other method. Can I do it with Ajax without using the API key, like the [frm-entry-update-field] shortcode does?

These are the things that delays my work, I know I'm still developing my skills but the docs are very vague in some points.

Julius hernándezJulius hernández replied 11 months ago

<p>Victor, I thought of a workaround: creating a new shortcode almost identical to the original [frm-entry-update-field] (assisted by ChatGPT in the implementation is easy and I learn a lot), but I'm having trouble to make it work. I'm going to open a new question, and I would really like your guidance. 🙏🏻 Only your guidance, I'm not asking you to write code for me.</p>

Victor Font replied 11 months ago

From my experience with ChatGPT, it's not very Formidable aware. The code I've seen it write is often generic and sometimes, just wrong.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right