We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10152
    • 156 Posts
    Hello, I’m using the Register snippet and attempting to validate a selected option from an html select list :

    <label for="activity">Activité
    <select name="activity:validFormActivity" id="activity" title="[[+error.activity]]" >
      <option value="[[+customFields.activity]]">[[+customFields.activity]]</option>
      <option value="1">Hotesse</option>
      <option value="2">Agent d'accueil</option>
      <option value="3">Mannequin</option>
      <option value="4">Steward</option>
    </select>
    


    and here the validation snippet "validFormActivity"
    <?php
    
        global $modx;
        global $scriptProperties;
        $tmp = $scriptProperties ['value'] ;
    
        if ( (int ) $tmp > 0 ) {
                 
            $modx->setPlaceholders( array ('activity' => $tmp ), 'customFields.' );  
    
        } else {
    
            return 'choose an option.<br />';
    
        }
    
        return true;
    
    ?>
    


    I presume the problem is because the value param is not on the <select> tag but inside <option value="something">.

    Before changing the html select list into a javascript input select-like field I would like to make sure I did not something wrong.


    EDIT : The problem had nothing with select. Actuallyn I can’t get any value, even a of simple text field

     <label for="otheractivity">Autre activité ( précisez )
            <input type="text" name="otheractivity:validFormOtheractivity" id="otheractivity" value="[[+otheractivity]]"  
     title="[[+error.otheractivity]]" />
            </label>
    

    and
    <?php
        
        global $modx;
    
        global $scriptProperties;
    
        $tmp =  $scriptProperties ['value'] ;
    
        if ( strlen ( $tmp ) < 30 ) {
    
            $modx->setPlaceholders( array ('otheractivity' =>'rrr'. $tmp ), 'customFields.' );  
    
        } else {
    
            return '30 chars max<br />';
    
        }
    
    
        return true;
    
    ?>
    


    $tmp is always empty and otheractivoty remains required !?
      • 28215
      • 4,149 Posts
      1. Never use global in code.
      2. The $scriptProperties array and $modx object are automatically available for you. You dont need to global them.
      3. Try this:
      <?php
      $tmp = $scriptProperties['value'];
      if (strlen($tmp) < 30) {
          $modx->setPlaceholders(array('otheractivity' =>'rrr'. $tmp ), 'customFields.' );
      } else {
          $scriptProperties['validator']->errors[$scriptProperties['key']] = '30 chars max<br />';
          return false;
      }
      return true;
      

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 10152
        • 156 Posts
        thank you Splittingred for those advices.

        I see that the basic registering fields ( fullname, phone, country...) do return an empty string in [[+error.fullname]], [[+error.phone]], [[+error.country]] if nothing wrong was detected.

        Is it normal that [[+error.otheractivity]] returns 1 on successful validation ? I assume yes it is ( because of the "return true" line ) but it does block registration process.

        I’ll feedback on further investigations.

          • 10525
          • 247 Posts
          Quote from: splittingred at May 26, 2010, 03:49 AM
          1. Never use global in code.
          2. The $scriptProperties array and $modx object are automatically available for you. You dont need to global them.
          splittingred,
          I have a static snippet. From the snippet file, I include another php file containing all my functions. I just use the snippet as the controller.

          I use different functions to display tpl chunks and set their placeholders. However, if I do not add
          global $modx;
          global $scriptProperties;
          my functions do not work and I get this error:
          Fatal error: Call to a member function getOption() on a non-object in /var/www/vhosts/mydomain.co.uk/httpdocs/modxfolder/assets/snippets/product/product.functions.inc.php on line 122
          The $scriptProperties array and $modx object seem to be available in the snippet and included files, but not from within functions. Is this correct behaviour?