lightning:recordForm Example

In this blog, I am going to show the simple example how to use lightning:recordForm .A lightning:recordForm component enables you to quickly create forms to add, view, or update a record. Using this component to create record forms is easier than building forms manually with lightning:recordEditForm and lightning:recordViewForm. This component takes care of field-level security and sharing for you, so users see only the data that they have access to.

The component accepts a mode value that determines the user interaction allowed for the form. The value for mode can be one of the following:

edit – Creates an editable form to add a record or update an existing one. When updating an existing record, specify the recordId.
view – Creates a form to display a record that the user can also edit. The record fields each have an edit button. View mode is the default, and as such, can be omitted.
readonly – Creates a form to display a record without enabling edits. The form doesn’t display any buttons.

Creating a Record

Use mode=”edit” and pass in the object API name for the record to be created. Specify the fields using the fields attribute, or layoutType attribute to load all the fields defined on the given layout.Because no recordId is passed, edit mode loads the form with input fields that aren’t populated with field data. The form displays Submit and Cancel buttons

 

Editing a Record

Use mode=”edit” and pass the ID of the record and the corresponding object API name to be edited. Specify the fields using the fields attribute, or layoutType attribute to load all the fields defined on the given layout.When a recordId is passed, edit mode loads the form with input fields displaying the specified record’s field values. The form also displays Submit and Cancel buttons.

 

Viewing a Record Read-Only

Use mode=”readonly” and pass the ID of the record and the corresponding object API name to be displayed. Specify the fields using the fields attribute, or layoutType attribute to display all the fields defined on the given layout.The readonly mode loads the form with output fields only, and without Submit or Cancel buttons

 

 

Complete code