We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36551
    • 416 Posts
    I'm using Login to enable web visitors to login to their private pages. That's all good.

    Now I need to add a check box that requires them to agree to terms before they login.

    I'm assuming that I can simply add a field to the standard loginTpl and use the valiation parameter in the same way that FormIt or Register extras do. I have working examples of this in both Formit and Register, but can't get it to work in Login. Login seems to just ignore it.

    Is my assumption correct? Does validation works the same with Login?

    Here's what I've got.
    [[!login? 
    &loginTpl=`mylgnLoginTpl`
    &validate=`terms:required`
    ]]

    Here's the checkbox I added to the form.
    [[!+fi.error.terms]]
    <label>I agree to the terms</label>
    <input type="hidden" name="terms" value="" />
    <input id="yes" type="checkbox" class="" name="terms" value="Yes" [[!+fi.terms:FormItIsChecked=`Yes`]] >Yes
    

    I also tried this with: name="terms[]" vs name="terms" (which works in Subscribe)
    <label>I agree to the terms</label>
    <input type="hidden" name="terms[]" value="" />
    <input id="yes" type="checkbox" class="" name="terms[]" value="Yes" [[!+fi.terms:FormItIsChecked=`Yes`]] >Yes

    When I submit, there is no validation and the form will login if left unchecked.

    I have to assume that would be a common requirement, yet I didn't find anything on this in forums specifically for Login.

    How do I make this work?
      • 36551
      • 416 Posts
      I just noticed this in the Modx error log.

      [2016-04-05 12:38:44] (ERROR @ /xxxxx/xxxxxxxx/public_html/core/components/login/controllers/web/Login.php : 97) PHP warning: htmlspecialchars() expects parameter 1 to be string, array given

      When I remove the [] from the field name, the error is no longer generated.

      name="terms[]"
        • 3749
        • 24,544 Posts
        You use the [] suffix only when you want to have multiple options with the same name so the result would be an array. You wouldn't want it for a single checkbox.

        For security, Login "sanitizes" each entry in the Login form with htmlspecialchars(), which can't handle an array, the assumption being that a login form only has one function -- to log users in.

        The Login snippet makes the same assumption, which is why it has no &validate property.

        I've never seen a Login form anywhere ask for anything but a username and password. TOS agreements I've seen have always been on the Register page.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 36551
          • 416 Posts
          Thanks Bob!
            • 46886
            • 1,154 Posts
            And I remember that BobRay told someone recently that this is usually done with a little java:

            just throw in a little JavaScript that keeps the form from being submitted and pops up a message if the agree checkbox is not checked. That's the way it's usually done.

            PS here is the thread but I dont think it will be very helpful: https://forums.modx.com/thread/99542/how-to-persist-checkbox-state-if-submission-fails-for-other-reasons-page-refreshes#dis-post-538755

            I think there could be good reasons to need forms for agreement on login. [ed. note: nuan88 last edited this post 10 years, 6 months ago.]