CheckBoxFor + FluenValidation + Client side

4 posts, 1 answers
  1. Pawel
    Pawel avatar
    5 posts
    Member since:
    Mar 2013

    Posted 06 Aug 2015 Link to this post

    Hi,
    I want to have client side for a checkbox. When the user presses submit button, or checkbox loose focus the validation should be trigged. Basically same behaviour as for textboxes (which are working fine).

    I try to make client side validation for a CheckBox
    Here is the sample code:
    @Html.CheckBoxFor(model => model.RulesAccepted, new { @class = "control-label" })
    @Html.ValidationMessageFor(model => model.RulesAccepted)


    If I do similar to the example code (http://demos.telerik.com/aspnet-mvc/validator/index) checkbox is validated. If it is not check then I get an error, which is correct.
    But there is number of issues preventing me to use that code:
    1.Model passed back to the post action does not have boolean value set (RulesAccepted is the property name on the model)
    2.I'm not able to plug-in fluent validation messages to display them to the user.

    If I try to do any other way no message is appearing when it should.
    In the code I have now the TextBoxes validators work fine. I have only problem with CheckBox validator.
  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 10 Aug 2015 Link to this post

    Hello Pawel,

     

    As far as I understand you have a RulesAccepted field/property in your model definition. Based on the 1. issue the RulesAccepted property is not boolean, is this correct?

     

    If yes - could you please clarify the following:

      1. What is the type of the RulesAccepted and what is the purpose of the check box element?     2. How the validation should work in this case? 

      3. What should the state of the check box element indicate? 

     

    Regards,
    Boyan Dimitrov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Pawel
    Pawel avatar
    5 posts
    Member since:
    Mar 2013

    Posted 10 Aug 2015 in reply to Boyan Dimitrov Link to this post

    RulesAccepted is boolean. In model and in viewmodel.

     

    This is simple checkbox used to clarify that user, who want to register on my portal has accepted the rules.

    I want to fire the client-side validation using Kendo Validator, but if I do it like here http://demos.telerik.com/aspnet-mvc/validator/index then MVC does not bind the property in Action in Controller.

  5. Answer
    Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 12 Aug 2015 Link to this post

    Hello Pawel,

    Please find attached a sample project that implements the desired functionality:  

         - CheckBoxFor helper is used in order to display a check box bound to the boolean property of the model.

         - Client validation is used to validate whether the check box is checked or not.

          -If the validation is passed successfully you can retrieve the entire model in the action method. 

     

    Regards,
    Boyan Dimitrov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready