Grails Null Error Message
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have grails custom validation error message Meta Discuss the workings and policies of this site About Us grails custom error messages Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with grails override default error messages us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just
Grails Error Message Format
like you, helping each other. Join them; it only takes a minute: Sign up Grails Problem with custom error messages up vote 12 down vote favorite 4 I am currently trying to specify custom error messages in grails for the default constraints but so far all I get back is the default error message. I know grails messagesource that I have to edit the grails-app/i18n/messages.properties file If I change the following default error codes message, it will correctly display the new error message default.blank.message=Property [{0}] of class [{1}] cannot be blank However, this is not what I am trying to do. I need more granular error reporting and have more than one field that can be blank etc. What I would like to be able to do would be, display custom messages for each field in a class package com.mycompany.myapp class Test{ String name def constraints = { name(nullable:false, blank:false) } } (following codes appended to end of messages.properties) test.name.blank=Name cannot be blank test.name.nullable=Name cannot be nullable According to the grails documentation this should work correctly, either with or without the package name - className.propertyName.blank grails.org/doc/latest/ (constraints section) & (section 7.4 - validation & internationalization) I have tried all comnbinations that I can think of, but it always displays the custom message I have also tried installing the grails i18n templates plugin http://www.grails.org/I18n+Templates+Plugin which gene
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers grails custom validator or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Grails Validation Error Messages
Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it
G:message
only takes a minute: Sign up How to compose Grails validation messages? up vote 0 down vote favorite I have a simple domain entity: package shoesshop class Brand { String name String description String logoImageURL static constraints = { http://stackoverflow.com/questions/3250667/grails-problem-with-custom-error-messages name(blank: false) logoImageURL(nullable: true) } } When I try to save a new brand with null value as its name, I want to render a message which says that 'Name must be specified'. I tryied to add a property to messages.properties: brand.name.nullable=Brand name must be specified But it's not picked up automatically. How should I retrieve it from there? I looked at brand.errors and it contains just a default message Property [{0}] of class [{1}] cannot be null. It also http://stackoverflow.com/questions/10705432/how-to-compose-grails-validation-messages contains a set of error codes, one of which is brand.name.nullable. validation grails error-handling share|improve this question asked May 22 '12 at 15:44 Roman 29.3k50174259 add a comment| 3 Answers 3 active oldest votes up vote 1 down vote Have you tried?: if(brand.name==null) { flash.message = message(code: 'brand.name.nullable',default:'Brand name must be specified.'); render(view: "create", model: [brand:brand]) return } You can try to change the message (don't know how this will perform with your entire app) of "default.null.message" to {1} {0} must be specified share|improve this answer answered May 22 '12 at 19:12 Alex Vargas 13612 add a comment| up vote 1 down vote It's strange to me that the nullable error message is showing up when you don't have the nullable constraint. In the docs for blank and nullable it clearly shows that nullable is different than blank and have separate messages. Try brand.name.blank I also seem to have to add the .error to get things to work correctly: brand.name.blank.error or brand.name.nullable.error share|improve this answer edited May 23 '12 at 5:33 answered May 22 '12 at 21:07 Kelly 3,29741228 it's because that when user submit from the web, "name" would be a blank string instead of null. –Hoàng Long May 23 '12 at 8:13 That's what I'm saying - he is not getting the blank error message like he should when the user submits with a blank field. He is getting the nullable messag
13The Service Layer 14Static Type Checking And Compilation 15Testing 16Internationalization 17Security 18Plugins 19Grails and Spring 20Grails and http://docs.grails.org/latest/guide/validation.html Hibernate 21Scaffolding 22Deployment 23Contributing to Grails Quick Reference << 11Asynchronous Programming (Quick Reference) 13The Service Layer >> 12 Validation Version: 3.2.0 Table https://dzone.com/articles/null-value-save-issue-grails of Contents 12.1Declaring Constraints 12.2Validating Constraints 12.3Sharing Constraints Between Classes 12.4Validation on the Client 12.5Validation and Internationalization 12.6Applying Validation to Other Classes error message 12 Validation Improve this doc Grails validation capability is built on Spring's Validator API and data binding capabilities. However Grails takes this further and provides a unified way to define validation "constraints" with its constraints mechanism. Constraints in Grails are a way to declaratively specify validation error message validation rules. Most commonly they are applied to domain classes, however URL Mappings and Command Objects also support constraints. 12.1 Declaring Constraints Improve this doc Within a domain class constraints are defined with the constraints property that is assigned a code block: class User { String login String password String email Integer age static constraints = { ... } } You then use method calls that match the property name for which the constraint applies in combination with named parameters to specify constraints: class User { ... static constraints = { login size: 5..15, blank: false, unique: true password size: 5..15, blank: false email email: true, blank: <
Guides Zones | Agile Big Data Cloud Database DevOps Integration IoT Java Mobile Performance Web Dev Let's be friends: Null Value on Save Issue in Grails by Marcin Świerczyński · Apr. 12, 10 · Java Zone Like (1) Comment (6) Save Tweet {{ articles[0].views | formatCount}} Views Edit Delete {{ articles[0].isLocked ? 'Enable' : 'Disable' }} comments {{ articles[0].isLimited ? 'Remove comment limits' : 'Enable moderated comments' }} Join the DZone community and get the full member experience. Join For Free Check out this8-step guideto see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround. If you’ve used Grails, you’re probably familiar with a domain class and its “constraints” block. There you can define conditions which have to be met by class’s fields. For example, you can enforce that a field have to be not-empty using “blank: false” condition. It’d be intuitive not to define conditions for the field you don’t care of. Unfortunately, there is a small trap here. Let’s use an example to explain it. Let’s define an Invoice class with a few fields: number, draw date and payment date. The number and draw data are obligatory while the payment date isn’t, because you’re able to pay for an invoice by cash.class Invoice { String number; Date drawDate; Date paymentDate; static constraints = { number(blank: false); drawDate(blank: false); } } It looks good, doesn’t it? You can generate controller and views for that class and test it in a browser. Try to leave a payment date field empty and save an invoice. Success – it works! OK, so let’s create a BootStrap entry for our new class. Again, try to omit its optional field – paymentDate.def invoice = new Invoice(number: “1/2010”, drawDate: new Date()).save(); What value does invoice variable have? Null! What’s wrong? Why does it work in a browser and not directly in a code? The answer is quite straight, but I haven’t found it in a documentation. The default, implicit value of a field’s constraint is “nullable: false”. When you fills in a form in your browser, you really sends a blank value – empty string. This string isn’t null so it meets “n