I need a combination of requiredIf and greaterthanorequalto.

Oct 22, 2012 at 11:02 PM

I have counter than must be equal to or greater than 1, if the adjacent radio button is clicked.  Is there already a way to take care of this with existing foolproof?  If not, can you send me a link to a good tutorial on how to extend the requiredIf attribute to cover this?  Thank you.

Coordinator
Oct 23, 2012 at 1:09 PM

More complex validation is taken care of by create custom validators, here is how: 

http://www.nickriggs.com/posts/build-model-aware-custom-validation-attributes-in-asp-net-mvc-2/

Oct 23, 2012 at 3:05 PM
Hi, thank you for your response. Will this solution also work for MVC 3?
Coordinator
Oct 23, 2012 at 3:07 PM

Sure, it's basically exactly how a lot of the built-in foolproof validators are written. 

Oct 23, 2012 at 3:11 PM
Excellent, and it looks really straight forward. Do I need to do anything else to tie it into the unobtrusive validation I am using on the client side?

Simon

On Tue, Oct 23, 2012 at 7:07 AM, nickriggs <notifications@codeplex.com> wrote:

From: nickriggs

Sure, it's basically exactly how a lot of the built-in foolproof validators are written.

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Oct 23, 2012 at 3:15 PM

You have to create the client side scripts if you want client side validation:

http://www.nickriggs.com/posts/client-side-model-aware-validation/

That article was pre-unobtrusive. So you will also have to do some rigging for that. Refer to the $Unob.adapters.add function calls near the bottom of this file for examples: 

http://foolproof.codeplex.com/SourceControl/changeset/view/94877#1749774

Oct 23, 2012 at 3:16 PM
Great, thank you so much Nick, I have been banging my head against this for a couple days now. Have a great day!

Simon.

On Tue, Oct 23, 2012 at 7:15 AM, nickriggs <notifications@codeplex.com> wrote:

From: nickriggs

You have to create the client side scripts if you want client side validation:

http://www.nickriggs.com/posts/client-side-model-aware-validation/

That article was pre-unobtrusive. So you will also have to do some rigging for that. Refer to the $Unob.adapters.add function calls near the bottom of this file for examples:

http://foolproof.codeplex.com/SourceControl/changeset/view/94877#1749774

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Oct 23, 2012 at 4:06 PM
Nick,

I have been reading over the source code at Foolproof.Codeplex and I am wondering if I should be inheriting from ContingentValidationAttribute, instead of ModelAwareValidationAttribute. I am wondering because both the Is and the RequiredIf classes inherit from ContingentValidationAttribute and those are the attributes I am trying to combine.

What say you?

Simon

On Tue, Oct 23, 2012 at 7:16 AM, Simon Rodgers <teahou@gmail.com> wrote:
Great, thank you so much Nick, I have been banging my head against this for a couple days now. Have a great day!

Simon.


On Tue, Oct 23, 2012 at 7:15 AM, nickriggs <notifications@codeplex.com> wrote:

From: nickriggs

You have to create the client side scripts if you want client side validation:

http://www.nickriggs.com/posts/client-side-model-aware-validation/

That article was pre-unobtrusive. So you will also have to do some rigging for that. Refer to the $Unob.adapters.add function calls near the bottom of this file for examples:

http://foolproof.codeplex.com/SourceControl/changeset/view/94877#1749774

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com



Oct 23, 2012 at 5:58 PM
Hello Again,

Are you starting to regret answering my initial email yet? ;-)

I have put this together based off your post:

public class RequiredIfTrueAndGreaterThanAttribute : ModelAwareValidationAttribute
{

// Register the new attribute with the Foolproof library
static RequiredIfTrueAndGreaterThanAttribute()
{
Register.Attribute(typeof(RequiredIfTrueAndGreaterThanAttribute));
}


public override bool IsValid(bool radioValue, int counterValue)
{
if (radioValue == true)
{
if (counterValue == 0)
{
return false;
}
else if (counterValue > 0)
{
return true;
}
}
return true;
}
}

