How do I prefill a name in First/Last format?

By: Tom Boltwood | Asked: 07/16/2022
ForumsCategory: Code HelpHow do I prefill a name in First/Last format?
Tom BoltwoodTom Boltwood asked 2 years ago

I'm trying to pre-populate a form with a customer's details. However, I'm struggling with the format for the first and last name option. As far as formidable is concerned it is one field. Looking at both the $values and the $fields I can't see where to put it in. I've tried the below with no success.
function form_populate( $values, $field ) {

// Add candidate details to telephone screening form
if( $field->form_id ==2 ) {

if( !empty( $_GET['account'] ) &&!empty( $guest = get_guest( $_GET['account'] ) ) ) {

if( $field->id =='6' ) {
$values['original_default']['first'] = $guest['first'];
$values['original_default']['last'] = $guest['last'];
if( $field->id =='7' ) {
$values['value'] = $guest['email'];
if( $field->id =='8' ) {
$values['value'] = $guest['company'];
return $values;
add_filter( 'frm_setup_new_fields_vars', 'form_populate', 10, 2 );

3 Answers
Best Answer
Rob LeVineRob LeVine answered 2 years ago

Hi Tom,
Victor was teaching you how to fish and I'll give you a fish .
I created a form with the name field and use a hook to inspect the properties of $values.  Here's what I found:

print_r of $values is Array ( [id] => 418 [default_value] => [name] => Name [description] => [options] => [required] => 0 [field_key] => ovveg [field_order] => 2 [form_id] => 47 [value] => Array ( [first] => Rob [last] => LeVine ) 

print_r of $values['value'] is Array ( [first] => Rob [last] => LeVine )

so $values['value']['first'] is 'Rob' and $values['value']['last'] is 'LeVine' 

Tom BoltwoodTom Boltwood replied 2 years ago

Thanks for that, I swear I'd tried that first, but maybe not! I've solved it using a shortcode for now. [guest attr='first']

Victor Font answered 2 years ago
This is a complicated method to use for pre-populating form data. Formidable has shortcodes you can set as default field values. Which shortcode you use depends on the source of the data. What is the data source for the values returned from your get_guest() function? With Formidable default value shortcodes you can pull user_meta or form entry data. See this to learn more: If Formidable's shortcodes don't quite cut it for you, you can create your own. Now back to your original question, I see that you're getting the account number from a query string. What is the user clicking on to deliver the query string? From the way your $field_id 6 if statement is structured, it looks like you're using an address field? I'm asking because I never use address fields when I build forms for the simple reason that an address field is actually an embedded form. Embedded forms can complicate development. I use them sparingly. I prefer keeping PII fields together in the main registration form rather than an embedded form. Either way is perfectly legit, this is just my personal preference. I can also tell that your an experienced developer by the fact you said, "Looking at both the $values and the $fields...". There are not many questions asked here that refer to examining filter parameters. That's an advanced development technique. If you are using vardump() or print_r() to view the parameter contents, I suggest giving Kint a try. Kint is available as a WordPress plugin in the repository. I use the version from Tonya Mork. It hasn't been updated for a long time and it doesn't work as delivered in PHP 8+ because of the way arrays are initialized in a couple of places. PHP used to allow array initialization with either { } or [ ] just like JavaScript does. The newest versions of PHP deprecated { } array initialization. There are 2 or 3 places in Tonya's code where the arrays need to be changed from { } to [ ] if you are working with PHP 8+. The reason I like Kint is because it let's us set breakpoints where you can visualize parameter values in a tree structure. There are only two Kint commands to know: d() and ddd(). If you're debugging on the front end, Kint doesn't work well for returned Ajax responses. For server side testing I use PHPStorm with Xdebug. This allows for pinpoint watch analysis and server side Ajax step testing.
Tom BoltwoodTom Boltwood answered 2 years ago
Thanks for that. Yeah, I'm emailing myself a print_r of the variables to try and figure out what's available. Kint sounds interesting, I'll check it out. In terms of what I'm trying to achieve, I have a form that is an RSVP for a party invitation. I want to pre-populate the form with their details. I have uploaded their details to the database with an account field that is the key for their details. I have been using formidable since about 2015 and this is the way I've always pre-populated the fields, just re-using a bit of code I found in the support docs back then. I really only use formidable to create quite simple forms and haven't kept up with half the stuff it does as I don't need it! I'll check out the shortcode option, thanks. As I've adapted it from a previous function I wrote, I can see that the comments aren't relevant to this form, that's the previous form!

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