Lightning:RecordForm is a powerful component which is part of Lightning data service. There are many new things coming in lightning that’s make us to try our hand on all of them. Previously if we need to create Edit or detail page we need to manually create them. Either we need to use custom code or we can use Lightning:recordEditForm or LightningDataService.
But now we have Lightning:RecordForm, which enables us to quickly create forms to add, view, or update a record. Using this component, it is few lines to create record forms than building forms manually with
lightning:recordViewForm. Using a single line of code we can create full page layout. And we don’t need to worry about layout changes as it take care of them.
<lightning:recordForm recordId="001XXXXXX" objectApiName="Account" layoutType="Full" mode="view" />
But what if we need to add some custom validation before creating record. If we need to update some fields before saving the record then we can also customize this as well. Today we will take an example where we will use onsubmit to save the record. And After save we will redirect to record detail page.
This is how our flow will run.
So here we are updating an existing record. We can remove the recordId if we want to create a new record. So once user click on save button it fires the onsubmit event. So in this event first we stop the standard execution and then we pass this data to apex. Finally in Apex we are creating the actual record and returing the record Id. Now using the navigation method we redirect to record detail page. So don’t forget to stop the standard flow else it will create two records for you.
As this component is generic so you can use it with any object to quickly get the edit page or new record screen.
We also have lightning-record-form which is an upgrade version in Lightning Web Components.
Did you want to add anything or have any question, please let me know in comments section. Happy programming 🙂