cancel
Showing results for 
Search instead for 
Did you mean: 

Input field not being highlighted on error

David21
Explorer
344

Hello everyone,

I'm using CAP Node.js(odata v4) and for frontend I have used a list report & object page template with fiori elements. The issue is that when i throw an error on invalid user input, the input field on the object page isn't  being highlighted. It does throw the error successfully but for some reason doesn't highlight the target. I have used the following code in the before(Create) handler : 

 if(req.data.name === null || req.data.name === ''){
            req.error({
                message: 'Name cannot be empty',
                target: 'name',
                status: 422
            })

        }

 

pic.png

View Entire Topic
Arley
Product and Topic Expert
Product and Topic Expert

The reason why the text input field is not being highlighted might be due to how the error target path is specified. Here are a few points to consider:

Correct Target Path: When calling req.error(...), ensure the target path is correct. The target should be a relative resource path correlating the error message with the input field. In draft scenarios, by convention, the target usually has the prefix in. For example:

req.error({
    message: 'Name cannot be empty',
    target: 'in/items(ID=6,IsActiveEntity=false)/name',
    status: 422
})

Or:

req.error({
    message: 'Name cannot be empty',
    target: 'in/name',
    status: 422
})

If the target path—also commonly named in SAPUI5/Fiori Elements as the binding path—is not specified correctly, the error message might not be propagated to the Message Popover correctly, resulting in a poor user experience. Therefore, it is crucial to ensure that the target path aligns with the field's binding path in the front end.

Mandatory/Required Fields: You could consider annotating the text input field as required in the model, or you could try the @mandatory annotation in your CDS model to mark fields as mandatory. This can help with validation and ensure that the UI framework correctly understands which fields are required. CAP provides generic features for data validation. If you haven't already, you can explore CAP's generic data validation features to ensure you leverage all available capabilities.

Additional Information:

David21
Explorer
0 Kudos
Appreciate the answer, It works now with the target written as 'in/name'. Is there any documentation relating to the use of prefix 'in', what is it used for ?
Arley
Product and Topic Expert
Product and Topic Expert
0 Kudos

The in prefix in your example is related to the action metadata of the associated action, where the in parameters are defined in an EDMX (Entity Data Model XML) file. This is commonly used in OData services to define the path of the entity set on which an action operates.

https://cap.cloud.sap/docs/releases/archive/2023/jan23#cds