Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am using Expressive annotations to handle RequiredIf conndition


My Model
[RequiredIf("IsSellable == false", ErrorMessage ="Offer code is required!")]
public string Code { get; set; }
public bool IsSellable { get; set; }

whenever I click on save button client side validation not working.

Note: I have downloaded expressive in my class library Model and my views are in separate project

What I have tried:

All client side validations on my form firing instead of above.
Posted
Updated 25-Sep-20 12:56pm
Comments
Sandeep Mewara 25-Sep-20 16:41pm    
Sounds like value of IsSellable is true thus the validation not getting triggered. Did you check that?
Member 14908411 25-Sep-20 22:14pm    
I have confirmed IsSellable value is false

1 solution

From the Expressive Annotations GitHub : [^]
Quote:
RequiredIf attribute is not working, what is wrong?
Make sure RequiredIf is applied to a field which accepts null values.

In the other words, it is redundant to apply this attribute to a field of non-nullable value type, like e.g. int, which is a struct representing integral numeric type, DateTime, etc. Because the value of such a type is always non-null, requirement demand is constantly fulfilled. Instead, for value types use their nullable forms, e.g. int?, DateTime?, etc.

[RequiredIf("true")] // no effect...
public int Value { get; set; } // ...unless int? is used
[RequiredIf("true")] // no effect...
public DateTime Value { get; set; } // ...unless DateTime? is used
If this doesn't help you, you can open an issue,
 
Share this answer
 
Comments
Member 14908411 25-Sep-20 22:14pm    
But I need to fire client side validation on string property, which is by default nullable
BillWoodruff 25-Sep-20 22:31pm    
So, open a case on the GitHub site and ask if what you want is possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900