views:

287

answers:

1

i have an asp.net mvc project with xval and data annotations and i need to switch to nhibernate validation

with data annotations i had an DataAnnotationsValidationRunner and i was doing something like this

  var errors = DataAnnotationsValidationRunner.GetErrors(this).ToList();

            if(errors.Any())
                throw new RulesException(errors);

how do you do that with nhibernate.validator ?

UPDATE: I saw something like this

 var engine = new ValidatorEngine();
            var errors = engine.Validate(objstovalid)

but i cannot do

if (errors.Any())
                throw new RulesException(errors);

because errors is not of the correct type (xVal.ServerSide.ErrorInfo)

+1  A: 

As far as I know xVal used to provice a runner for NHibernate Validation, but it only worked on a previous version. To my knowledge there is no runner available for the current NHV version.

Just to clarify, do you still want to use xVal? If not then ignore the above, and run the validation on NHV like this:

var validator = new ValidatorEngine();
InvalidValue[] values = validator.Validate(theEntityYouWantToValidate);
UpTheCreek
yes i still want to use xval, i dont' want to use System.ComponentModel.DataAnnotations
Omu
I guess i don't need xval at the server side at all, could you please tell me how to throw a hibernate rule exception
Omu
where do i find these hibernate exceptions, which namespace
Omu
The NHibernate ValidatorEngline doesn't throw exceptions as far as I know. It just returns an array of InvalidValues. You should be able to find ValidatorEngine and InvalidValue types in either the NHibernate.Validator or NHibernate.Validator.Engine Namespaces.
UpTheCreek
So, to clarify, rather than dealing with an exception, you would just check the size of the invalidvalues array returned - if greater than z, loop through it and add the errors to your modelstate.
UpTheCreek
how about business rules validation rules which are not detected by the validator.Validate
Omu
You would run the validator, move any errors to the modelstate, then run your business rule validations (which might just be custom methods, or something else), and add any errors from that to the modelstate also.
UpTheCreek
i managed to make nhiberante work with xval, my current problem is that client side validation for datetime (future / past) doesn't occur
Omu
Yes, I becuase the xVal runner for NHV doesn't fully support the new version. I imagine this will be updated soon though (if the project is continued). By the way, just interested in your reasons for moving from DataAnnotations?
UpTheCreek
could you tell me how to correctly add the hibernate's InvalidValue to the ModelState ?
Omu
well, i turned off javascript and the server side validation for datetime doesn't work with data annotations
Omu
ok, so i found out how to add them to the ModelState, now i have another problem, when i turn off javascript the messages aren't displayed near each field (just an *), and they are displayed in the summary cuz i add the errors to the ModelState; and i have something like this <%= Html.ValidationMessage("ClientName", "*") %> near each field, i understand that if i would change the * with a message it would appear, but would like to appear the message from the annotations, somehow automatically
Omu
You don't need the second param in the ValidationMessage Helper. It should display the message that is contained in your modelstate error collection. Check that you (or the library) is adding this info. E.g. modelState.AddModelError(...
UpTheCreek
everything is ok now, i just had something like .UseValidationSummary("summaryid") in the View, i removed it and it's working; so i'm using now data annotations validation, not hibernate, looks ok
Omu