Make inherited constraints stricter

69 Views Asked by At

In Grails when using CommandObjects or DomainClass how can I restrict a constraint of an inherited property?

Say I have a parent class with non-null property payload:

abstract class TextContentCommand extends ContentCommand {

    String payload

    static constraints = {            
        payload nullable: false
    }

In the subclass I'd like to make the property stricter and set the max length:

class FacebookTextContentCommand extends TextContentCommand {

    public static final int LENGTH_MAX = 4

    static constraints = {
        importFrom TextContentCommand
        payload maxSize: LENGTH_MAX
    }
}

This way is not working and when a longer string is provided the validation passes. My knowledge of Grails is very superficial. How can I restrict the inherited properties?

1

There are 1 best solutions below

0
On

The constant LENGTH_MAX was causing the problem. Providing the value directly makes the validation work again.

class FacebookTextContentCommand extends TextContentCommand {

    static constraints = {
        importFrom TextContentCommand
        payload maxSize: 4
    }
}