We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9674
    • 170 Posts
    Hi,

    We have build a customer site under modx Revo 2.2.8 and using Formit 2.2.0.
    The site is multinational, and we use babel 2.2.5.

    We have a form that is used for room booking (the site is for a guest room). In this form we need to validate the date
    on the english version with
    [[!FormIt? &validate=`date:isDate=^%m/%d/%Y^`]]
    ,
    and in the french version of the form with
    [[!FormIt? &validate=`date:isDate=^%d/%m/%Y^`]]
    .

    The english form works fine.

    In the french form we can't get it to validate a french format date like 30/07/2013 it only validates 07/30/2013.

    the french context is :
    Langue cultureKey fr
    locale locale fr_FR.UTF-8


    the english context is :
    Langue cultureKey en
    locale locale en_US.UTF-8


    Thanks for your help

    Philippe


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

    [ed. note: Fildefer57 last edited this post 13 years, 2 months ago.]
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      isDate validates input ("date") using strtotime, which expects the american format if you're using slash ("m/d/y "), or european format if you're using dash ("d-m-y").

      So try to keep the <input /> field feeds the validator with the correct format first.
      Then it'll be processed further for the output (^%d/%m/%Y^).
        Rico
        Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
        MODx is great, but knowing how to use it well makes it perfect!

        www.virtudraft.com

        Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

        Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

        Maintainter/contributor of Babel

        Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
        • 9674
        • 170 Posts
        Thanks for your help.

        unfortunatly, it does not validate ANY other format than 07/30/2013...

        If I do not find any alternative solution, I will look further creating an alternate validation.
        I am not a genius in php, but I will do my best.

        Philippe
          • 9674
          • 170 Posts
          I try to make a custom validator called inverseDate.
          For that I put the following code to my resource page :
          <p>[[!FormIt? 
                  &customValidators=`inverseDate`
          	&hooks=`spam,email,redirect` 
          	&emailTpl=`MyEmailChunk` 
          	&emailTo=`[email protected]` 
          	&emailReplyTo=`[email protected]` 
          	&errTpl=`<span class="errors">[[+error]]</span>` 
          	&validate=`date:inverseDate=^%m/%d/%Y^`
                  &store=`1` 
                  &redirectTo=`9`
          
                  ]]
          
          </p>
          <p>[[+fi.error.error_message]]</p>
          <form id="contactForm" action="[[~[[*id]]]]" method="post"><input type="hidden" name="nospam:blank" />
          
          <p><label for="date"> Date de réservation souhaitée (mm/jj/aaaa) : <span class="error">[[+fi.error.date]]</span> </label> <input id="date" class="contactInput" type="text" name="date" value="[[+fi.date]]" /></p>
          <div class="form-buttons"><input type="submit" value="Envoi des données" /></div>
          </form>
          </div>


          Then i created a snippet based on Shaun's code to change the validation process :
          <?php
          public function inverseDate($key,$value,$format = '%m/%d/%Y') {
          
          echo '<br />key'.$key;
          echo '<br />value'.$value;
          echo '<br />param'.$param;
          echo '<br />type'.$type;
          // echo '<br />validator'.$validator;
          
                  $ts = false;
                  if (!empty($value)) {
          /*	      $ts = strtotime(str_replace('/', '-', $value)); */
          /* This was supposed to replace */
                     $ts = strtotime($value);
                  }
                  if ($ts === false || empty($value)) {
                      return $this->_getErrorMessage($key,'vTextIsDate','formit.not_date',array(
                          'format' => $format,
                          'field' => $key,
                          'value' => $value,
                      ));
                  }
                  if (!empty($format)) {
                      $this->fields[$key] = strftime($format,$ts);
                  }
                  return true;
              }


          I get no echoes, nor any validation ?

          Any ideas modx friends ?

          Philippe (ps : I know, I am not a php person...) [ed. note: Fildefer57 last edited this post 13 years, 2 months ago.]
          • discuss.answer
            • 9674
            • 170 Posts
            I will edit this post as I progress on my way.

            First mistake : code for my inverseDate snippet does not have to be in
            public function inverseDate($key,$value,$format = '%m/%d/%Y') { 
            }

            So must be :
            <?php
            
            echo '<br />key'.$key;
            echo '<br />value'.$value;
            echo '<br />param'.$param;
            echo '<br />type'.$type;
            // echo '<br />validator'.$validator;
             
                    $ts = false;
                    if (!empty($value)) {
            /*        $ts = strtotime(str_replace('/', '-', $value)); */
            /* This was supposed to replace */
                       $ts = strtotime($value);
                    }
                    if ($ts === false || empty($value)) {
                        return $this->_getErrorMessage($key,'vTextIsDate','formit.not_date',array(
                            'format' => $format,
                            'field' => $key,
                            'value' => $value,
                        ));
                    }
                    if (!empty($format)) {
                        $this->fields[$key] = strftime($format,$ts);
                    }
                    return true;
             


            Second mistake : do not use code you don't undertand...
            Replaced :
                        return $this->_getErrorMessage($key,'vTextIsDate','formit.not_date',array(
                            'format' => $format,
                            'field' => $key,
                            'value' => $value,
                        ));


            with :

             $validator->addError($key,'daate obligatoirement en français!');


            Final working code for the validator is :

            <?php
                    $ts = false;
                    if (!empty($value)) {
                  $ts = strtotime(str_replace('/', '-', $value)); 
            /* on this line, I replace the / use in my form date (30/07/2013 for july, 30th 2013) with a - that strtotime undertand as a so called european date 30-07-2013 */
                    }
                    if ($ts === false || empty($value)) {
             $validator->addError($key,'date obligatoirement en français! date must be in french format');
                   }
                    if (!empty($format)) {
                        $this->fields[$key] = strftime($format,$ts);
                    }
                    return true;
            ?>


            And make sure to add
            &customValidators=`inverseDate` &validate=`date:inverseDate=^%m/%d/%Y^`


            Hope this will help other modxers !

            Philippe [ed. note: Fildefer57 last edited this post 13 years, 2 months ago.]
              • 11055 ☆ A M B ☆
              • 3,112 Posts
              I have been using some jQuery's date drop down plugins for this kind of field.
              That would make the date comes as the specified format I need, mm/dd/yyyy.
              That'll be easier for user to pick the date, and avoid to mess up with the proper format.

              since then,
              [[!FormIt? &validate=`date:isDate=^%d/%m/%Y^`]]

              always works.

              It might not be the correct format if you switch context, because you have different locale.
                Rico
                Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                MODx is great, but knowing how to use it well makes it perfect!

                www.virtudraft.com

                Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                Maintainter/contributor of Babel

                Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.