We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 12584
    • 208 Posts
    Can anyone please tell me how you make a form field required using the built in eForm.

    I have added more form fields but cant work out how to make one of them required.

    Cheers

    Ian
      • 14050
      • 788 Posts
      Just place an eform attribute in the html for the input like:

      <input name="departure" id="cfDeparture" class="calendarSelectDate" type="text" eform="Name of Text Field::1" />

      The "::1" tells eform that it is required and if it is not filled in, it will say "Name of Text Field" is a required field.

      Here is the information regarding validation from the eForm snippet’s documentation.

      Datatypes and formatting
      eForm is incorporating a form parser which extracts formatting and validation options from each form field. To set options for a field add the eform(pseudo) attribute to each required form field.

      <input type="text" name="color" eform="A Color:string:1" />

      The basic format of the eform attribute is:

      [description/title]:[datatype]:[required]:[validation message]:[validation rule]

      Data types
      You only need to set the following data types. Others will be set automatically (radio & checkbox as they are, string for textbox and listbox for select)

      Standard validation: All fields that are required will be checked if they are left empty.

      string - No specific validation besides checking if it’s empty if the field is required.
      date - Checks if it is a valid date (based on php’s strtotime() function)
      integer - Checks if it is a number (does not check if it is in fact an integer)
      float - Checks if it is a number
      email - Checks if it’s a valid email address using a simple regular expression
      file - (for file upload input) - checks if a size error occurs, does not currently check file type
      html - Same as string except that it converts line endings (\n) to
      tags
      The listbox, checkbox and radio fields do normally not require the datatype to be set. eForm will recognize these automatically. It will validate the values against the list of values placed in the form.

      Extended Server Validation
      This version instroduces extended server validation and word filtering using very flexible validation rules that can be set in teh eformattribute. 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:
      #FILTER #LIST
      use double pipe to separate 2 comma separated lists of words and replacement values.
      example; #FILTER #LIST badword,verybadword||goodword,verygoodword
      #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);
      }
      #FILTER #REGEX
      regular expression replace - syntax as for preg_replace() separate the search and replacement expression with a double pipe symbol (||)
      TODO: example
      Select boxes, radio options and checkbox fields
      Select boxes, radio options and checkbox fields now have working automatic validation. Any input for these fields is validated against the values set in your form template. This avoids anyone tampering with the form by adding their own values to these fields

        Jesse R.
        Consider trying something new and extraordinary.
        Illinois Wine

        Have you considered donating to MODx lately?
        Donate now. Every contribution helps.
        • 12584
        • 208 Posts
        Thankyou very much, I was close with one of my attempts I only added 1 sadfull colon) instead of 2.

        Really helpful information thankyou for your time

        Regards

        Ian