This is the API docs for LemonStand V1, which has been discontinued. LemonStand is now a cloud based platform, available at lemonstand.com.

LemonStand API

Phpr_ValidationRules class

Defined in /phproad/modules/phpr/classes/phpr_validation.php, lines 447-1338
Author LemonStand eCommerce Inc.
Represents a set of validation rules. Objects of this class are usually created by Phpr_Validation::add() and Db_ColumnDefinition::validation() methods. Almost all methods of this class return the updated object. It allows to define rules as a chain:
$this->define_column('name', 'Name')->order('asc')->validation()->fn('trim')->required("Please specify the theme name.");
Rules are executed in the order they added. Some rules, like fn() can update the input value, instead of performing the actual validation. The updated value then used in other rules. If the validation object is used with models, the updated field values are assigned to the model properties before it is saved to the database.

Public methods

Method Description Defined By
alpha() checks whether the value contains only Latin characters. Phpr_ValidationRules
alphanum() checks whether the value contains only Latin characters and digits. Phpr_ValidationRules
email() checks whether the value is a valid email address. Phpr_ValidationRules
float() checks whether the value is a valid floating point number. Phpr_ValidationRules
fn() adds a rule that processes a value using a PHP function. Phpr_ValidationRules
ip() checks whether the value is a valid IP address. Phpr_ValidationRules
length() checks whether a value length matches the specified value. Phpr_ValidationRules
maxLength() checks whether a value is not longer than the specified length. Phpr_ValidationRules
method() adds a rule that validates a value with an owner class' method. Phpr_ValidationRules
minLength() checks whether a value is not shorter than the specified length. Phpr_ValidationRules
numeric() checks whether the value is a valid number. Phpr_ValidationRules
optional() makes the field optional. Phpr_ValidationRules
regexp() checks whether the value matches the specified regular expression. Phpr_ValidationRules
required() makes the field required. Phpr_ValidationRules
unique() checks whether a value is unique. Phpr_ValidationRules
url() checks whether the value is a valid URL. Phpr_ValidationRules

Method details

alpha() method

public Phpr_ValidationRules alpha(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value contains only Latin characters.

alphanum() method

public Phpr_ValidationRules alphanum(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value contains only Latin characters and digits.

email() method

public Phpr_ValidationRules email(boolean $allow_empty=false, string $custom_message=NULL)
$allow_empty boolean determines whether the value can be empty.
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value is a valid email address.

float() method

public Phpr_ValidationRules float(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value is a valid floating point number. Correct numeric values: 10, 10.0, -10.0.

fn() method

public Phpr_ValidationRules fn(string $name)
$name string specifies a PHP function name.
{return} Phpr_ValidationRules returns the updated rule set.
Adds a rule that processes a value using a PHP function. The function must accept a single parameter - the value and return a string or boolean value. The updated value is used by all following validation rules. Example:
$this->define_column('author_name', 'Author')->validation()->fn('trim');

ip() method

public Phpr_ValidationRules ip(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value is a valid IP address.

length() method

public Phpr_ValidationRules length(int $length, string $custom_message=NULL)
$length int specifies the required value length.
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether a value length matches the specified value.

maxLength() method

public Phpr_ValidationRules maxLength(int $length, string $custom_message=NULL)
$length int specifies the maximum value length.
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether a value is not longer than the specified length.

method() method

public Phpr_ValidationRules method(string $name)
$name string specifies the method name.
{return} Phpr_ValidationRules returns the updated rule set.
Adds a rule that validates a value with an owner class' method. Use this method with ActiveRecord models. The model class should contain a public method with the specified name. The should accept two parameters - the field name and value, and return a string or boolean value. Alternatively you can use setError() method of the validation object to throw an exception.
public function define_columns($context = null)
{
  $this->define_column('is_enabled', 'Enabled')->validation()->method('validate_enabled');
  ...
}

public function validate_enabled($name, $value)
{
  if (!$value && $this->is_default)
    $this->validation->setError('This theme is default and cannot be disabled.', $name, true);

  return $value;
}

minLength() method

public Phpr_ValidationRules minLength(int $length, string $custom_message=NULL)
$length int specifies the minimum value length.
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether a value is not shorter than the specified length.

numeric() method

public Phpr_ValidationRules numeric(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value is a valid number. Correct numeric values: 10, -10.

optional() method

public Phpr_ValidationRules optional()
{return} Phpr_ValidationRules returns the updated rule set.
Makes the field optional.

regexp() method

public Phpr_ValidationRules regexp(string $pattern, string $custom_message=NULL, boolean $allow_empty=false)
$pattern string specifies a Perl-compatible regular expression pattern.
$custom_message string specifies an error message to display if the validation fails.
$allow_empty boolean determines whether the value can be empty. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value matches the specified regular expression.

required() method

public Phpr_ValidationRules required(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Makes the field required.

unique() method

public Phpr_ValidationRules unique(string $custom_message=NULL, callback $checker_filter_callback=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
$checker_filter_callback callback specifies the required value length.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether a value is unique. This rule is applicable only when validation is used with a model. The rule creates a test object (an instance of the model class) to detect whether the value is unique.
By default, if the second parameter omitted, the rule checks whether the value is unique in the entire table. The second parameter allows to define a callback method in the model for configuring the test model object. The method should accept 3 parameters - the test object, the model object and the deferred session key value. Example:
public function define_columns($context = null)
{
  $this->define_column('file_name', 'File Name')->validation()
    ->unique('File name "%s" already used by another template.', array($this, 'configure_unique_validator'));
  ...
}

public function configure_unique_validator($checker, $page, $deferred_session_key)
{
  // Exclude pages from other themes
  $checker->where('theme_id=?', $page->theme_id);
}

url() method

public Phpr_ValidationRules url(string $custom_message=NULL)
$custom_message string specifies an error message to display if the validation fails. Can contain %s placeholder which is replaced with the actual field name.
{return} Phpr_ValidationRules returns the updated rule set.
Checks whether the value is a valid URL.