I am using Grails 3. I have the following field:
<g:field id="myVar" name="myVar" type="number" value="${this.myController?.myVar}"/>
Domain class:
class myDomain{
int myVar
static constraints ={
myVar nullable:true, blank:true
}
}
When I try to submit this field, it will not allow a blank answer. I have it set up in my constraints in the domain class that this field can be null and blank. All of the number fields inside of my form are giving me this error, yet I can leave other fields blank.
This is the error message:
Property myVar is type-mismatched
Is there a setting I am missing?
int is a primitive type. It will never be null. You should have Integer. Also you should remove "blank" constraint. It make sense only for String type.
Related
I am a bit new to grails and I'd like to get a clear understanding of how to use 'nullable' and 'blank' constraints in a grails domain class.
An example is;
static constraints = {
name nullable: true
}
static constraints = {
name blank: true
}
static constraints = {
name nullable: true, blank: true
}
What do each of these mean and how best can they be applied?
All properties are not-null by default, so generally the only time you use the nullable constraint is when you want to allow nulls, i.e. nullable: true.
Moreover, by default Grails databinding will convert blank strings to null, which effectively means that blank: false is applied by default (because blanks are converted to null, and nulls are prohibited).
There are some theoretical cases wherein it would be necessary to explicitly specify blank: false, e.g. if a property is set to a blank string after databinding. However, these are very unlikely to occur in practice, so ignoring some edge cases it's reasonable to assume that blank: false, nullable: false are applied by default.
Well, at first you should look into the docs.
Second:
Nullable is already set by default to false. If you want some value to be nullable, then you write name nullable: true.
Nullable means that when creating an object, that value can be left null (nothing inputted).
Blank - when you will create i.e. form for objects param input and you left a field empty, it will save with no errors and accept empty value.
Long story short - Blank is for forms to accept empty. Nullable is for the coded object to be saved without value.
You can also see this post.
In my model I've got a non-nullable DateTime field. I haven't made it a required field. When I leave the corresponding input in the view empty and check for the modelstate I see that the validation fails on this field. It says "Value cannot be empty". Now, I understand that simple values can't be null so they have to be assigned some value. I also understand that making this field nullable will solve the problem. But how can I catch the case when the attempted value is empty for a certain field (just like default model binding does) to show my custom error message instead of the generic one?
public class Person
{
[DataType(DataType.DateTime)]
[Required(ErrorMessage = 'show my custom error message instead of the generic one')]
public DateTime StartDate{get;set;}
}
explicitly specify error messages as strings. Alternatively you can define them within resource files and optionally localize them depending on the language/culture of the incoming user.
I am trying to realize valition on data type. I have used DataAnnotations, but for data type it's not showing customized message
for example when I' am trying enter string data into int typed field. How I can customize messages in this case?
If I had to guess, you sound like you want a custom message to display when validating one or more fields in your model. You can subclass the DataAnnotations.ValidationAttribute class and override the IsValid(object) method and finally setting a custom ErrorMessage value (where ErrorMessage already belongs to the ValidationAttribute class)
public class SuperDuperValidator : ValidationAttribute
{
public override bool IsValid(object value)
{
bool valid = false;
// do your validation logic here
return valid;
}
}
Finally, decorate your model property with the attribute
public class MyClass
{
[SuperDuperValidator(ErrorMessage="Something is wrong with MyInt")]
public int MyInt { get; set; }
}
If you're using out-of-the-box MVC3, this should be all you need to propertly validate a model (though your model will probably differ/have more properties, etc) So, in your [HttpPost] controller action, MVC will automagically bind MyClass and you will be able to use ModelState.IsValid to determine whether or not the posted data is, in fact, valid.
Pavel,
The DataAnnotations DataType attribute does not affect validation. It's used to decide how your input is rendered. In such a case, David's solution above works.
However, if you want to use only the built-in validation attributes, you probably need to use the Range attribute like this:
[Range(0, 10, ErrorMessage="Please enter a number between 0 and 10")]
public int MyInt { get ; set ;}
(Of course, you should really be using the ErrorMessageResourceName/Type parameters and extract out hard-coded error message strings into resx files.)
Make sure to let MVC know where to render your error message:
<%= Html.ValidationMessageFor(m => m.MyInt) %>
Or you can just use EditorForModel and it will set it up correctly.
I don't think this has been answered because I have the same issue.
If you have a Model with a property of type int and the user types in a string of "asd" then the MVC3 framework binding/validation steps in and results in your view displaying "The value 'asd' is not valid for <model property name or DisplayName here>".
To me the poster is asking can this message that the MVC3 framework is outputting be customized?
I'd like to know too. Whilst the message is not too bad if you label your field something that easily indicates an number is expected you might still want to include additional reasons so it says something like:
"The value 'asd' is not valid for <fieldname>; must be a positive whole number."
So that the user is not entering value after value and getting different error messages each time.
In my grails application i use GORM. I want to customize error messages for each class. Imagine i have this class:
class City {
String name
Region regiao
District district
static belongsTo = District
static constraints = {
regiao(blank: false, nullable:false)
district(blank: false, nullable:false)
name(blank: false, nullable:false, unique: true)
}
String toString(){
name
}
}
i want to customize the error messages in "messages.proprieties".
Imagine i want to make an error message for this class. the default error message for unique is the following:
default.not.unique.message=Property [{0}] of class [{1}] with value [{2}] must be unique
My error message will be something like this: ?
packagename.City.not.unique.message= Must be unique !
Please help, i cant get this to work..
Thx in advanced.
EDIT -- turns out that the answer is in the documentation. Each constraint, in the Constraints section, has the property path to use. So for unique its
className.propertyName.unique
but the path varies according to the specific constraint.
ok so it is it. to make sure how message syntax is just check grails documentation, in constrains section. for each type of constrain, at the end there is the corresponding error message.
For example, go: http://grails.org/doc/latest/
The constrains type 'maxSize' error is the following:
Error Code: className.propertyName.maxSize.exceeded
You want to customise below message.
default.not.unique.message=Property [{0}] of class [{1}] with value [{2}] must be unique
I have tried below code it is working .
city.name.unique.error = city name must be unique.
or
city.name.unique.message = city name must be unique.
I'm doing my first experiments with Grails and am looking for a way to have fields represented by a combobox (such as one-to-one domain associations and numbers with a narrow range constraint) to be optional, i.e. there should be an empty entry in the combobox.
How can this be achieved? I've tried both adding a nullable:true constraint and listing the fields in the optionals static property, but neither produces the desired result.
These are my domain classes:
class Customer {
String name
}
class Book {
static optionals = ['year','loanedTo','loanedSince']
static constraints = {
title(blank:false)
author(blank:false)
year(range:1900..new Date().getAt(Calendar.YEAR), nullable:true)
loanedTo(nullable:true)
loanedSince(min:new Date())
}
String title;
String author;
Integer year;
Customer loanedTo;
Date loanedSince;
}
I've found that the nullable:true constraint actually does have the desired effect - however, it does not take effect immediately; you have to restart Grails to see it.
If you've generated your scaffolding code, you'll also have to regenerate it so that the option is present.
I don't think optionals is still supported: http://jira.codehaus.org/browse/GRAILS-472
The tag also has an attribute for a default, "not selected" value: noSelection. You can use it like this to have the drop-down default to "---" instead of your regular values:
noSelection="${['':'---']}"
In the controller, the default value shows up as an empty string, as specified in the first part of the value.