cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Regarding CAP Java target and custom error messages

KrishmaBhatia
Product and Topic Expert
Product and Topic Expert
0 Likes
3,580

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

View Entire Topic
Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Likes

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,
Ivan
avijit_khandelwal
Product and Topic Expert
Product and Topic Expert
0 Likes

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..

davidlam
Advisor
Advisor
0 Likes

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.