简体   繁体   中英

Setting default value of Name field in Gravity Forms

In Gravity Forms you can set the default value of a field by using code like:

$field->defaultValue = 'My default value';

this is mentioned in documentation here: https://docs.gravityforms.com/field-object/

However this doesn't work for the Name field as this is a multi-input field as it contains a first and last name. How do I set the default value of the name field in Gravity Forms?

As you mentioned, the Name field is a multi-input field. You must interact with the desired input on a multi-input field. For example, to set the first name, you would do something like this:

$field->inputs[2]['defaultValue'] = 'Dave';

Here is the full contents of the inputs field property as JSON:

[{ "id": "9.2", "label": "Prefix", "name": "", "choices": [{ "text": "Mr.", "value": "Mr.", "isSelected": false, "price": "" }, { "text": "Mrs.", "value": "Mrs.", "isSelected": false, "price": "" }, { "text": "Miss", "value": "Miss", "isSelected": false, "price": "" }, { "text": "Ms.", "value": "Ms.", "isSelected": false, "price": "" }, { "text": "Dr.", "value": "Dr.", "isSelected": false, "price": "" }, { "text": "Prof.", "value": "Prof.", "isSelected": false, "price": "" }, { "text": "Rev.", "value": "Rev.", "isSelected": false, "price": "" }], "isHidden": true, "inputType": "radio", "defaultValue": "Mr." }, { "id": "9.3", "label": "First", "name": "", "defaultValue": "David" }, { "id": "9.4", "label": "Middle", "name": "", "isHidden": true, "defaultValue": "Stover" }, { "id": "9.6", "label": "Last", "name": "", "defaultValue": "Smith" }, { "id": "9.8", "label": "Suffix", "name": "", "isHidden": true, "defaultValue": "Jr." }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM