<commit_before>= Validators =

[[PageOutline]]

Validators help you to validate an input before perstisting it to the database. In Propel, validators are rules describing what type of data a column accepts. Validators are referenced in the `schema.xml` file, using `<validator>` tags.

Validators are applied at the PHP level, they are not created as constraints on the database itself. That means that if you also use another language to work with the database, the validator rules will not be enforced.
You can also apply multiple rule entries per validator entry in the schema.xml file. 

== Overview ==

In the following example, the `username` column is defined to have a minimum length of 4 characters:

{{{
#!xml
<table name="user">
  <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
  <column name="username" type="VARCHAR" size="34" required="true" />
  <validator column="username">
    <rule name="minLength"  value="4"  message="Username must be at least ${value} characters !" />
  </validator>
</table>
}}}

Every column rule is represented by a `<rule>` tag. A `<validator>` is a set of `<rule>` tags bound to a column.

At runtime, you can validate an instance of the model by calling the `validate()` method:

{{{
#!php
<?php
$user = new User();
$user->setUsername("foo"); // only 3 in length, which is too short...
if ($objUser->validate()) {
  // no validation errors, so the data can be persisted
  $user->save();
} else {
  // Something went wrong.
  // Use the validationFailures to check what
  $failures = $objUser->getValidationFailures();
  foreach($failures as $failure) {
    echo $objValidationFailure->getMessage() . "<br />\n";
  }
}
}}}

`validate()` returns a boolean. If the validation failed, you can access the array  `ValidationFailed` objects by way of the `getValidationFailures()` method. Each `ValidationFailed` instance gives access to the column, the messagen and the validator that caused the failure.

== Core Validators ==

Propel bundles a set of validatorts that should help you deal with the most common cases.

=== !MatchValidator ===

The `MatchValidator` is used to run a regular expression of choice against the column. Note that this is a `preg`, not `ereg` (check [http://www.php.net/preg_match the preg_match documentation] for more information about regexps).

{{{
#!xml
<validator column="username">
  <!-- allow strings that match the email adress pattern -->
  <rule
    name="match"
    value="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/"
    message="Please enter a valid email address." />
</validator><commit_msg>[1.5][1.6] Fixed typo in validators documentation (closes #1168)<commit_after>= Validators =

[[PageOutline]]

Validators help you to validate an input before perstisting it to the database. In Propel, validators are rules describing what type of data a column accepts. Validators are referenced in the `schema.xml` file, using `<validator>` tags.

Validators are applied at the PHP level, they are not created as constraints on the database itself. That means that if you also use another language to work with the database, the validator rules will not be enforced.
You can also apply multiple rule entries per validator entry in the schema.xml file. 

== Overview ==

In the following example, the `username` column is defined to have a minimum length of 4 characters:

{{{
#!xml
<table name="user">
  <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true"/>
  <column name="username" type="VARCHAR" size="34" required="true" />
  <validator column="username">
    <rule name="minLength"  value="4"  message="Username must be at least ${value} characters !" />
  </validator>
</table>
}}}

Every column rule is represented by a `<rule>` tag. A `<validator>` is a set of `<rule>` tags bound to a column.

At runtime, you can validate an instance of the model by calling the `validate()` method:

{{{
#!php
<?php
$user = new User();
$user->setUsername("foo"); // only 3 in length, which is too short...
if ($objUser->validate()) {
  // no validation errors, so the data can be persisted
  $user->save();
} else {
  // Something went wrong.
  // Use the validationFailures to check what
  foreach ($objUser->getValidationFailures() as $failure) {
    echo $failure->getMessage() . "\n";
  }
}
}}}

`validate()` returns a boolean. If the validation failed, you can access the array  `ValidationFailed` objects by way of the `getValidationFailures()` method. Each `ValidationFailed` instance gives access to the column, the messagen and the validator that caused the failure.

== Core Validators ==

Propel bundles a set of validatorts that should help you deal with the most common cases.

=== !MatchValidator ===

The `MatchValidator` is used to run a regular expression of choice against the column. Note that this is a `preg`, not `ereg` (check [http://www.php.net/preg_match the preg_match documentation] for more information about regexps).

{{{
#!xml
<validator column="username">
  <!-- allow strings that match the email adress pattern -->
  <rule
    name="match"
    value="/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/"
    message="Please enter a valid email address." />
</validator>
{'fraction_non_alphanumeric': 0.12609191036840106, 'fraction_numerical': 0.006456513482719331, 'mean_word_length': 4.298792756539235, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2239844', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1495, 'n_words': 658}