<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Regarding CAP Java target and custom error messages in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504939#M4686328</link>
    <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;krishma_bhatia16&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;1:&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://github.wdf.sap.corp/cds-java/home/issues/231"&gt;backlog&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;What you can do at the moment is to handle the validation yourself as  &lt;SPAN class="mention-scrubbed"&gt;ivan.mirisola&lt;/SPAN&gt; described exemplary.&lt;/P&gt;&lt;P&gt;2:&lt;/P&gt;&lt;P&gt;Normally, all message targets for all missing fields should be set. In our &lt;A href="https://github.com/SAP-samples/cloud-cap-samples-java"&gt;bookshop&lt;/A&gt; sample there's an example with the &lt;A href="https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/db/reviews.cds#L19"&gt;review entity&lt;/A&gt; which is annotated with  @mandatory for the subject and title fields.&lt;/P&gt;&lt;P&gt;If not set when creating a new review...&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2054477-screen-01.png" /&gt;&lt;/P&gt;&lt;P&gt;you get the error with the set targets, therefore the fields in the UI are highlighted accordingly.&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2054478-screen-02.png" /&gt;&lt;/P&gt;&lt;P&gt;Please create a &lt;A href="https://github.tools.sap/cap/issues/issues"&gt;CAP issue&lt;/A&gt; if this is not the case for you, and we can check why.&lt;/P&gt;&lt;P&gt;3:&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Hope that helps.&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;David&lt;/P&gt;</description>
    <pubDate>Thu, 19 May 2022 15:02:52 GMT</pubDate>
    <dc:creator>davidlam</dc:creator>
    <dc:date>2022-05-19T15:02:52Z</dc:date>
    <item>
      <title>Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaq-p/12504937</link>
      <description>&lt;P&gt;Dear CAP Team,&lt;/P&gt;
  &lt;P&gt;Need help related to following CAP Java questions which are as follows:-&lt;/P&gt;
  &lt;P&gt;1. Is it possible to give custom message for @mandatory fields in CAP Java like Field1name cannot be null,Field2name cannot be null?&lt;/P&gt;
  &lt;P&gt;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?&lt;/P&gt;
  &lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2027776-target-not-set.png" /&gt;&lt;/P&gt;
  &lt;P&gt;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?&lt;/P&gt;
  &lt;P&gt;Any help would be appreciated.&lt;/P&gt;
  &lt;P&gt;Thanks in advance!!&lt;/P&gt;
  &lt;P&gt;Regards,&lt;/P&gt;
  &lt;P&gt;Krishma&lt;/P&gt;</description>
      <pubDate>Mon, 07 Mar 2022 05:55:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaq-p/12504937</guid>
      <dc:creator>KrishmaBhatia</dc:creator>
      <dc:date>2022-03-07T05:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504938#M4686327</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;krishma_bhatia16&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;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. &lt;SPAN class="mention-scrubbed"&gt;davidlam&lt;/SPAN&gt; may help you find the correct person to assist with your doubts.&lt;/P&gt;&lt;P&gt;Meanwhile, you could have your own Custom Validation Class to handle the Create/Update events.&lt;/P&gt;&lt;P&gt;Here is a code snippet on how to implement such validation in CDS4J:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;@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&amp;lt;CdsData&amp;gt; data) {
	CdsDataProcessor.create()
			.addValidator(
			(Path path, CdsElement element, CdsType type) -&amp;gt; 
				&amp;lt;&amp;lt;&amp;lt;Check Logic&amp;gt;&amp;gt;&amp;gt;, myValidator)
			.process(data, context.getTarget());
}
&lt;/CODE&gt;&lt;/PRE&gt;And the validator class where a message can be thrown:&lt;PRE&gt;&lt;CODE&gt;public class MyValidator implements Validator {

	@Override
	public void validate(Path path, CdsElement element, Object value) {
		&amp;lt;&amp;lt;&amp;lt;Validation Logic&amp;gt;&amp;gt;&amp;gt;
		if .... {
			throw new ServiceException(&amp;lt;&amp;lt;&amp;lt; validation message &amp;gt;&amp;gt;&amp;gt;);
		}
	}
}&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;Best regards,&lt;BR /&gt;Ivan</description>
      <pubDate>Wed, 18 May 2022 16:43:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504938#M4686327</guid>
      <dc:creator>Ivan-Mirisola</dc:creator>
      <dc:date>2022-05-18T16:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504939#M4686328</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;krishma_bhatia16&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;1:&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://github.wdf.sap.corp/cds-java/home/issues/231"&gt;backlog&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;What you can do at the moment is to handle the validation yourself as  &lt;SPAN class="mention-scrubbed"&gt;ivan.mirisola&lt;/SPAN&gt; described exemplary.&lt;/P&gt;&lt;P&gt;2:&lt;/P&gt;&lt;P&gt;Normally, all message targets for all missing fields should be set. In our &lt;A href="https://github.com/SAP-samples/cloud-cap-samples-java"&gt;bookshop&lt;/A&gt; sample there's an example with the &lt;A href="https://github.com/SAP-samples/cloud-cap-samples-java/blob/main/db/reviews.cds#L19"&gt;review entity&lt;/A&gt; which is annotated with  @mandatory for the subject and title fields.&lt;/P&gt;&lt;P&gt;If not set when creating a new review...&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2054477-screen-01.png" /&gt;&lt;/P&gt;&lt;P&gt;you get the error with the set targets, therefore the fields in the UI are highlighted accordingly.&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/2054478-screen-02.png" /&gt;&lt;/P&gt;&lt;P&gt;Please create a &lt;A href="https://github.tools.sap/cap/issues/issues"&gt;CAP issue&lt;/A&gt; if this is not the case for you, and we can check why.&lt;/P&gt;&lt;P&gt;3:&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Hope that helps.&lt;/P&gt;&lt;P&gt;Regards,&lt;BR /&gt;David&lt;/P&gt;</description>
      <pubDate>Thu, 19 May 2022 15:02:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504939#M4686328</guid>
      <dc:creator>davidlam</dc:creator>
      <dc:date>2022-05-19T15:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504940#M4686329</link>
      <description>&lt;P&gt;Thanks &lt;SPAN class="mention-scrubbed"&gt;davidlam&lt;/SPAN&gt;  for stepping in! Much appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 19 May 2022 15:12:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/12504940#M4686329</guid>
      <dc:creator>Ivan-Mirisola</dc:creator>
      <dc:date>2022-05-19T15:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/13672901#M4825986</link>
      <description>&lt;P&gt;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?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;entity Student &lt;SPAN class=""&gt;:&lt;/SPAN&gt; cuid&lt;SPAN class=""&gt;,&lt;/SPAN&gt; managed &lt;SPAN class=""&gt;{&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;rollno&lt;/SPAN&gt;    &lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;String&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;5&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;   @mandatory  @Core&lt;SPAN class=""&gt;.&lt;/SPAN&gt;Immutable @assert&lt;SPAN class=""&gt;.&lt;/SPAN&gt;unique&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;name&lt;/SPAN&gt;      &lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;String&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;50&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;  @mandatory &lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;email&lt;/SPAN&gt;     &lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;String&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;200&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt; @mandatory&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
&lt;SPAN class=""&gt;}&lt;/SPAN&gt;

