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

    I’ve been searching for answers on the forum for the past 30 minutes to no real avail. What I want to do is add custom validation to my forms. Bare with me as I’m new to MODx.

    My form looks like this:
    [!WebLoginPE? &type=`taconite`!]
    
    Add New
    
    [!eForm? &noemail=`true` &formid=`add` &eFormOnBeforeMailSent=`c4w_add` &tpl=`add` &vericode=`1` &thankyou=`3`!]


    Then I have my chunk:
    Service Description: <textarea name="description" eform="Description:description:string:#FUNCTION CheckLength($value, 200);"></textarea>


    I need this "description" field to have a minimum of 200 characters (including whitespace). So I’ve created the function CheckLength which is messy as I’ve been trying to get it to run:

    <?php
    function CheckLength($value, $limit){
    		if (strlen($value) > $limit)
    echo $value.">>".$limit;
    			return true;
    		else
    echo $value.">>".$limit;
    			return false;
    	}
    ?>


    What exactly am I doing wrong? Basically how do I run a custom function as a validation method?

    Thanks,

    Much appreciated
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      This should help; you can either use the intput field’s eform attribute, or create a snippet with a function to do the job.

      http://modxcms.com/forums/index.php/topic,33958.msg206684.html#msg206684
      http://modxcms.com/forums/index.php/topic,41805.msg251151.html#msg251151
      http://modxcms.com/forums/index.php/topic,35873.msg216788.html#msg216788

      Extended Server Validation

      This version instroduces extended server validation and word filtering using very flexible validation rules that can be set in the eform attribute. You can set 2 extra validation parameters, a custom error message and a validation or filter rule.

      example: eform="Year of Birth:integer:1:Must be between 1950 and 2002:#RANGE 1950-2002"
      The validation rules

      * #LIST - comma separated list of valid values
      example: #LIST blue,red,green.maroon
      * #RANGE - a comma separated list of numbers or numeric ranges. When setting a range the order is not important. 1~10 or 10~1 will both validate a number between 1 and 10 (inclusive). Handles negative as well as positive numbers
      example: #RANGE 1,3,-5~-15,60~82
      * #SELECT - list of valid values retrieved from a database query The query should only return a single column of values (the function only checks against the first returned column). You can use the {DBASE} {PREFIX}tags which will be replaced by the MODx database name and table prefix respectively.
      example: #SELECT keyword FROM {PREFIX}site_keywords
      * #EVAL - string of php code. Should return either true or false
      deprecatedAlthough #EVAL still works in eForm 1.4 this rule will very likely no longer be supported in future versions. Use #FUNCTION instead.
      * #FUNCTION - Name of a function. The function should expect one parameter (the posted value) and return either TRUE or FALSE. See the eform event example on how you can include a function. example: #FUNCTION myValidationFunction
      * #REGEX - regular expression - syntax as for preg_match() - see php manual
      example: #REGEX /^[a-z]+ [a-z0-9_]+/i
      * #FILTER - Filters do not validate the input but simply replace words or values using filter criteria. You can use the following filters:
      o #FILTER #LIST
      use double pipe to separate 2 comma separated lists of words and replacement values.
      example; #FILTER #LIST badword,verybadword||goodword,verygoodword
      o #FILTER #EVAL
      example: #FILTER #EVAL return myFilterFunction($value);
      (offcourse you have to make sure the function exists somehow)

      example filter function
      function myFilterFunction($value){
      $badWords = array(’scribble’,’coding’);
      $goodWords = array(’design’,’sleep’);
      return str_replace($badWords,$goodWords,$value);
      }
      o #FILTER #REGEX
      regular expression replace - syntax as for preg_replace() separate the search and replacement expression with a double pipe symbol (||)
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org