0
votes

I'm using Oracle 18c and Apex 19.1. I'm trying to create a page validation process in APEX. I have 4 Author fields each with a first name and last name for a total of 8 author-related fields. I'd like to prevent the user from filling in only the last name, and leaving the first name blank. I'm hoping I can put the validation of the four fields into one validation process. I created the process as type PL/SQL Function Body (returning Boolean). I use the following code:

BEGIN
    IF       :P133_AUTHOR1_LAST_NAME is not null AND :P133_AUTHOR1_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author1 LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
             RETURN FALSE;
    ELSIF    :133_AUTHOR2_LAST_NAME is not null AND :P133_AUTHOR2_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author2LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
             RETURN FALSE;
    ELSIF    :133_AUTHOR3_LAST_NAME is not null AND :P133_AUTHOR3_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author3LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
             RETURN FALSE;
    ELSIF    :133_AUTHOR4_LAST_NAME is not null AND :P133_AUTHOR4_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author4LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
             RETURN FALSE;

    ELSE
        RETURN TRUE;
    END IF;
END;

It checks the first If/Then and shows the text in Error Message when appropriate. However the code ignores the remaining 3 ELSIF statements. How do I construct the IF/Then/ElsIf statements so that it will RETURN FALSE when appropriate?

3
user3138025, is this still an issue? I see you were chatting with Littlefoot but then things went dead...Dan McGhan

3 Answers

0
votes

Why reinventing the wheel? Set all those items to be required.


If you insist on your own validation, slightly rewrite it so that your code contains 4 separate IF - END IFs, i.e. (pseudocode)

if :author1_first is null or :author1_last is null then
   error
end if;

if :author2_first is null or :author2_last is null then
   error
end if;

etc.

How to collect all messages:

declare
  l_msg varchar2(500);
begin
    if :author1_first is null or :author1_last is null then
       l_msg := l_msg || 'First '; 
    end if;

    if :author2_first is null or :author2_last is null then
       l_msg := l_msg || 'Second ';
    end if;

    -- etc.

    l_msg := case when l_msg is not null then l_msg || ' author''s first and/or last name are missing';
                  else null
             end;
end;
0
votes

The problem was a typo. I was missing the "P" after the ":" on the ELSIF statements 2-4. Once I fixed the typo's, it worked fine. The corrected code is below.

BEGIN
    IF       :P133_AUTHOR1_LAST_NAME is not null AND :P133_AUTHOR1_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author1 LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
    END IF;
    IF       :P133_AUTHOR2_LAST_NAME is not null AND :P133_AUTHOR2_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author2 LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
    END IF;
    IF       :P133_AUTHOR3_LAST_NAME is not null AND :P133_AUTHOR3_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author3 LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
    END IF;
    IF       :P133_AUTHOR4_LAST_NAME is not null AND :P133_AUTHOR4_FIRST_NAME IS NULL 
    THEN
             :P133_AUTHOR_ERROR_MSG := 'Author4 LAST name is populated but the FIRST ' || 
             'name is blank.  Please populate both names for a given author.';
    END IF;

    If :P133_author_error_msg is null
    Then    
              Return TRUE;
    Else
              Return FALSE;
    End If;
END;
0
votes

The problem was actually a data problem. I didn't notice it right away. The code that I posted originally actually works fine. Thanks for the responses.