annotate Student &lt;SPAN class=""&gt;with&lt;/SPAN&gt; &lt;SPAN class=""&gt;{&lt;/SPAN&gt;
    name      @assert&lt;SPAN class=""&gt;.&lt;/SPAN&gt;format&lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;'/^[a-zA-Z]+ [a-zA-Z]+$/'&lt;/SPAN&gt;&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    email     @assert&lt;SPAN class=""&gt;.&lt;/SPAN&gt;format&lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'&lt;/SPAN&gt;&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
&lt;SPAN class=""&gt;}&lt;/SPAN&gt;

entity Scorecard &lt;SPAN class=""&gt;:&lt;/SPAN&gt; cuid&lt;SPAN class=""&gt;,&lt;/SPAN&gt; managed &lt;SPAN class=""&gt;{&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;student&lt;/SPAN&gt;   &lt;SPAN class=""&gt;:&lt;/SPAN&gt; Association to Student&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;details&lt;/SPAN&gt;   &lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;String&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;100&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;marks&lt;/SPAN&gt;     &lt;SPAN class=""&gt;:&lt;/SPAN&gt; Integer&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
    &lt;SPAN class=""&gt;grade&lt;/SPAN&gt;     &lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;String&lt;/SPAN&gt;&lt;SPAN class=""&gt;(&lt;/SPAN&gt;&lt;SPAN class=""&gt;3&lt;/SPAN&gt;&lt;SPAN class=""&gt;)&lt;/SPAN&gt;&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
&lt;SPAN class=""&gt;}&lt;/SPAN&gt;

