4.3. Field Rules

Here is an example of applying a couple of rules to a field - this field must be filled in, it will only accept well formed email addresses, and there can be no duplicates in the system:

...

  <field id="email" type="text" multiLingual="0">
    <title lang="da">Email</title>
    <title lang="en">Email</title>
    <rules>
      <required/>
      <email/>
      <unique/>
    </rules>
  </field>

...

The following rules can be applied to any field in your XML. It is your own responsibility not to add contradicting or meaningless rules, like adding numeric and lettersonly to the same field or adding email to a date field.

alphanumeric

Letters and numbers. No commas allowed. Scandinavian characters, like æ, ø and å are not allowed.

email

A well formed email address.

lettersonly

Letters only. A to Z upper or lowercase. Scandinavian characters, like æ, ø and å are not allowed.

mimetypes

Can be applied to fields of type file or image and adapts a slightly different syntax, as the rule element has children:

...

  <field id="illustration" type="image" multiLingual="0">
    <title lang="da">Illustration</title>
    <title lang="en">Illustration</title>
    <rules>
      <mimetypes>
        <valid>image/jpeg</valid>
        <valid>image/png</valid>
        <valid>image/gif</valid>
      </mimetypes>
    </rules>
  </field>

...

Moski2.net tries to determine the mime type of the uploaded file in two ways:

  1. The browser sends along a suggested mime type (which can't really be trusted since it is typically just derived from the file name extension).

  2. Using the PHP extension fileinfo (which will be moved to the base PHP distribution in 5.3). This actually reads the file and tries to determine the mime type from the bytes in the file.

By default Moski2.net will only trust what is revealed by 2), but if you want to also trust what is suggested by the browser, set the trustBrowser attribute to 1:

...

  <field id="illustration" type="image" multiLingual="0">
    <title lang="da">Illustration</title>
    <title lang="en">Illustration</title>
    <rules>
      <mimetypes trustBrowser="1">
        <valid>image/jpeg</valid>
        <valid>image/png</valid>
        <valid>image/gif</valid>
      </mimetypes>
    </rules>
  </field>

...
numeric

Numbers only. Decimal comma (dot) is allowed.

nonzero

A number other than zero. No commas allowed.

regex

A regular expression that the input must match. The syntax is that of JavaScript regex, as described at http://www.w3schools.com/jsref/jsref_obj_regexp.asp.

The regex rule has content, contrary to most other rules - you simply add a string of regex code in the regex element:

...

  <field id="mylowercase" type="text" multiLingual="0">
    <title lang="da">Små bogstaver</title>
    <title lang="en">Lowercase</title>
    <rules>
      <required/>
      <regex>/^[a-z]+$/</regex>
    </rules>
  </field>

...
required

The field must be filled in with something.

unique

Used to ensure that what ever is entered in this field is unique amongst the records of this type.

Note

Can not be used with external content types.