Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:35:00+00:00 2026-05-27T01:35:00+00:00

I have a validation for big file size in a try block, after throwing

  • 0

I have a validation for big file size in a try block, after throwing that error its again going and checking the null values of the other attributes of static constraints and throwing that error as well.

How to stop the flow after returning the first error?

here is the code

static constraints = {
        applicationName(blank: false, size: 1..25)
        applicationShortName(blank: false, size: 1..10)
        applicationImage(nullable: false, maxSize: MAX_SIZE)
        contentProviderId (
            validator: {
                if (it == 0) {
                    return ['notSelected']
                }
            }
        )
        customErrorMessage (
                validator: {
                    if ("fileToBig".equals(it)) {
                        return ['fileToBig']
                    }
                }
        )
    }

try {
            CommonsMultipartFile file = request.getFile('applicationImageUrl');

            logger.debug("POSTPROCESS: is file empty=${file.isEmpty()}")
            if(!file.isEmpty()) {

                try {


                        -- other logic


                }
                catch (Exception ex) {
                    logger.warn("Failed to upload file - improper file type", ex)
                    return [];
                }

                logger.debug("Getting new image file")
                try {
                    -- logic

                    if (file.size <= MAX_SIZE) {
                        -- logic
                    } else {
                        customErrorMessage = "fileToBig"; ( ERROR FOR BIG FILE SIZE)


                    }
                } catch (Exception e) {
                    logger.warn("Failed to upload file", e)
                    customErrorMessage = "fileToBig";

                }
            } else {
                logger.debug("File was empty. Will check if there is a file in submission")
                if (submission.applicationImage != null && submission.applicationImage != []) {
                    logger.debug("submission contains applicationImage=${submission.applicationImage}")
                    this.applicationImage = submission.applicationImage;
                }
            }
        } catch (Exception e) {
            this.errors.reject("error","An error occured when uploading file. Please try again.");
            logger.error("Failed to upload file", e);
            return [];
        }

        --logic
        if (application != null) { //Application already exists!
            submission.applicationId = application.id;
            return [next: 10];
        }

        return [];
    }

after the big file size error, the application image is not set , so its throwing application image null error as well…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:35:01+00:00Added an answer on May 27, 2026 at 1:35 am

    You can add a custom validator to your applicationImage field, to only check for null value if customErrorMessage field is not null.
    This way you will only get applicationImage error in validation exception if customErrorMessage is not null.

    In validator closure you have access to the value of the field being checked and to the whole object as well:

    myField(validator: { val, obj -> return propertyName == "myField" })
    

    Thus you can do something like this:

    static constraints = {
            applicationName(blank: false, size: 1..25)
            applicationShortName(blank: false, size: 1..10)
            applicationImage(validator: {val, obj ->
                if (obj.customErrorMessage != null) {
                    if (val == null) return ['imageNull']
                    if (val.size() > MAX_SIZE) return ['tooLarge']
                }
            })
            contentProviderId (
                validator: {
                    if (it == 0) {
                        return ['notSelected']
                    }
                }
            )
            customErrorMessage (
                    validator: {
                        if ("fileToBig".equals(it)) {
                            return ['fileToBig']
                        }
                    }
            )
        }
    

    Might need a tweak in error messages but I hope you get my point 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently inherited a VBA macro that needs to have validation logic added to
I have a validation control that has the following expression: (?=(.*\\d.*){2,})(?=(.*\\w.*){2,})(?=(.*\\W.*){1,}).{8,} That's a password
I have two validation groups: parent and child I have an add button that
I have a big validation JS script. I want to put this script to
We have a ASP.NET MVC 3 application that uses unobtrusive jQuery validation. The page
I have a file (image) upload script in PHP that I use to upload
i have a validation in my .net textbox where it will take only numbers
i have a validation in my .net textbox where it will take only numbers
I have a validation function I'm using inside of codeigniter. function valid_image() { if
I have some validation code which should display the max / min of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.