annotate Scorecard &lt;SPAN class=""&gt;with&lt;/SPAN&gt; &lt;SPAN class=""&gt;{&lt;/SPAN&gt;
    @error&lt;SPAN class=""&gt;.&lt;/SPAN&gt;mandatory&lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;'Scorecard.marks.mandatory'&lt;/SPAN&gt;
    marks&lt;SPAN class=""&gt;;&lt;/SPAN&gt;

    @error&lt;SPAN class=""&gt;.&lt;/SPAN&gt;mandatory&lt;SPAN class=""&gt;:&lt;/SPAN&gt; &lt;SPAN class=""&gt;'Scorecard.grade.mandatory'&lt;/SPAN&gt;
    grade&lt;SPAN class=""&gt;;&lt;/SPAN&gt;
&lt;SPAN class=""&gt;}&lt;/SPAN&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Apr 2024 05:55:07 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/13672901#M4825986</guid>
      <dc:creator>KomalM</dc:creator>
      <dc:date>2024-04-17T05:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/14146639#M4919272</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/751908"&gt;@davidlam&lt;/a&gt;&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/744"&gt;@Ivan-Mirisola&lt;/a&gt;&amp;nbsp;, 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..&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jul 2025 04:21:50 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/14146639#M4919272</guid>
      <dc:creator>avijit_khandelwal</dc:creator>
      <dc:date>2025-07-08T04:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: Regarding CAP Java target and custom error messages</title>
      <link>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/14146961#M4919321</link>
      <description>&lt;P&gt;There are specific annotations for input validations that create specific error messages: &lt;A href="https://cap.cloud.sap/docs/guides/providing-services#input-validation" target="_blank" rel="noopener"&gt;https://cap.cloud.sap/docs/guides/providing-services#input-validation&lt;/A&gt; However, there are no annotations to define custom error messages for a field.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;See &lt;A href="https://cap.cloud.sap/docs/java/event-handlers/indicating-errors#target" target="_blank" rel="noopener"&gt;https://cap.cloud.sap/docs/java/event-handlers/indicating-errors#target&lt;/A&gt; for more details.&lt;/P&gt;&lt;P&gt;EDIT:&amp;nbsp;&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/733670"&gt;@avijit_khandelwal&lt;/a&gt;&amp;nbsp;I have to correct myself a bit. Alternatively, to what I said above, we've released in April&amp;nbsp;&lt;A href="https://pages.github.tools.sap/cap/docs/releases/apr25#assert-constraints" target="_self"&gt;assert constraints&lt;/A&gt;&amp;nbsp;as an experimental alpha feature. This might exactly be what you're looking for.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jul 2025 06:58:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/regarding-cap-java-target-and-custom-error-messages/qaa-p/14146961#M4919321</guid>
      <dc:creator>davidlam</dc:creator>
      <dc:date>2025-07-10T06:58:38Z</dc:date>
    </item>
  </channel>
</rss>

