Introduction
The Field object contains all settings for a particular field. It is part of the Form Object and is available in most Gravity Forms hooks. It can be manipulated to dynamically change the way the field is displayed.
Usage
// returns the label of the first field on the form
$form['fields'][0]->label;
// displays the types of every field in the form
foreach ( $form['fields'] as $field ) {
echo $field->type . '<br/>';
}
Properties
The following properties are common to multiple field types.
Property | Type | Description |
---|---|---|
adminLabel | string | When specified, the value of this property will be used on the admin pages instead of the label. It is useful for fields with long labels. Applies to: All fields |
adminOnly | bool | Determines if this field should only visible on the administration pages. A value of 1 will mark the field as admin only and will hide it from the public form. Useful for fields such as “status” that help with managing entries, but don’t apply to users filling out the form. Applies to: All fields |
allowsPrepopulate | bool | Determines if the field’s value can be pre-populated dynamically. 1 to allow field to be pre-populated, 0 otherwise. Applies to: All fields |
choices | array | An array of choices. See Choice Properties for how each choice in the array should be defined. Applies to: checkboxes, radio, select, and post_category |
Conditional Logic | array | Controls the visibility of the field based on values selected by the user. |
content | string | Content of an HTML block field to be displayed on the form Applies to: html |
cssClass | string | Custom CSS class to be added to the <li> tag that contains the field. Useful to apply custom formatting to specific fields. Applies to: All fields |
defaultValue | string | Contains the default value for the field. When specified, the field’s value will be populated with the contents of this property when the form is displayed. Applies to: hidden, text, website, phone, number, date, textarea, email, post_title, post_content, post_excerpt, post_tags, post_custom_field |
description | string | Field description. Applies to: All fields |
enableChoiceValue | bool | Determines if the field (checkbox, select or radio) have choice values enabled, which allows the field to have choice values different from the labels that are displayed to the user Applies to: checkbox, select and radio |
errorMessage | string | Contains the message that is displayed for fields that fail validation Applies to: All fields except html, section and hidden |
id | integer | Field id |
inputName | string | Assigns a name to this field so that it can be populated dynamically via this input name. Only applicable when allowsPrepopulate is set to 1. Applies to: All fields except section and captcha |
inputs | array | An array of inputs. See Input Properties for how each input should be defined. Applies to: name, address, checkbox. Note: For checkbox fields, there should be an input for each choice. |
isRequired | bool | Determines if the field requires the user to enter a value. 1 marks the field as required, 0 marks the field as not required. Fields marked as required will prevent the form from being submitted if the user has not entered a value in it. Applies to: All fields except section, html and captcha |
label | string | Field label that will be displayed on the form and on the admin pages Applies to: All fields |
noDuplicates | bool | Determines if the field allows duplicate submissions. 1 to prevent users from submitting the same value more than once, 0 to allow duplicate values. Applies to: hidden, text, website, phone, number, date, time, textarea, select, radio, email, post_custom_field |
size | string | Determines the size of the field when displayed on the page Applies to: All fields except html, section and captcha Possible values: small, medium, large |
type | string | The type of field to be displayed Applies to: All fields Possible values: html, hidden, section, text, website, phone, number, date, time, textarea, select, checkbox, radio, name, address, fileupload, email, post_title, post_content, post_excerpt, post_tags, post_category, post_image, post_custom_field, captcha |
placeholder | string | Use the Placeholder to give a hint at the expected value or format. Applies to: All fields except section, HTML, captcha, hidden, radio buttons, checkbox, multiple choice, image choice, page |
choiceAlignment | string | The alignment for choices Applies to: multiple choice field Possible values: horizontal, vertical |
Choice Properties
Property | Type | Description |
---|---|---|
text | string | The text to be displayed to the user when displaying this choice. |
value | string | The value to be stored in the database when this choice is selected. |
isSelected | bool | Indicates if the choice should be selected by default when the field is displayed. |
price | string | The price of the item. Used by Product, Option, and Shipping type fields. |
$choices = $field->choices;
$choices[0]['text'] = 'Drop down item 1';
$field->choices = $choices;
Input Properties
Property | Type | Description |
---|---|---|
id | string | The input ID. Following the following naming convention: FIELDID.NUMBER (i.e. 5.1), where FIELDID is the id of the containing field, and NUMBER is a unique value that does not end with 0 .For example, inputs for the name field are numbered as follows: FIELDID.2 Name Prefix input id (i.e. 4.2) FIELDID.3 First Name input id (i.e. 4.3) FIELDID.6 Last Name input id (i.e. 4.6) FIELDID.8 Name Suffix input id (i.e. 4.8) |
label | string | The input label. |
name | bool | The input parameter name for use with dynamic population. |
$inputs = $field->inputs;
$inputs[0]['label'] = 'First Name';
//sets the label for the first input
$field->inputs = $inputs
Field (input) Types
For field type-specific properties, see the properties sections of the following articles:
- Address
- Calculation (Product)
- CAPTCHA
- Checkboxes
- Consent
- Credit Card (deprecated)
- Date
- File Upload
- Hidden
- Hidden (Product)
- HTML
- List
- Multi Select
- Name
- Number
- Page
- Password
- Phone
- Radio
- Section
- Select (Drop Down)
- Single Product
- Single Shipping
- Text
- Textarea (Paragraph)
- Time
- Total
- User Defined Price
- Website
Post Fields
Property | Type | Description |
---|---|---|
postCustomFieldName | string | The name of the Post Custom Field that the submitted value should be assigned to. Applies to: post_custom_field |
displayAllCategories | bool | Determines if all categories should be displayed on the Post Category drop down. 1 to display all categories, 0 otherwise. If this property is set to 1 (display all categories), the Post Category drop down will display the categories hierarchically. Applies to: post_category |
displayCaption | bool | Controls the visibility of the caption metadata for Post Image fields. 1 will display the caption field, 0 will hide it. Applies to: post_image |
displayDescription | bool | Controls the visibility of the description metadata for Post Image fields. 1 will display the description field, 0 will hide it. Applies to: post_image |
displayTitle | bool | Controls the visibility of the title metadata for Post Image fields. 1 will display the title field, 0 will hide it. Applies to: post_image |
inputType | string | Contains a field type and allows a field type to be displayed as another field type. A good example is the Post Custom Field, that can be displayed as various different types of fields. Applies to: post_custom_field |
Add-On Fields
See the following pages for details about fields added by add-ons:
Field JSON
This example shows how a field object would look when formatted as JSON for use by the Gravity Forms CLI Add-On.
{
"type": "select",
"label": "My Dropdown",
"choices": [{
"text": "Choice 1",
"value": "one"
}, {
"text": "Choice 2",
"value": "two"
}],
"id": "2",
"visibility": "visible",
"formId": "14",
"pageNumber": 1,
"isRequired": false
}