Custom validator key property & currentobject were always returning null when i override Dovalidate method.

Topics: Validation Application Block
Sep 12, 2011 at 1:13 AM

Hi,

I was trying to create a custom validator and the key property & currentobject were always returning null when i override Dovalidate method.

Please advice

Thanks

Subu

 

Sep 12, 2011 at 1:30 AM

Hi Subu,

 

Can you post your custom validator code and your config here? My initial idea is that you are targeting a wrong object.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

Sep 12, 2011 at 1:34 AM

Here is the code..

public class RequiredQuestionValidator:Validator<Question>

{

 protected override void DoValidate(question objectovalidate,object currenttaget,string key,validationresults validationresults)

{

if (objectToValidate ==null)

{  

this.logvalidationresult(validationResults,defaultmessagetemplate,currentarget,key);

}

}

 

}

Sep 12, 2011 at 1:51 AM

Can you also post your config here? 

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 12, 2011 at 2:07 AM

I am trying to invoke this programmatically in asp.net mvc2 as a part of model binder

var QuestionValidator = new RequiredQuestionValidator();

ValidationResults = questionValidator.Validate(model.myQuestion);

where model.myQuestion is an instance of question class

 

Thanks

Subu

Sep 12, 2011 at 3:11 AM

Subu,

Can you also post the stack trace of your error?

Sep 12, 2011 at 3:13 AM

When i try t debug itself the key and currenttaget are coming as null. what could be the reason. do you want me to include the validation application block and debug ?

Sep 12, 2011 at 3:16 AM
subramn wrote:

When i try t debug itself the key and currenttaget are coming as null. what could be the reason. do you want me to include the validation application block and debug ?


Yes, that would actually help a lot. Can you also post the stack trace please?

Sep 13, 2011 at 2:00 AM

I tried to debug, no matter the class i use,it always sends null to key parameter from validator class, it always calls out the following method

public validationResults Validate(object target)

{

Do Validate(target,target,null,validationresults)

}

How to force the key to be populated?

 

Thanks

Sep 13, 2011 at 2:50 AM

subramn,

Can you post the stack trace, please? The stack trace is very important for me to help you with your error.

Sep 13, 2011 at 4:38 AM

Hi,

The key parameter contains the name of the member that is associated with the validator. It is null if the validator is defined at the type level which is in your case.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

 

Sep 13, 2011 at 11:52 AM

Ok thanks. My requirement is slightly different I am using this along with asp.net mvc where in the user does not answer the question in screen , the question type will be null and i want to trigger an Required field edit.

How can i acheive this using validation application block?

Sep 14, 2011 at 8:20 AM

I guess this blog will provide you an idea. Hope this helps.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

 

Sep 14, 2011 at 11:58 AM

Thanks. our main issue is the key is coming null. the approach works fine.