jwaliszko / ExpressiveAnnotations

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

Validation with Hidden Checkbox #119

Closed madrianr closed 8 years ago

madrianr commented 8 years ago

Hello, is it possible to add validation on a Datetime field which is based on a Checkbox Value (chkRuhendmeldungVorhanden) of a hidden field?

[RequiredIf("chkRuhendmeldungVorhanden == true", ErrorMessage = "Sie müssen ein Wiederbetriebsdatum eingeben.")] public DateTime? Personenbetreuer_Wiederbetriebsdatum1 { get; set; }

if the checkbox "chkRuhendmeldungVorhanden" is visible it works but if I set it to hidden it doesn't work: @Html.HiddenFor(model => model.chkRuhendmeldungVorhanden, new { htmlAttributes = new { @class = "form-control" } })

robert

robert

jwaliszko commented 8 years ago

Hi, it should work without problems. Maybe the hidden template you are using is your custom one, and e.g. overrides the default name of the field?

The output HTML, for this chkRuhendmeldungVorhanden field, should be reassembled into this:

<input 
    name="chkRuhendmeldungVorhanden" 
    type="hidden" 
    ...

Check the web console output for any warnings.

jwaliszko commented 8 years ago

If you have some time provide me please self-contained, minimal, reproducible test case so I can help you.

madrianr commented 8 years ago

Hi,

I use:

@Html.HiddenFor(model => model.chkRuhendmeldungVorhanden, new { htmlAttributes = new { @class = "form-control" } })

and this code creates:

might be a problem that this is a hidden Input with data-val=true and Value=False instead of a checkbox?

robert

Von: Jarosław Waliszko [mailto:notifications@github.com] Gesendet: Sonntag, 24. April 2016 20:01 An: jwaliszko/ExpressiveAnnotations Cc: Robert Madrian (madrian e-dbsolutions); Author Betreff: Re: [jwaliszko/ExpressiveAnnotations] Validation with Hidden Checkbox (#119)

If you have some time provide me please self-contained, minimal, reproducible test case so I can help you.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/jwaliszko/ExpressiveAnnotations/issues/119#issuecomment-214010151 https://github.com/notifications/beacon/AKB-qteIAQXUI_puECjZSfWRJdZdhSMDks5p66_vgaJpZM4INimk.gif

jwaliszko commented 8 years ago

Your message seems to be incomplete, please use GitHub instead of direct reply: image

madrianr commented 8 years ago

found it - in Javascript I have to use $('#chkRuhendmeldungVorhanden').val(true); to assign a value to the field...

robert

jwaliszko commented 8 years ago

Ok, so your chkRuhendmeldungVorhanden property is of Nullable<bool> type. When uninitialized, it is interpreted as null. The result of the expression null == true is always false since a value of type bool is never equal to null of type bool?.

Instead of setting the field value in JS you can initialize the model before sending to client.

It has nothing to do with the visibility of the control.

madrianr commented 8 years ago

Thank you…

robert

Von: Jarosław Waliszko [mailto:notifications@github.com] Gesendet: Montag, 25. April 2016 15:47 An: jwaliszko/ExpressiveAnnotations Cc: Robert Madrian (madrian e-dbsolutions); Author Betreff: Re: [jwaliszko/ExpressiveAnnotations] Validation with Hidden Checkbox (#119)

Ok, so your chkRuhendmeldungVorhanden property is of Nullable type. When uninitialized, it is interpreted as null. The result of the expression null == true is always false since a value of type bool is never equal to null of type bool?.

Instead of setting the field value in JS, you can either change the type to bool, or initialize the model before sending to client.

It has nothing to do with visibility of the control.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/jwaliszko/ExpressiveAnnotations/issues/119#issuecomment-214335891 https://github.com/notifications/beacon/AKB-qsTfNShareVxKr_kk-f6ncgn9azzks5p7MXVgaJpZM4INimk.gif