Grails: Why "g:if" around fields in show template? - grails-2.0

The default show template places an g:if test="${domainInstance?.fieldName}" around each domain class field. The problem is that Boolean fields which have a 'false' value are not displayed on the page. What is the purpose of the g:if test?

I personally use the g:if option to check if a field is null to avoid null pointer exceptions. You are right though that for a Boolean field that is not null but has a value of false this would return false. I feels a bit like a "feature" of the scaffolding that grails does.

Related

How to force checkbox type in Symfony sending false value if it is't checked?

Here is a list of all options in checkbox type https://symfony.com/doc/current/reference/forms/types/checkbox.html#empty-data
But I couldn't find exactly what I want.
Are you trying to make a GET request? In this case, false is translated as '' (empty space).
Are you trying to make a POST request? Good. Is your formed linked to an entity / model?
2.1. If yes, just set that property as bool and by default, if is not marked it will be false.
2.1. If no, after request get your property like this: $form->get('property_name')->getData() and validate it with empty() function.
More details here: https://symfony.com/doc/current/forms.html#processing-forms

ASP.NET Web Pages 2 Select Value Validation

I figure this must be so simple and I'm missing something really obvious. I want to validate the selected value of a select input on an ASP.NET Web Pages 2 form using the built in validators but it doesn't look possible so far.
For example:
Validation.Add("my-select", Validator.ValueEquals("Some Value"));
Where Validator.ValueEquals would compare the selected value to the supplied parameter value "Some Value". I realize I could do:
if(Request["my-select"] != "Some Value") {
Validation.AddFormError("Invalid option selected");
}
But then I don't have the error message associated with the field and it will only appear if I'm rendering the validation summary at the top of the form.
What am I missing?
I've cracked the code! Unfortunately the solution is outside of the scope of the built in Validation and Validators static methods but I was able to achieve what I needed by using the following in place of the using the Validation class.
if(Request["my-select"] != "Some Value") {
ModelState.AddError("my-select", "Invalid option selected");
}
Then check if the ModelState is valid when checking Validation.IsValid():
if(ModelState.IsValid && Validation.IsValid()) {
// More codes...
}
It's kind of cool that ModelState is available but it really seems clunky that this isn't handled by the Validation and Validator classes.
You could use the EqualsTo validator and compare the supplied value to a hidden field value, or if you are worried that users might tamper with the hidden field value, you can write your own custom validator. I've blogged how to do that: http://www.mikesdotnetting.com/Article/195/ASP.NET-Web-Pages-Creating-Custom-Validators

Auto validation of properties in Caliburn.Micro

So I'm messing around a bit with Caliburn.Micro, and suddenly I notice something interesting.
I have a ViewModel property called Maximum of type int, auto bound with CM via the naming convention, to a TextBox.
When I enter something that is not and integer, i.e. a character, the textbox' border turns red, and the setter of the property is not called.
Is this an auto-feature of CM?
No, this is the behaviour of WPF. One option is to bind to a string property on your view model, and then perform the validation within the view model (i.e. parse to an int, and provide a default value if the parse fails).

How to check constraints on dexterity content types fields

I want to check during edit form saving process the value of a field verify some constraints
(understand calling a method where I can invalidate the form action)
The field must be defined via schema (not supermodel), otherwise the field isn't visible in the schema. Once the field is defined in the schema, you may use a decorated function like the following to set a field validator:
#form.validator(field=IMySchema['title'])
def validateTitle(value):
if value == value.upper():
raise schema.ValidationError(u"Please don't shout")
I'm pretty sure you can do this with a filesystem code dexterity type using zope.interface invariants.
Take a look at the Dexterity Developer Manual, on the chapter dedicated to validators.

How would you validate a checkbox in ASP.Net MVC 2?

Using MVC2, I have a simple ViewModel that contains a bool field that is rendered on the view as a checkbox. I would like to validate that the user checked the box. The [Required] attribute on my ViewModel doesn't seem to do the trick. I believe this is because the unchecked checkbox form field is not actually transmitted back during the POST, and therefore the validation doesn't run on it.
Is there a standard way to handle checkbox "required" validation in MVC2? or do I have to write a custom validator for it? I suspect the custom validator won't get executed either for the reason mentioned above. Am I stuck checking for it explicitly in my controller? That seems messy...
Any guidance would be appreciated.
Scott
EDIT FOR CLARITY: As pointed out in comments below, this is a "agree to our terms" type of checkbox, and therefore "not checked" is a valid answer, so I'm really looking for an "is checked" validation.
a custom validator is the way to go. I'll post my code which I used to validate that the user accepts the terms ...
public class BooleanRequiredToBeTrueAttribute : RequiredAttribute
{
public override bool IsValid(object value)
{
return value != null && (bool)value;
}
}
I usually use:
[RegularExpression("true")]
If you didn't want to create your own custom validator and still wanted to use existing attributes in the model you could use:
[Range(typeof(bool), "true", "true", ErrorMessage="You must accept the terms and conditions.")]
This ensures that the range of the boolean value is between true and true. However, whilst this method will work, I would still prefer to use a custom validator in this scenario. I just thought i'd mention this as an alternative option.
I too am looking for a way to have the model binder correctly handle check boxes with Boolean values. In the mean time I'm using this in the Actions:
Object.Property = !String.IsNullOrEmpty(Request.Form["NAME"]);
Maybe this will be of some use to you.

Resources