I am trying to create a new button for a Custom object to add it in community.On clicking the button lightning-record-form will be displayed in edit mode to show input fields of the layout but what ever fields are required on the layout are not showed as required on the form.
2 Answers
As of Winter '20 you can make the lightning-input-field component required
lightning-input-field The following attribute is new.
- required—To make an input field required only on the client, include the required attribute. Use this attribute to require a value in a field before the form can be submitted, and the field isn’t marked required in Setup. If the field doesn’t have a value, the component’s client-side validation catches the error before the form data is submitted to the server.
https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_lwc_components.htm
lightning-record-edit-form will not take care of fields that are required on the page layout. If a field is required on a field level, that will be taken-care by lightning data service. You need to explicitly check the field in your form which is not required on field level using onsubmit event.
handleSubmit(event){
event.preventDefault(); // stop the form from submitting
const fields = event.detail.fields;
if(fields.Street){
this.template.querySelector('lightning-record-edit-form').submit(fields);
}else{
//do something
}
}
-
I was using this as an alternative for NavigationMixin to set defaultvalue for a field. Commented Sep 10, 2019 at 17:30
-
You can use it for checking blank values too. In else condition, you can show some error toast or anything you want. Commented Sep 10, 2019 at 17:34