Formidable Forms [auto_id]
shortcode is used to set an unique and auto-incrementing value to form entries. Example usecases include serial number, job number and invoice number.
By default, numbers start from 1 but it can be changed with “start” attribute.
e.g. [auto_id start=1000]
A prefix and/or suffix can be added by simply adding letters before and/or after the shortcode.
e.g. SN[auto_id start=1000]A
This will generate SN1000A, SN1001A, SN1002A, ....
and so on.
One of the limitations of the shortcode is, prefix/suffix can’t have numbers. If numbers included, it could result in unexpected values. However, adding zeros as the prefix will work since leading zeros have no effect on a number. We might tempt to use it generate a serial number with a fixed number of digits like 001, 002, 003, … using 00[auto_id]
. But the issue is after 10, it will become 0010 instead of 010. In order to dynamically add leading zeros, following custom code can be used.
add_filter('frm_pre_create_entry', 'format_auto_number'); function format_auto_number($values){ if ( $values['form_id'] == 27 ) {//Replace 27 with your Form ID //Replace 126 with your field ID. //Here 3 in sprintf function represents number of fixed digits. $values['item_meta'][126] = sprintf("%03d", $values['item_meta'][126]); } return $values; }
Hi, auto-id in a repeter row would be cool. could you create a code for this, too!? THX
this seems not to work 🙁
https://formidableforms.com/knowledgebase/javascript-examples/#kb-add-row-counter
I’ve tried your custom code, and placed it in functions.php inside child theme folder. Put the proper id on both form and field, but somehow it didn’t work, for the auto id still displaying a default numbering without any leading zeros.
Where did I do wrong?
I require elaboration…
Hi,
The above code updates the leading zero upon saving. It doesn’t show leading zero when displaying the form. If you would like to show it in the form, use the following code. You will need both code snippets in order to work correctly.
add_filter(‘frm_setup_new_fields_vars’, ‘format_auto_number_in_form’, 20, 2);
function format_auto_number_in_form($values, $field){
if($field->id == 262){//Replace with the ID of your field
$values[‘value’] = sprintf(“%03d”, $values[‘value’]);
}
return $values;
}
What if I used [date format=”ym”]00[auto_id]? Will October become a problem?
It should not. The format “ym” will add leading zero in front end of months before October.
do you know how to reset auto increment back to 1 every day?
suppose at 25 august 2021, the auto increment is at 355
next day, i want this auto increment start from 1 again at 26 august 2021
Is any code or tricks to do auto-id( unique) in a repeater? Please help me.