Show TOC

Validating Logical Filenames Entered By the UserLocate this document in the navigation structure

Procedure

  1. Define a constant to use for the logical filename.

    For each parameter for a physical filename in your application create one global constant of type FILEINTERN (logical filename). Set the value of this constant to the logical filename used for filename validation in this context.

    Tip

    CONSTANTS gc_fname TYPE fileintern VALUE 'EXAMPLE_FIN1'.

  2. During PBO, call the function module FILE_LOGFILE_ALIAS_PBO.

    This function module performs the following:

    • It checks whether validation for your logical filename is active.

    • If validation is active, it attempts to change the parameter on your screen to a list box. It fills the list box with the logical filenames allowed in this context.

      Note

      Validation for a logical filename is defined as active if the system administrator has set up the logical filename whitelist to use for validation or maintained aliases.

      AT SELECTION-SCREEN OUTPUT.
        CALL FUNCTION 'FILE_LOGFILE_ALIAS_PBO'
          EXPORTING
            ed_logfile_appl   = gc_logfile
            ed_parameter_name = 'PA_FILE'
          CHANGING
            cd_logfile_param  = pa_file.
      Note

      Some programs provide an input field that can be used for either a physical filename or a logical filename. In such a scenario, you may have to adjust the call above depending on the context. For example, if you do not provide a field name in ed_parameter_name, the field is not converted to a list box. If cd_logfile_param is empty, the function module sets the hard-coded logical filename as a default.

  3. During PAI, call the function module FILE_LOGFILE_ALIAS_PAI.

    This function module performs the following:

    • It checks whether validation for your logical filename is active.

    • If validation is active, and if the user specified or selected a logical filename, the function module checks whether the specified logical filename is allowed in this context. If it is not allowed, it raises the exception EXC_INVALID_FILENAME with a corresponding error message. You must handle this error message according to the context of your application. In most cases, you can use the error message as is.

      Note

      This call is necessary when loading a variant. In this case, the UI automatically adds an entry to the list box containing the value from the variant. Therefore, even when using a list box, it is possible that the user selects a logical filename which has not been defined as a valid alias.

      AT SELECTION-SCREEN.
        CALL FUNCTION 'FILE_LOGFILE_ALIAS_PAI'
          EXPORTING
            ed_logfile_appl            = gc_logfile
          CHANGING
            cd_logfile                 = pa_file
          EXCEPTIONS
            OTHERS                     = 1.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
      Note

      If the parameter pa_file can be used for either physical and logical filenames you, only have to call this function module if the specified filename is a logical filename. The recommended procedure is to call the function module FILE_GET_NAME first.

      If the result from FILE_GET_NAME is that the content of pa_file is a logical filename, call the function module FILE_LOGFILE_ALIAS_PAI as illustrated above.

      If the result from FILE_GET_NAME is that the content of pa_file is not a logical filename, call the function module FILE_VALIDATE_NAME and provide the hard-coded logical filename in the EXPORT parameter logical_filename and the content of pa_file for the CHANGING parameter physical_filename.

  4. Add a call to function module FILE_LOGFILE_ALIAS_F4.

    Use this call to in case the user tries to use the value help for entering a logical filename. This function module performs the following:

    • If the validation for your logical filename is active, only the logical filenames allowed in this context are available with the value help. The user cannot select a logical filename that he or she is not allowed to use when executing the program.

    • If the validation for your logical filename is not active, the function module will start a dialog which allows the user to select any logical filename defined in the system.

      AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file.
        CALL FUNCTION 'FILE_LOGFILE_ALIAS_F4'
          EXPORTING
            ed_logfile_appl   = gc_logfile
            ed_parameter_name = 'PA_FILE'
          CHANGING
            cd_logical_file   = pa_file.
      Note

      If the parameter pa_file can be used for either physical and logical filenames, you need to decide whether you want to provide value help for a logical filename (in that case use FILE_LOGFILE_ALIAS_F4) or a physical filename (for example, by using cl_gui_frontend_services=>file_open_dialog).

  5. Validate the filename.

    Validate the physical filename directly before the OPEN DATASET or related statement.

    CALL FUNCTION 'FILE_GET_NAME'
        EXPORTING
          logical_filename  = pa_file
    * any parameters defined for that logical filename
        CHANGING
          physical_filename = ld_physfile.
     
    CALL FUNCTION 'FILE_VALIDATE_NAME'
        EXPORTING
          logical_filename  = gc_fname
    * any parameters defined for that logical filename
        CHANGING
          physical_filename = ld_physfile
        EXCEPTIONS
          OTHERS            = 1.
     
      IF sy-subrc <> 0.
    * Implement suitable error handling here, for example,
    *    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    *      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.
     
      OPEN DATASET ld_physfile
         FOR INPUT IN TEXT MODE ENCODING DEFAULT.
      IF sy-subrc <> 0.
    * ...
      ENDIF.