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 Kudos
1,690

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
davidlam
Advisor
Advisor

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

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert

Thanks davidlam for stepping in! Much appreciated.

KomalM
Explorer
0 Kudos

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;
}