on ‎2022 Mar 07 5:55 AM
Dear CAP Team,
Need help related to following CAP Java questions which are as follows:-
1. Is it possible to give custom message for @mandatory fields in CAP Java like Field1name cannot be null,Field2name cannot be null?
2. When default/standard error messages are thrown for all the @mandatory annotated fields, target is not getting set for 1 field(any random field in the entity like name, sometimes type or role), why is that so and how this could be resolved?

3. Is it possible in CAP Java to make one field mandatory or not based on value selected/filled in the other field i.e., conditional mandatory?
Any help would be appreciated.
Thanks in advance!!
Regards,
Krishma
Request clarification before answering.
Hi krishma_bhatia16,
1:
At the moment it's not possible to intercept exceptions, in order to for example change the error message, but we have planned such a feature for the future, it's currently on our backlog.
What you can do at the moment is to handle the validation yourself as ivan.mirisola described exemplary.
2:
Normally, all message targets for all missing fields should be set. In our bookshop sample there's an example with the review entity which is annotated with @mandatory for the subject and title fields.
If not set when creating a new review...

you get the error with the set targets, therefore the fields in the UI are highlighted accordingly.

Please create a CAP issue if this is not the case for you, and we can check why.
3:
Having a field mandatory based on conditions is not supported with the @mandatory annotation. If you have such a use case, you need to implement your own validation handler as mentioned in 1.
Hope that helps.
Regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks davidlam for stepping in! Much appreciated.
Thank davidlam for detailed explaination. I've tried this approach and it worked for main entity but not working for any of my assoicated entites. Can you please help me to understand how this approach will work with Association and Composition?
entity Student : cuid, managed { rollno : String(5) @mandatory @Core.Immutable @assert.unique; name : String(50) @mandatory ; email : String(200) @mandatory; } annotate Student with { name @assert.format: '/^[a-zA-Z]+ [a-zA-Z]+$/'; email @assert.format: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'; } entity Scorecard : cuid, managed { student : Association to Student; details : String(100); marks : Integer; grade : String(3); } annotate Scorecard with { @error.mandatory: 'Scorecard.marks.mandatory' marks; @error.mandatory: 'Scorecard.grade.mandatory' grade; }
Hi krishma_bhatia16,
I couldn't find any means to intercept the error messages in CDS4J. Maybe you could find someone in the product team to give you more insights on this. davidlam may help you find the correct person to assist with your doubts.
Meanwhile, you could have your own Custom Validation Class to handle the Create/Update events.
Here is a code snippet on how to implement such validation in CDS4J:
@Component
@ServiceName(value = "*", type = ApplicationService.class)
public class CommonServiceEventHandler implements EventHandler {
private MyValidator myValidator;
@Before(event = { CdsService.EVENT_CREATE, CdsService.EVENT_UPDATE })
@HandlerOrder(HandlerOrder.LATE)
public void validateSomething(EventContext context, List<CdsData> data) {
CdsDataProcessor.create()
.addValidator(
(Path path, CdsElement element, CdsType type) ->
<<<Check Logic>>>, myValidator)
.process(data, context.getTarget());
}
And the validator class where a message can be thrown:public class MyValidator implements Validator {
@Override
public void validate(Path path, CdsElement element, Object value) {
<<<Validation Logic>>>
if .... {
throw new ServiceException(<<< validation message >>>);
}
}
}
Best regards,You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @davidlam @Ivan-Mirisola , I am looking for the same issue ..As of today do we have any annotation for error message to be added to the field in entity label..So that the custom error message send from backend directly mapped to UI fields..
There are specific annotations for input validations that create specific error messages: https://cap.cloud.sap/docs/guides/providing-services#input-validation However, there are no annotations to define custom error messages for a field.
It wouldn't be enough to specify the content of the error message, but you'd also need to specify when to throw these. At the moment, you have to do this programmatically in an event handler and use the message target API to indicate the elements targeted by your error.
See https://cap.cloud.sap/docs/java/event-handlers/indicating-errors#target for more details.
EDIT: @avijit_khandelwal I have to correct myself a bit. Alternatively, to what I said above, we've released in April assert constraints as an experimental alpha feature. This might exactly be what you're looking for.
| User | Count |
|---|---|
| 7 | |
| 6 | |
| 6 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.