Set a field using field key rather than field ID

By: Henry King | Asked: 10/17/2022
ForumsCategory: Code HelpSet a field using field key rather than field ID
Henry King asked 1 year ago
Hi   I understand I can set a field using field ID such as $_POST['item_meta'][125] = "XYZ".   However, if I want to set it using field key instead, how do I do that?    Referencing by field key may be useful as field ID may change.   Thanks!   Henry
1 Answers
Victor Font answered 1 year ago
You're right that IDs can change when moving code to bew instances of Formidable. This is something to be aware of when you write Formidable apps and offer them for sale. You have to mae sure that your custom code works across any platform for any user. This means creating field keys and using them as your reference. First, here's an explanation for writing transportable code: https://formidable-masterminds.com/writing-transportable-code-keys-vs-ids/ Next, Formidable provides the get_id_by_key() helper functions to convert keys to IDs in any environment. The PHP $_POST array referenced in your post can only use field IDs because this is how the name attribute is built for the HTML generated for input fields. The field key is used in the input's HTML element id, but the name uses the field id. hHis cannot be changed. The get_id_by_key() function is available in multiple Formidable classes. For example, when getting a field id, use FrmField::get_id_by_key("key"). For a form, use FrmForm::get_id_by_key("key"), and etc. for other object types.
Henry King replied 1 year ago

Thanks for the prompt and detailed reply.
Hence in conclusion there is no ability to directly use field key to set value. I need to get the field ID through get_id_by_key first then use the ID to set or retrieve value for the field?

Victor Font replied 1 year ago

Not using the PHP $_POST array. The PHP $_POST array stores the field value by HTML input name.

Henry King replied 1 year ago

I need not use $_POST in PHP. Is there another method that I can use to directly change a field using field key using PHP?

Victor Font replied 1 year ago

If you want to change the value of a field before the entry is created, you have to use $_POST. The values in $_POST are what's written to the database. If you want to change a field value after the entry has been saved, there are several ways to do it including using your own custom SQL. No matter what technique you use, you still have to use get_id_by_key(). The field key is not stored with the data. It is stored in a completely different table and not related to how data is stored. The Formidable database schema may help with understanding how Formidable stores values in the database. https://formidableforms.com/knowledgebase/database-schema/

Henry King replied 1 year ago

Ok. Got it which means it is rather inefficient as I've to get the field id based on field key and then use the $_POST with the field id.

Thanks.

Victor Font replied 1 year ago

This is the way any form (not just Formidable) submits data to a PHP back end.

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