Code Snippets for Validations

Required Field Validation – Child Entity

  • Seminar is the root entity
  • Teilnehmer is the child entity
  • Validation is only triggered on main view

Simple Required Field Validation

Behavior Definition

Define the fields as mandatory

   field ( mandatory )
   Title,
   Description,
   Difficulty;

Define a validation for the fields

  validation validateReqFields on save { create; update; field Title, Description, Difficulty; }

Your source code should look like this:

managed implementation in class ZBP_R_JS_RECEIPT unique;
strict ( 2 );
with draft;
define behavior for ZR_JS_RECEIPT alias Receipt
persistent table ZJS_RECEIPT
draft table ZJS_RECEIPT_D
etag master LocalLastChangedAt
lock master total etag LastChangedAt
authorization master( global )
early numbering

{
...

   field ( mandatory )
   Title,
   Description,
   Difficulty;


  create;
  update;
  delete;

  validation validateReqFields on save { create; update; field Title, Description, Difficulty; }

...
  draft determine action Prepare
  {
    validation validateReqFields;
  }
...
}

Behavior Implementation

💡Use quick assist to add method definition in behavior class

💡%element-FIELDNAME = if_abap_behv=>mk-on results into connecting the error message and the field via activeLink in the MessagePopover

CLASS lhc_zr_js_receipt DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.
    METHODS:
      validateReqFields FOR VALIDATE ON SAVE
        IMPORTING keys FOR Receipt~validateReqFields.
ENDCLASS.

CLASS lhc_zr_js_receipt IMPLEMENTATION.
  METHOD validateReqFields.

*--- read necessary data by keys
    READ ENTITIES OF zr_js_receipt IN LOCAL MODE
    ENTITY Receipt
    FIELDS ( Title Description Difficulty )
    WITH CORRESPONDING #( keys )
    RESULT DATA(receipts).

*--- do business logic
    LOOP AT receipts INTO DATA(receipt).
      APPEND VALUE #( %tky = receipt-%tky
                      %state_area = 'VALIDATION' ) TO reported-receipt.
      "check for field Title
      IF receipt-Title IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Title'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Title = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

      "check for field Description
      IF receipt-Description IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Description'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Description = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

      "check for field Difficulty
      IF receipt-Difficulty IS INITIAL.
        APPEND VALUE #( %tky = receipt-%tky ) TO failed-receipt.
        APPEND VALUE #( %tky = receipt-%tky
                            %state_area = 'VALIDATION'
                            %msg = me->new_message(
                             id       = 'ZJS_MESSAGES'
                             number   = '001'
                             v1       = 'Difficulty'
                             severity = if_abap_behv_message=>severity-error
                            )
                            %element-Difficulty = if_abap_behv=>mk-on
                             ) TO reported-receipt.
      ENDIF.

    ENDLOOP.

  ENDMETHOD.

ENDCLASS.

Leave a Reply

Your email address will not be published. Required fields are marked *