Within a Monorail project, I have the following class which uses the Castle.Components.Validator validation attributes:
public class NewProductViewModel
{
[ValidateNonEmpty("Name is a required field")]
public string Name { get; set; }
[ValidateDecimal("Price must be a decimal")]
public decimal Price { get; set; }
[ValidateInteger("SupplierId must be an integer")]
public int SupplierId { get; set; }
}
which gets passed as a parameter in this controller's action:
[AccessibleThrough(Verb.Post)]
public void Create([DataBind("newproductviewmodel", Validate = true)] NewProductViewModel prod)
{
if (!HasValidationError(prod))
{
Logger.InfoFormat("The following NewProductViewModel has been received:{0}", prod.ToString());
var tx = _session.BeginTransaction();
try
{
Logger.InfoFormat("Retrieving supplier with id <{0}> from database", prod.SupplierId);
var supplier = _session.Load<Supplier>(prod.SupplierId);
var newProduct = new Product()
{
Supplier = supplier,
Price = prod.Price,
Name = prod.Name
};
_session.Save(newProduct);
tx.Commit();
RedirectToAction("list");
}
catch (Exception ex)
{
Flash["error"] = ex.Message;
Flash["newproductviewmodel"] = prod;
Logger.ErrorFormat("The following error has occured:{0}", ex.Message);
RedirectToAction("new");
}
finally
{
tx.Dispose();
}
}
else
{
Logger.ErrorFormat("The following NewProductViewModel is invalid:{0}", prod.ToString());
Flash["contact"] = prod;
Flash["error"] = GetErrorSummary(prod);
RedirectToAction("new");
}
}
However, if i set the Price to a value of "fadaadsa" instead of a decimal, the HasValidationError doesn't say anything! Instead, it maps the Price property to 0. I'm not sure what I should do, considering that the documentation pretty much says that this is all there is to it. Thanks in advance.