Umbraco culture and hostnames affects validation - validation

I've got very wired problem. I have payment page on which I've got amount with custom model. Here is validation for that property:
[Required(ErrorMessage = "Kwota - Pole wymagane!")]
[Display(Name = "Kwota: ")]
[RegularExpression(#"^\d+(\.\d{1,2})?$", ErrorMessage = "Proszę podać poprawną kwotę!")]
[Range(00.01, 99999999999, ErrorMessage = "Kwota musi być większa niż 0.00")]
public decimal Amount { get; set; }
I'm trying to make site in multiple languages so I follow standard umbraco process but once I change language or hostname the above property return:
"The value '1.00' is not valid for Kwota: ."
This doesn't fail if my culture language is inherit and there is no domains added but once I add domain or change culture language it fails. Any idea or reason?
I have checked and it happens on server side as I can hit breakpoint on post action.
I have also noticed that once I change culture and hostnames I'm no longer hitting breakpoint on my custom validation for honeypot witch is on model - I just simply implement IValidatableObject.
Just to clarify I'm running: Umbraco version 7.4.3 assembly: 1.0.5948.18141
Any help very appreciated.

Related

Web application's form validation - design to propagate domain errors to client-side?

Data validation should occur at the following places in a web-application:
Client-side: browser. To speed up user error reporting
Server-side: controller. To check if user input is syntactically valid (no sql injections, for example, valid format for all passed in fields, all required fields are filled in etc.)
Server-side: model (domain layer). To check if user input is domain-wise valid (no duplicating usernames, account balance is not negative etc.)
I am currently a DDD fan, so I have UI and Domain layers separated in my applications.
I am also trying to follow the rule, that domain model should never contain an invalid data.
So, how do you design validation mechanism in your application so that validation errors, that take place in the domain, propagate properly to the client? For example, when domain model raises an exception about duplicate username, how to correctly bind that exception to the submitted form?
Some article, that inspired this question, can be found here: http://verraes.net/2015/02/form-command-model-validation/
I've seen no such mechanisms in web frameworks known to me. What first springs into my mind is to make domain model include the name of the field, causing exception, in the exception data and then in the UI layer provide a map between form data fields and model data fields to properly show the error in it's context for a user. Is this approach valid? It looks shaky... Are there some examples of better design?
Although not exactly the same question as this one, I think the answer is the same:
Encapsulate the validation logic into a reusable class. These classes are usually called specifications, validators or rules and are part of the domain.
Now you can use these specifications in both the model and the service layer.
If your UI uses the same technology as the model, you may also be able to use the specifications there (e.g. when using NodeJS on the server, you're able to write the specs in JS and use them in the browser, too).
Edit - additional information after the chat
Create fine-grained specifications, so that you are able to display appropriate error messages if a spec fails.
Don't make business rules or specifications aware of form fields.
Only create specs for business rules, not for basic input validation tasks (e.g. checking for null).
I want to share the approach used by us in one DDD project.
We created a BaseClass having fields ErrorId &
ErrorMessage.
Every DomainModel derive from this BaseClass & thus have a two extra fields ErrorId & ErrorMessage available from
BaseClass.
Whenever exception occurs we handle exception(Log in server, take appropriate steps for compensating logic & fetch User Friendly message from client location based localized Resource file for message ) then propagate data as simple flow without raising or throwing exception.
At client side check if ErrorMessage is not null then show error.
It's basic simple approach we followed from start of project.
If it's new project this is least complicated & efficient approach, but if you doing changes in big old project this might not help as changes are big.
For validation at each field level, use Validation Application Block from Enterprise Library.
It can be used as :
Decorate domain model properties with proper attributes like:
public class AttributeCustomer
{
[NotNullValidator(MessageTemplate = "Customer must have valid no")]
[StringLengthValidator(5, RangeBoundaryType.Inclusive,
5, RangeBoundaryType.Inclusive,
MessageTemplate = "Customer no must have {3} characters.")]
[RegexValidator("[A-Z]{2}[0-9]{3}",
MessageTemplate = "Customer no must be 2 capital letters and 3 numbers.")]
public string CustomerNo { get; set; }
}
Create validator instance like:
Validator<AttributeCustomer> cusValidator =
valFactory.CreateValidator<AttributeCustomer>();
Use object & do validation as :
customer.CustomerNo = "AB123";
customer.FirstName = "Brown";
customer.LastName = "Green";
customer.BirthDate = "1980-01-01";
customer.CustomerType = "VIP";
ValidationResults valResults = cusValidator.Validate(customer);
Check Validation results as:
if (valResults.IsValid)
{
MessageBox.Show("Customer information is valid");
}
else
{
foreach (ValidationResult item in valResults)
{
// Put your validation detection logic
}
}
Code example is taken from Microsoft Enterprise Library 5.0 - Introduction to Validation Block
This links will help to understand Validation Application Block:
http://www.codeproject.com/Articles/256355/Microsoft-Enterprise-Library-Introduction-to-V
https://msdn.microsoft.com/en-in/library/ff650131.aspx
https://msdn.microsoft.com/library/cc467894.aspx

Overriding .net mvc framework validations?

I have a popupeditform from a grid. I have applied validation rules to the binded model While testing I found that other than user specified validations, some framework generated errors are also coming up.
How can I override those messages
Eg :
This is what I give as validation
[Range(0, 100, ErrorMessage = "Ratio should be between 0 and 100")]
[Required(ErrorMessage = "Ratio is required")]
public double Ratio {get; set; }
During run time I give value "2147483648" which is just above the maximum value. And I am getting error as "Value was either too large or too small for an Int32." If I give a value within permitted range say 2147483647 which is the margin value for int32 then my validation rules gets applied.
So my understaning is that the control comes first to the framework and shows framework validation message first.
In any way can I override the same ?
you mentioned range validation and required validation.If the validation fails due to any other reason it will generated it's message related to error and not your escaped validation message.
you should limit the max length of input. if its double set max length accordingly and similarly for others. so your validation wont have bugs.

ASP.NET MVC 3 not accepting my German date format settings - why?

I have a new ASP.NET MVC 3 project, and MVC and Razor are all brand new to me... can't figure this one out.
I have a dev machine on US-English Win7 x64, running English versions of VS 2010 Pro SP1 and ASP.NET MVC 3. But the app I'm working on is for a local client, and needs to be all in German and use all the formatting and defaults commonly used here.
In my view model, I have defined a variable of type DateTime and augmented it with some extra hints as to how to display (and edit) it - I need the Swiss-German date formatting, which is: day.month.year (in that order, separated by dots . - not slashes)
public class MyViewModel
{
[Display(Name = "Gültig am")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString="dd.MM.yyyy", ApplyFormatInEditMode=true)]
public DateTime ValidAt { get; set; }
}
(of course, there are more properties in that view model, but those aren't relevant just now)
In my Razor view, I simply use the default stuff set up by the ASP.NET MVC 3 Add View T4 template:
#Html.EditorFor(model => model.ValidAt)
Trouble is: if I enter something like 13.06.2011 (13th of June, 2011) which I believe is valid and conforming to my display and editing formats, I am still getting a client-side validation saying that The value '13.06.2011' is invalid - WHY???
There must be something really fundamental (and probably totally silly) that I'm missing here....
Even though I have set all of this up,
What do you use for client validation? Did you look at Combining validation methods in jQuery Validation plugin with "or" instead of "and" ?

wp7 - application.current as app Value can not be null

I put some properties in the App.xaml.cs file which I am using to store data and populate textboxes as I navigate through my application:
public String appRXName { set; get; }
public String appRXNumber { set; get; }
Originally I had a pivot control that called different pages to gather data, but then I moved that pivot control item off to its own page that still calls other pages to collect data. Now when I run the application I get an error.
Basically it was working when I had it inside the original Pivot control. Once I moved it to a separate page (pivot page calles it) then I started to get this error:
System.ArgumentNullException was unhandled Message=Value can not be null. Parameter name: Text
No matter what page I hit always the second item in the list displays the error.
txtRxNotes.Text = (Application.Current as App).appDosageNotes;
txtQuantity.Text = (Application.Current as App).appQuantity.ToString();
I found something online about a RootVisual but I'm not sure if that is what I looking at or not. Does anyone have any ideas?
The ArgumentNullException is being thrown because the value that you are trying to set for the Text property is null, which you cannot do; the Text property is not a nullable type.
Without knowing how and when these application-level properties are being set it's difficult to provide a good explanation of why the behavior is different since your refactor, but you could either:
Put a null check in the code that accesses these application-level properties.
Initialise the application-level properties to string.Empty in the application constructor.

ASP.NET MVC 3 - Client-Validation with money field

I did followed this blog here and here.
And i have the following problem.
One field in my form is currecy format, from brazil.
I'm using client-side validation. Everything works great expept one issue.
I have 2 validation:
[DisplayName("Taxa de adesão")]
[MoedaReal(ErrorMessage = "Taxa deve ser numérico")]
[Required(ErrorMessage = "Taxa é obrigatório")]
public decimal ValorAdesao { get; set; }
The rule REQUIRED works ok, the MoedaReal rule works okay.
After these rules are passed ok, one final rule is triggered:
The field Taxa de adesão must be a number
I already tried to change the web.config in this line:
<globalization culture="pt-br" uiCulture="pt-br" />
My numbers format accepcted are these:
1,00
11,00
111,00
1.111,00
11.111,00
111.111,00
1.111.111,00
1.111.111.111.111,00
How can i "fool" .NET to accept this format? Because it expects DECIMAL format instead.
You can't fool the default model binder. It simply tries to parse the request string value into a decimal using the culture specified in your web.config. So because you are also using client validation there might be a culture difference between the client and the server. For example the browser could be configured to use en-US and the server pt-BR and then you might have a problem. You could try this:
<globalization culture="auto" uiCulture="auto" />
This means that the culture used by the server will be dictated by the client. If this doesn't work you have a couple of other possibilities:
Write a custom model binder
Use string instead of decimal and then do the parsing manually

Resources