Vaadin 7 on Grails 2.x
  • Introduction
  • Project setup
    • Command Line
    • IntelliJ IDEA
    • Eclipse
    • NetBeans
    • Plugin Configuration
    • Clean up
    • Best Practices
  • Database
    • GORM
      • Create Domain Model
      • Transactions
      • LazyInitializationException
      • Open Session In View I.
      • Open Session In View II.
      • Table Container
    • Groovy SQL
      • Create Sql
      • Execute SQLs
    • MyBatis
      • Configuration
      • Basics
    • JdbcTemplate
      • Create Beans
      • Usage
    • Clean Up With Alternatives
  • Architecture
    • Model View Presenter
  • Spring Autowiring
    • Create Simple Application
    • Application With Navigator
  • UI
    • Re-using GORM Validations
    • Async Push
    • Multiple application
    • SASS Compilation
    • Widgetset Compilation
  • Spring Security
    • Spring Security Dependency
    • Spring Security Basics
    • Secured Navigator
  • Localization
    • Localization Basics
    • Custom MessageSource
  • REST
    • Without using root URL
    • Using root URL for Vaadin app
  • Plugin development
    • Github
    • Development
Powered by GitBook
On this page

Was this helpful?

  1. UI

Re-using GORM Validations

PreviousUINextAsync Push

Last updated 5 years ago

Was this helpful?

Example code is available on .

When we define validation rules, constraints, in domain class we probably do not want to repeat that validation rules in Vaadin code. We would rather re-use it in Vaadin code for validation of input fields.

We can get constraints from a domain class and use it. Because Vaadin provides many validator, we can use them and fill it with what we have defined in constraints for a domain class.

Assume we have a product, domain class, that has a name. The product name needs to fulfil certiain validation rules. The name cannot be blank, it has too be more than 2 characters long and maximum lenght is 255.

class Product {

    String name

    static constraints = {
        name(blank: false, minSize: 2, maxSize: 255)
    }
}

Then we can get the constraints in Vaadin code and use it as parameters for validation.

Product product = new Product()
product.name = "iPhone 7"
product.save()

FormLayout form = new FormLayout()
FieldGroup binder = new FieldGroup(new BeanItem(product))

TextField txnName = binder.buildAndBind("Name", "name")
txnName.setNullRepresentation("")

def constraints = Product.constraints
def name = constraints.name

int minSize = name.minSize
int maxSize = name.maxSize
boolean blank = name.blank

StringLengthValidator validator = new StringLengthValidator("Validation failed", minSize, maxSize, blank)
txnName.addValidator(validator)

form.addComponent(txnName)
github.com/vaadin-on-grails/gorm-validations