jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

RequiredIfAttribute has no effect when applied to a field of non-nullable value type 'System.Boolean' #162

Closed matmiranda closed 7 years ago

matmiranda commented 7 years ago

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:

[Display(Name = "Checkbox name")]
[RequiredIf("test == false", ErrorMessage = "test message error")]
public bool test { get; set; }

View:

<div>
    @Html.LabelFor(x => x.test)
    @Html.CheckBoxFor(x => x.test)
    @Html.ValidationMessageFor(x => x.test)
</div>

First attempt:

public bool? test { get; set; }

Error:

Can not implicitly convert type "bool?" In "bool". Is there an explicit cast (is there a missing cast?)

Second attempt:

@Html.CheckBoxFor(x => x.test ?? false)

Error:

Templates can be used only with expressions of field access, property access, single-dimension array index, or single-parameter standardized indexer.

Three attempt:

Model:

[Display(Name = "Checkbox name")]
[AssertThat("test == false", ErrorMessage = "test message error")]
public bool test { get; set; }

View:

<div>
 @Html.LabelFor(x => x.test)
 @Html.CheckBoxFor(m => m.test)
 @Html.ValidationMessageFor(x => x.test)
</div>

Third attempt, nothing happens! What did I do wrong ?

Any Solution ?

jwaliszko commented 7 years ago

As denoted in the docs, AssertThat is what works for non-nullable fields.

matmiranda commented 7 years ago

@jwaliszko The problem has disappeared, but the error message is not displayed in view. Nothing happens.

matmiranda commented 7 years ago

@jwaliszko I updated my post, please look at the third attempt.

jwaliszko commented 7 years ago

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).

matmiranda commented 7 years ago

@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.

jwaliszko commented 7 years ago
[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.

matmiranda commented 7 years ago

Tank you, solved problem.

jwaliszko commented 7 years ago

You're welcome.