Closed matmiranda closed 7 years ago
As denoted in the docs, AssertThat
is what works for non-nullable fields.
@jwaliszko The problem has disappeared, but the error message is not displayed in view. Nothing happens.
@jwaliszko I updated my post, please look at the third attempt.
Are you sure AssertThat("test == false")
is what you need? From the context of how you wanted to use RequiredIf
, I assume AssertThat("test")
is simply enough. Message will appear when validation is triggered (e.g. on submit). Do you need client-side validation as well? If so, are the respective scripts attached on the page in specifically denoted order? Have you faced any errors flushed to the web console?
Please take a look at the MVC sample project (and the docs). I believe you can dig into helpful information there with a little effort (otherwise please give me a shout).
@jwaliszko
Are you sure AssertThat("test == false") is what you need? Yes
Do you face any errors on the web console? No
Do you need client-side validation as well? Yes
If so, are the scripts attached on the page? Yes
I already did test with string works perfectly.
The test I did:
"stringempty == null"
-----> Works right.
I have 4 checkbox, one of them must be checked, that is my need. How can I do this ?
I can not work with bool
type. Help me.
[AssertThat("test1 || test2 || test3 || test4", ErrorMessage = "check at least one")]
public bool test1 { get; set; }
public bool test2 { get; set; }
public bool test3 { get; set; }
public bool test4 { get; set; }
@Html.ValidationMessageFor(model => model.test1)
@Html.CheckBoxFor(model => model.test1)
@Html.CheckBoxFor(model => model.test2)
@Html.CheckBoxFor(model => model.test3)
@Html.CheckBoxFor(model => model.test4)
Just an example above. You can apply this attribute to each prop (e.g. to have it all highlighted in the view). You can also print the validation message for every prop if you wish.
Tank you, solved problem.
You're welcome.
Please check link of stackoverflow:
http://stackoverflow.com/questions/44076590/requiredifattribute-has-no-effect-when-applied-to-a-field-of-non-nullable-value
Code:
Model:
View:
First attempt:
public bool? test { get; set; }
Error:
Second attempt:
@Html.CheckBoxFor(x => x.test ?? false)
Error:
Three attempt:
Model:
View:
Third attempt, nothing happens! What did I do wrong ?
Any Solution ?