Framework  3.9
ValidationEngine Class Reference

The ValidationEngine takes an array of validator objects in its constructor, and is then able to generate both client- and server-side validation for HTML forms. More...

Public Member Functions

 ValidationEngine ()
 Create a new ValidationEngine. More...
 
 writeScript ()
 Output the client-side form validation function, based on all the validators that have been added. More...
 
 add ()
 Add a validator. More...
 
 getValidator ($field, $class)
 Get the specified validator by field and class. More...
 
 getValidators ($field)
 Get the validators attached to a specific field. More...
 
 validate ()
 Perform server-side validation of the posted form values. More...
 
 isRequired ($field)
 Checks to see whether the specified field has a RequiredValidator attached to it. More...
 
 hasRequiredFields ()
 Checks to see whether any fields have been marked as required. More...
 

Public Attributes

 $validators = array()
 The array of validators making up this validation engine. More...
 
 $msg = ""
 Validation error messages accumulator;. More...
 
 $id = ""
 The ID of this validator. More...
 
 $generateScript = true
 Specifies whether to generate client-side validation script. More...
 

Detailed Description

The ValidationEngine takes an array of validator objects in its constructor, and is then able to generate both client- and server-side validation for HTML forms.

Definition at line 1557 of file validation.inc.

Member Function Documentation

◆ add()

ValidationEngine::add ( )

Add a validator.

Definition at line 1606 of file validation.inc.

1607  {
1608  foreach(func_get_args() as $v)
1609  {
1610  $this->validators[] = $v;
1611  }
1612  }

◆ getValidator()

ValidationEngine::getValidator (   $field,
  $class 
)

Get the specified validator by field and class.

Parameters
unknown_type$fieldthe validator's field
unknown_type$classthe validator's class

Definition at line 1619 of file validation.inc.

1620  {
1621  foreach($this->validators as $v)
1622  {
1623  if ($v->field == $field && get_class($v) == $class)
1624  {
1625  return $v;
1626  }
1627  }
1628 
1629  return null;
1630  }

◆ getValidators()

ValidationEngine::getValidators (   $field)

Get the validators attached to a specific field.

Parameters
$fieldthe field

Definition at line 1636 of file validation.inc.

1637  {
1638  $vals = array();
1639  foreach($this->validators as $v)
1640  {
1641  if ($v->field == $field)
1642  {
1643  $vals[] = $v;
1644  }
1645  }
1646 
1647  return $vals;
1648  }

◆ hasRequiredFields()

ValidationEngine::hasRequiredFields ( )

Checks to see whether any fields have been marked as required.

Returns
true if there is at least one required field, false if no fields are required.

Definition at line 1692 of file validation.inc.

1693  {
1694  foreach($this->validators as $v)
1695  {
1696  if (get_class($v) == RequiredValidator) return true;
1697  }
1698 
1699  return false;
1700  }
RequiredField Validator.
Definition: validation.inc:76

◆ isRequired()

ValidationEngine::isRequired (   $field)

Checks to see whether the specified field has a RequiredValidator attached to it.

Parameters
$fieldthe field to check
Returns
true if the field is required, false if it is optional

Definition at line 1678 of file validation.inc.

1679  {
1680  foreach($this->validators as $v)
1681  {
1682  if ($v->field == $field && get_class($v) == RequiredValidator) return true;
1683  }
1684 
1685  return false;
1686  }

◆ validate()

ValidationEngine::validate ( )

Perform server-side validation of the posted form values.

The msg field of the ValidationEngine accumulates all the error messages raised by the individual validators.

Returns
true if the form data is valid for all validators, false if one or more fields contain invalid data.

Definition at line 1655 of file validation.inc.

1656  {
1657  $result = "";
1658 
1659  foreach($this->validators as $v)
1660  {
1661  $msg = $v->validate();
1662  if ($msg)
1663  {
1664  $result .= $msg."<br>";
1665  }
1666  }
1667 
1668  $this->msg = $msg;
1669 
1670  return $result;
1671  }
$msg
Validation error messages accumulator;.

◆ ValidationEngine()

ValidationEngine::ValidationEngine ( )

Create a new ValidationEngine.

Definition at line 1567 of file validation.inc.

1568  {
1569  $this->validators = func_get_args();
1570  }

◆ writeScript()

ValidationEngine::writeScript ( )

Output the client-side form validation function, based on all the validators that have been added.

Definition at line 1575 of file validation.inc.

1576  {
1577  $suffix = ($this->id) ? "_{$this->id}" : "";
1578 
1579  $script = <<<ENDSCRIPT
1580 
1581 function validate{$suffix}(form)
1582 {
1583 ENDSCRIPT;
1584 
1585  if ($this->generateScript)
1586  {
1587  foreach($this->validators as $v)
1588  {
1589  $script .= $v->writeClient();
1590  }
1591  }
1592 
1593  $script .= <<<ENDSCRIPT
1594 
1595  return true;
1596 }
1597 
1598 ENDSCRIPT;
1599 
1600  return $script;
1601  }
validate()
Perform server-side validation of the posted form values.
$id
The ID of this validator.

Member Data Documentation

◆ $generateScript

ValidationEngine::$generateScript = true

Specifies whether to generate client-side validation script.

Definition at line 1562 of file validation.inc.

◆ $id

ValidationEngine::$id = ""

The ID of this validator.

Definition at line 1561 of file validation.inc.

◆ $msg

ValidationEngine::$msg = ""

Validation error messages accumulator;.

Definition at line 1560 of file validation.inc.

◆ $validators

ValidationEngine::$validators = array()

The array of validators making up this validation engine.

Definition at line 1559 of file validation.inc.


The documentation for this class was generated from the following file: