Skip to Content

Mark field as mandatory in Fiori app through CDS annotations

I've created a Fiori app in Web IDE using the list report template based on CDS views.

I want to make a few of the fields mandatory when editing a record. I've added the annotation:

@ObjectModel.mandatory: true

for each of the fields I want to be mandatory on my business object CDS view.

The result of this annotation on the app is that I see a red asterisk next to the field. However, nothing really happens if I edit the record and hit Save with the mandatory field being blank - I am able to successfully save the values even if the mandatory fields are empty.

Am I missing something? I would expect to get an error for the mandatory fields.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Nov 01, 2018 at 12:38 AM

    Hello Kelly,

    Please go through this thread.

    Thank You,

    Sai

    Add comment
    10|10000 characters needed characters exceeded

    • Thank you for your reply.

      Unfortunately I've already looked at the thread mentioned and it does not resolve my issue. The thread is about a mandatory search field, and what I am looking for is to make editable fields in the object page as mandatory

  • Nov 01, 2018 at 04:58 PM

    Sorry, I have misunderstood your requirement. I have tried this a few months back and I recollect the annotation in CDS only marks field with an asterisk. I had to explicitly code my validations in the controller.js.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 01, 2018 at 06:04 PM

    Hi,

    You would have to check through the controls - I do this by passing through a list of controls to the below function and then iterate through, in the case below, the input controls (convention based checking based on my control names - i.e. they start with 'input'.

    If they have no value I set their state to error and return false defining whether the action should proceed.

    There are properties you can set on the control itself as well to validate other aspects.

    Let me know if you need more detail.

            CheckForNoInput: function(controls){
                jQuery.each(controls, function (i, control) {
                    if (control.getId().startsWith("input")){
                        if (!control.getValue()) {
                            control.setValueState(sap.ui.core.ValueState.Error);
                        }
                    }
                 });
            }
    
    Add comment
    10|10000 characters needed characters exceeded

  • Jan 10 at 04:17 PM

    Hi,

    I have the same issue. Using nullable="false" for my OData property, everything works as expected (i.e value state is set to Error and the request is not sent when the field is empty) but this solution is not dynamic. Using the field-control (i.e mandatory) annotation, the field is marked as mandatory in the UI but the form data is not checked and the request is submitted even if the required field is empty.

    How can we check the form data before sending the request using the List Report + Object Page template?

    Cheers,

    Pierre

    Add comment
    10|10000 characters needed characters exceeded