1

I'm trying to prevent users crashing the create new product apex page. On the create page i have a text field:product_name and a numeric field: product_quantity.

Currently when they enter text in the product_quantity field and click 'Save' they get the following error:

Error processing validation.

ORA-01722: invalid number

I have investigated the error however i thought in Apex, if you selected a numeric field, it would detect whether the user entered text or numeric characters?

Is there a method to display a validation message if the user has entered text, it shouts, otherwise it enables the user to save the new entry?

UPDATE

I know why its happening but dont know how to solve it.

I recreated my page and it worked. I then added two pieces of validation in my page processing and when i then try it i get the error in my intial post. If i disable them it works again. The validation use NOT EXISTS to find whether the entered value already exists in the table before they add it.

If only the validation kicks in after looking whether a numerical value has been entered. I stopped the validation looking at an associated item, and turned off the 'when button pressed' but still no joy.

select 1 from MY_TABLE where column_name = :P6_TEXT_FIELD

Is there a way to run the text box validation (checking whether its text entered) before the validation i have created in the page processing?

5
  • I wonder if you accidentally deleted the validation. Could you try recreating the page and see if the problem persists? Commented Mar 1, 2013 at 22:35
  • Is the field defined as a text field or a number field? Are numbers entered with . or , ? Commented Mar 2, 2013 at 10:49
  • @Tom It was a text field but I hen said to myself the database column is numeric, and the values entered are numeric so technically the field should be a numeric field. The numeric validation works on a field on another page perfectly fine. This is why I'm abit lost with this one. Ill try what Matthew suggests tomorrow. Commented Mar 3, 2013 at 20:11
  • @Tom Ive found whats causing the issue (see updated post), i wondered if you knew a way of getting round it? thanks Commented Mar 4, 2013 at 9:35
  • @MatthewMoisen I tried what you suggested and I've found whats causing the issue, not too sure how it can be resolved. Commented Mar 4, 2013 at 9:40

1 Answer 1

1

That's the thing with validations: they all get executed and do not short-circuit. You can actually clearly see this happening when you debug the page.
In the case of a number field, you'd see it does not pass the number validation. But this does not stop validation. So your second validation will be run which uses the submitted value, but would obviously fail when you entered text for instance.


There are some work-arounds for that. For example, you could change your NOT-EXISTS validation to a PLSQL function returning an error message and execute something like this (example):

DECLARE
  v_test_nbr NUMBER;
  v_check_exists NUMBER;
BEGIN
  BEGIN
    v_test_nbr := to_number(:P6_TEXT_FIELD);
  EXCEPTION
  WHEN OTHERS THEN
    -- or catch 1722 (invalid number) and 6502 (char to number conversion error)
    v_test_nbr := NULL;
  END;

  IF v_test_nbr IS NOT NULL
  THEN
  -- if v_test_nbr is not null then the field should be numerically valid
  -- if it isn't then this code would be skipped and this validation
  -- will not throw an error.
  -- However, the previous validation will still fail when text is entered, 
  -- so this shouldn't matter.
    BEGIN
      SELECT 1
      INTO v_check_exists
      FROM my_table
      WHERE column_name = :P6_TEXT_FIELD;
    EXCEPTION 
    WHEN no_data_found THEN
      v_check_exists := 0;
    END;

    IF v_check_exists = 1 
    THEN
      RETURN 'A record with this key already exists';      
    END IF;
  END IF;

  RETURN NULL;
END;

HOWEVER - in this case, where you want to check for duplicate entries a better option may exist if you are at least on version 4.1. If your table has the correct constraints defined, then you would have a unique key defined on the field you are performing the not-exists on. This means that if you would not have this validation on the field, you would get an ora-00001 DUP_VAL_ON_INDEX error.
You could then use the error processing provided by apex to catch this error, and produce a user-friendly message.
You can find an example of how to use and implement this on the blog of Patrick Wolf of the apex development team:
apex-4-1-error-handling-improvements-part-1/
apex-4-1-error-handling-improvements-part-2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.