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

    I need to validate that there are only 14 numbers (no more no less) in an input.

    What type of custom hook I should write ? I don't know a word of PHP sad

    Thank you ! [ed. note: Perrine last edited this post 12 years, 2 months ago.]
      • 1778
      • 659 Posts
      Hello
      You don't need a "hook" but a customValidator.
      This piece of code should work, (only 14 numbers allowed, no space, no letters, no special chars only numbers)

      $pattern = '`^[[:digit:]]{14}$`'; // accepts only 14 numbers (no letters, no space etc...)
      if(!preg_match($pattern, $value)){
         $validator->addError('yourfieldname','your error message'); // error message if the value does not matche the pattern
         return false;
      }
      


      Hope this helps
      Cheers
        • 23050
        • 1,842 Posts
        Hello Anso,

        Thank you for your help ! Ok for custom validator wink

        I've created the snippet :

        <?php
        $pattern = '`^[[:digit:]]{14}$`'; // accepts only 14 numbers (no letters, no space etc...)
        if(!preg_match($pattern, $value)){
           $validator->addError('N° SIRET','Le n° SIRET doit contenir 14 chiffres'); // error message if the value does not matche the pattern
           return false;
        }


        but it doesn't work. FormIt validates even if there is one letter.

        If I refer to http://rtfm.modx.com/display/ADDON/FormIt.Validators#FormIt.Validators-CustomValidators, there is a return $success; at the end of the snippet... So I tried to make :

        <?php
        $value = (float)$value;
        $pattern = '`^[[:digit:]]{14}$`';
        $success = preg_match($pattern, $value);
        if (!$success) {
          // Note how we can add an error to the field here.
          $validator->addError('N° SIRET','Le n° SIRET doit contenir 14 chiffres');
        }
        return $success;


        No chance...

        I've tested the isBigEnough snippet and it works.

        Here is a part of my call :

        [[!FormIt? 
             &hooks=`spam,email,redirect`
             &emailSubject=`Nouvelle inscription`
             &emailTpl=`formItInscriptionAnnonceur`
             &emailTo=`[email protected]`
             &redirectTo=`17`
             &customValidators=`isBigEnough`
             &validate=`pagetitle:required,
                        societe_siret:isBigEnough`
        ]]
         
        <p>[[+fi.error.error_message]]</p>
        <form class="form" action="[[~[[*id]]]]" method="post">
            <input name="nospam:blank" type="hidden" />
            <input name="resource_id" type="hidden" value="[[+fi.id]]" />
         
        
            <div class="champ">
              <label for="form_pagetitle"><span class="req">*</span>Nom de votre enseigne : 
                  <span class="error">[[!+fi.error.pagetitle]]</span>
              </label>
              <input type="text" name="pagetitle" id="form_pagetitle" value="[[!+fi.pagetitle]]" />
            </div>
        
            <div class="champ">
              <label for="form_siret"><span class="req">*</span>N° SIRET : 
                  <span class="error">[[!+fi.error.societe_siret]]</span>
              </label>
              <input type="text" name="societe_siret" id="form_siret" value="[[!+fi.societe_siret]]" />
            </div>


        Thank you smiley
          • 1778
          • 659 Posts
          Hello

          I tested it and it works for me

          the snippet (validator) code let say we call it "vSiret":
          <?php
          $modx->log(modX::LOG_LEVEL_ERROR, 'in my validator' ); // just to be sure you fire the validator
          
          $pattern = '`^[[:digit:]]{14}$`'; // accepts only 14 numbers (no letters, no space etc...)
          
          $modx->log(modX::LOG_LEVEL_ERROR, 'value :' .$value); // this line only to return the input value in your error log
          if(!preg_match($pattern, $value){
             $validator->addError('siret','Le SIRET doit contenir uniquement 14 chiffres.');
             return false;
          }else{
          $modx->log(modX::LOG_LEVEL_ERROR, 'SIRET OK'); // this line is returned in your error log if the siret is 14 numbers only
          }
          
          return true;
          


          All the lines beginning with "$modx->log" can be removed once you have it working as you expect (they are only here to help you to see where it fails if it fails)

          your fomit call
          [[!FormIt? 
               &hooks=`spam,email,redirect`
               &emailSubject=`Nouvelle inscription`
               &emailTpl=`formItInscriptionAnnonceur`
               &emailTo=`[email protected]`
               &redirectTo=`17`
               &customValidators=`vSiret`
               &validate=`pagetitle:required,
                          societe_siret:vSiret`
          ]]
          


          It should work... If you have any trouble let me know (if it's more easy for you , you can reach me by email and explain your issue in french...)
          Cheers [ed. note: anso last edited this post 12 years, 2 months ago.]
            • 23050
            • 1,842 Posts
            (It might help some other people so let's continue here in english if you don't mind wink )

            I get this in my error log :
            [2012-02-27 13:39:31] (ERROR @ /v2/index.php) in my validator
            [2012-02-27 13:39:31] (ERROR @ /v2/index.php) value :rg
            [2012-02-27 13:40:17] (ERROR @ /v2/index.php) in my validator
            [2012-02-27 13:40:17] (ERROR @ /v2/index.php) value :12345678901234
            [2012-02-27 13:40:17] (ERROR @ /v2/index.php) SIRET OK

            ...

            Ok I've found it !! it's not
            $validator->addError('siret',

            but
            $validator->addError('societe_siret',


            always typo.... :p

            Thanks for your help and the tip of log() smiley
              • 1778
              • 659 Posts
              Glad you have it to work ^_^
              Cheers