locked
How to get data-val-required=" RRS feed

  • Question

  • User1991482975 posted

    Hi i would love to write my html code like this :

    <input data-val="true" data-val-required="Le champ Statut d'activation du champ: Nom et Prénom est requis." id="PartnerAdvertiseActiveSex" name="PartnerAdvertiseActiveSex" type="checkbox" value="true">

    but i need to get the value frome my model :

     data-val-required="Le champ Statut d'activation du champ: Nom et Prénom est requis." 

    how to get this automatically without using :

    @Html.EditorFor(model => model.PartnerAdvertiseActiveTel)

    thanks

    Tuesday, December 1, 2020 9:09 AM

Answers

  • User1686398519 posted

    Hi grafic.web, 

    You also raised a similar question in another post. You don't seem to want to use html helper, and you have used resource file to store error messages. In other words, the problem you are encountering now is how to get the value in the resource file on the view.

    <input class="form-control" data-val="true"
           data-val-length="@ViewRes.ValidationStringsLength"
           data-val-length-max="2"
           data-val-required="@ViewRes.ValidationStrings"
           id="PartnerAdvertiseTitle"
           name="PartnerTitle"
           type="text"
           value="">

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2020 8:14 AM

All replies

  • User475983607 posted

    I think you are asking how to read a data- attribute with JavaScript/jQuery.

    <script>
        var val = $('#PartnerAdvertiseActiveSex').data('val-required');
        console.log(val);
    </script>

    Reference documentation

    https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

    Tuesday, December 1, 2020 1:58 PM
  • User1991482975 posted

    no, i wrote wrongly..

    in fact i need to populate the same line of html.

    when i put this : 

    @Html.EditorFor(model => model.PartnerAdvertiseActiveTel)

    i got this :

     <input class="check-box" data-val="true" data-val-required="The Statut d'activation du champ: Téléphone field is required." id="PartnerAdvertiseActiveTel" name="PartnerAdvertiseActiveTel" type="checkbox" value="true"><input name="PartnerAdvertiseActiveTel" type="hidden" value="false">

    but now i am writing the code by hands and i just need to populate this :

    data-val-required="The Statut d'activation du champ: Téléphone field is required." 

    how to do?

    Tuesday, December 1, 2020 2:02 PM
  • User475983607 posted

    no, i wrote wrongly..

    in fact i need to populate the same line of html.

    when i put this : 

    @Html.EditorFor(model => model.PartnerAdvertiseActiveTel)

    i got this :

     <input class="check-box" data-val="true" data-val-required="The Statut d'activation du champ: Téléphone field is required." id="PartnerAdvertiseActiveTel" name="PartnerAdvertiseActiveTel" type="checkbox" value="true"><input name="PartnerAdvertiseActiveTel" type="hidden" value="false">

    but now i am writing the code by hands and i just need to populate this :

    data-val-required="The Statut d'activation du champ: Téléphone field is required." 

    how to do?

    You want to set the value on the server?

    data-val-required="@Model.MyMessage" 

    Or the client?

    $('#PartnerAdvertiseActiveSex').data('val-required', 'My text');

    Tuesday, December 1, 2020 2:07 PM
  • User1991482975 posted

    on the server

    data-val-required="@Model.MyMessage" 

    Thanks
    Tuesday, December 1, 2020 2:13 PM
  • User1686398519 posted

    Hi grafic.web, 

    You also raised a similar question in another post. You don't seem to want to use html helper, and you have used resource file to store error messages. In other words, the problem you are encountering now is how to get the value in the resource file on the view.

    <input class="form-control" data-val="true"
           data-val-length="@ViewRes.ValidationStringsLength"
           data-val-length-max="2"
           data-val-required="@ViewRes.ValidationStrings"
           id="PartnerAdvertiseTitle"
           name="PartnerTitle"
           type="text"
           value="">

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 4, 2020 8:14 AM