<lightning-messages></lightning-messages>
Two uses of the label to display error messages
Article Directory
Label introduction
<lightning-messages></lightning-messages>
Is a component lightning-record-edit-form
that comes with the label, it prompts the user with a unified ui error messages (error messages).
usage
Field-level validation rule error message display
In the custom field, we can set validation rules, and set error messages for the validation rules, so that it will be displayed in the <lightning-messages></lightning-messages>
position when the validation fails .
Establish verification rules
- Enter the custom object/standard object page in the object manager
- Choose validation rule options
- Create a new validation rule
- Enter the rule name (it is recommended to give a name that can summarize your validation rules)
- Enter the error condition formulas formula that is true , the trigger error, stop recording the new / updated
- Enter a custom error message in the error message, and select the top of the page for the error location.
effect
After the validation rule is established, whether it is in the standard lightning creation, editing record table or custom component, lightning-record-edit-form
if there is an error in the validation rule, the error message will be displayed at the top of the page:
Establish code-level verification rules in Trigger
<lightning-messages></lightning-messages>
Essentially, it is the front-end display of errors in the standard data-service of the lightning framework. Therefore, in Trigger, we can also trigger record-level errors and display them on page components. At the code level, we can be more flexible, but at the same time it will also increase the amount of code.
example
We create a trigger for a certain object.
trigger Battleship on Battle_Station__c (before insert,before update) {
if (Trigger.isBefore) {
for (Battle_Station__c bs : Trigger.new) {
if (bs.Name.contains('RF')) {
bs.addError('记录名中含有“RF”,无法保存');
}
}
}
}
When the Name
field in the record contains certain characters, the update is prevented and an error is reported.
Note that, in the Trigger or adding a field recording addError()
method, a recording prevent various operations (create, update, etc.) occur.
effect
to sum up
In the above two methods is lightning standard, custom form Top error message method, particularly to be noted that, in the custom component, <lightning-record-edit-form>
the label, <lightning-input-field>
before you need to add the standard <lightning-messages>
tag!
There are two methods: one is low code, and the other is more customized options. Choose between you and your record verification requirements!