I get 2 errors:

1 'EligibityForm.RequiredIfTrueAndGreaterThanAttribute' does not implement inherited abstract member 'Foolproof.ModelAwareValidationAttribute.IsValid(object, object)'

and

2 'EligibityForm.RequiredIfTrueAndGreaterThanAttribute.IsValid(bool, int)': no suitable method found to override.

Any advice?


Simon.



On Tue, Oct 23, 2012 at 8:06 AM, Simon Rodgers <teahou@gmail.com> wrote:
Nick,

I have been reading over the source code at Foolproof.Codeplex and I am wondering if I should be inheriting from ContingentValidationAttribute, instead of ModelAwareValidationAttribute. I am wondering because both the Is and the RequiredIf classes inherit from ContingentValidationAttribute and those are the attributes I am trying to combine.

What say you?

Simon


On Tue, Oct 23, 2012 at 7:16 AM, Simon Rodgers <teahou@gmail.com> wrote:
Great, thank you so much Nick, I have been banging my head against this for a couple days now. Have a great day!

Simon.


On Tue, Oct 23, 2012 at 7:15 AM, nickriggs <notifications@codeplex.com> wrote:

From: nickriggs

You have to create the client side scripts if you want client side validation:

http://www.nickriggs.com/posts/client-side-model-aware-validation/

That article was pre-unobtrusive. So you will also have to do some rigging for that. Refer to the $Unob.adapters.add function calls near the bottom of this file for examples:

http://foolproof.codeplex.com/SourceControl/changeset/view/94877#1749774

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com




Coordinator
Oct 23, 2012 at 6:36 PM

Yes, you should be using ModelAware because it will expose the entire model to you instead of just the property value of the contingent property.

You can't change the signature of the IsValid, it has to implement it as it exists in the abstract base class. It should look like this in your class:

 

public override bool IsValid(object value, object container)

 

The container variable, will be your model, just cast it to your model's type, like in the example I provided:

var model = (YourModelTypeHere)container;
Once you have typed model, you will have access to the two properties you need to property evaluate if it's valid.

 

Oct 23, 2012 at 7:07 PM
Oh I see, so I need to specify the fields, such as model.Firstname instead of somehow passing it in later. I had hoped to pass it in because I had planned to use this custom attribute on 6 different properties.

Basically I have 6 sets made up of (radio buttons, textboxes, button, counter, table). The user clicks Yes radio, fills in hte textboxes, and clicks the button which increments the counter and moves the textbox values to the table. I think the simplest thing is to put a custom attribute on each counter (which each have different names) that says (if "<enter radio button name> is true, then <this> needs to be > 0).

So is this example I have been using, capable of getting me here, or will I need to write 6 custom attributes, naming specific properties in the model?

Thank you again for all your help on this.

Simon.

On Tue, Oct 23, 2012 at 10:36 AM, nickriggs <notifications@codeplex.com> wrote:

From: nickriggs

Yes, you should be using ModelAware because it will expose the entire model to you instead of just the property value of the contingent property.

You can't change the signature of the IsValid, it has to implement it as it exists in the abstract base class. It should look like this in your class:

public override bool IsValid(object value, object container)

The container variable, will be your model, just cast it to your model's type, like in the example I provided:

var model = (YourModelTypeHere)container;
Once you have typed model, you will have access to the two properties you need to property evaluate if it's valid.

Read the full discussion online.

To add a post to this discussion, reply to this email (foolproof@discussions.codeplex.com)

To start a new discussion for this project, email foolproof@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com


Coordinator
Oct 23, 2012 at 7:38 PM

I think a single custom validator could work for you if you created a model class to represent a row. So a row class that looking something like:

 

class SomethingRowModel{
 
    prop Radio;
    prop TexBox;
    prop Count;
    ect;

}

Then in your main model, Instead of have a bunch of duplicated properties for your six rows, just have six properties for each row of your new SothingRowModel class.

You can then apply the validator to the SothingRowModel once, and it should work for all your rendered rows.