We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44606
    • 8 Posts
    Hi there!
    I'm trying to make a form to make a reservation for a certain date.
    So I created a text input field in my form with name date like so:
    <input type="text" name="date" value="[[!+fi.date]]">


    I want to validate the form the have the format dd/mm.
    So I created a validation like so:
    date:required:isDate=^%d/%m^


    But unless the isDate parameter it still expects mm/dd, since it won't accept a date like 31/10.
    I did look into the settings of MODX but didn't find a setting that would fix this.

    Anybody got an idea on how to fix this?
    Thanks in advance!

    This question has been answered by bennyb. See the first response.

      • 36926
      • 701 Posts
      Quote from: johman10 at Oct 15, 2014, 03:48 PM
      Hi there!
      But unless the isDate parameter it still expects mm/dd,

      Sorry bit confused with what you're saying here.
      So is the user enters in a date like "31/10" it's not validating as it's expecting it to be mm/dd?
        • 44606
        • 8 Posts
        Quote from: bennyb at Oct 15, 2014, 04:38 PM
        Quote from: johman10 at Oct 15, 2014, 03:48 PM
        Hi there!
        But unless the isDate parameter it still expects mm/dd,

        Sorry bit confused with what you're saying here.
        So is the user enters in a date like "31/10" it's not validating as it's expecting it to be mm/dd?

        Yeah! That's exactly the problem!
          • 36926
          • 701 Posts
          Can you paste your full snippet call.
            • 44606
            • 8 Posts
            Full snippet call:
            [[!FormIt?
            			&hooks=`email`
            			&emailFrom=`[[+email]]`
            			&emailTpl=`EmailReserveren`
            			&emailTo=`[email protected]`
            			&emailSubject=`Reservering [[+play]]`
            			&validate=`name:required,
            			email:email:required,
            			phone:required:minLength=^10^:maxLength=^10^:isNumber,
            			play:required,
            			date:isDate=^%d/%m^,
            			count:required:isNumber`
            			&successMessage=
            			`<div class="succesMessage"><span class="message">Uw reservering is succesvol verzonden</span></div>`]]
            			
            			[[!+fi.validation_error_message:!empty=`
            			<div class="errorMessage">
            			  <span class="message">De volgende verplichte velden zijn leeg of niet geldig:</span>
            			  <ul>
            				[[!+fi.error.name:!empty=`<li>Naam is een verplicht veld</li>`]]
            				[[!+fi.error.email:!empty=`<li>E-mail is een verplicht veld</li>`]]
            				[[!+fi.error.phone:!empty=`<li>Telefoonnr. is een verplicht veld</li>`]]
            				[[!+fi.error.play:!empty=`<li>Voorstelling is een verplicht veld</li>`]]
            				[[!+fi.error.date:!empty=`<li>Datum is een verplicht veld</li>`]]
            				[[!+fi.error.count:!empty=`<li>Aantal kaarten is een verplicht veld</li>`]]
            			  </ul>
            			</div>`]]
            
            			[[!+fi.successMessage]]
            [ed. note: johman10 last edited this post 12 years ago.]
              • 36926
              • 701 Posts
              In that call you've got the validation for date different to what you'd originally said. You have :
              date:isDate=^%D/%m/%Y^
              


              So according to the modx docs it would be expecting:

              10/31/14/10/2014

              as %D = the full date formatted like 10/31/14.

              So for just 31/10/2014 you'd just want:
              %d/%m/%Y


              EDIT:
              Link to the modx doc for date formats:
              http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/commonly-used-template-tags/date-formats
                • 44606
                • 8 Posts
                Oohh yeah you're right Bennyb...
                But that is just because I was trying around a lot with different options etc.
                It doesn't work with %d/%m/%Y too.

                Thank you for you help.
                  • 36926
                  • 701 Posts
                  I'll take a quick look and see if it works for me.

                  You might wanna block out your email on the snippet call in the post above.
                  • discuss.answer
                    • 36926
                    • 701 Posts
                    Hi johman,

                    From looking at the isDate function it's running this:
                                $ts = strtotime($value);
                    


                    Which I think will return false with the format "d/m" so I reckon doing your own validator might be best. Here's a quick example that should do the trick.

                    SNIPPET: isDateFormat
                    $date = DateTime::createFromFormat("d/m/Y", $value);
                    
                    if ($date !== false && !array_sum($date->getLastErrors())) {
                        $success = true;
                    }
                    
                    if (!$success) {
                    
                      $validator->addError($key,'Incorrect date format');
                    }
                    
                    return $success;
                    


                    And add this to your formit call:
                    &customValidators=`isDateFormat`
                    &validate=`date:isDateFormat`
                    

                      • 44606
                      • 8 Posts
                      I did try to use your code but it didn't work out for me.
                      I decided to take another approach with a select field through Migx.

                      Thank you very much for your help bennyb!