Class Ice

Validation

Allows to validate array data.

  $validation = new Ice\Validation();
  
  $validation->rules([
      'fullName' => 'required',
      'email' => 'required|email',
      'repeatEmail' => 'same:email',
      'about' => 'required|length:10,5000',
  ]);
  
  $valid = $validation->validate($_POST);
  
  if (!$valid) {
      $messages = $validation->getMessages();
  }
 
Methods
getDi()
setRules(variable $rules)
setFilters(variable $filters)
setLabels(variable $labels)
setAliases(variable $aliases)
setTranslate(variable $translate)
setHumanLabels(variable $humanLabels)
__construct(array $data)
Validation constructor. Fetch Di and set the data if given.
resolve(string $alias, string $field, variable $options)
Resolve one rule.
rule(string $field, variable $validators, variable $options)
Add one rule.
rules(array $validators, bool $merge)
Add multiple rules at once.
bool
validate(array $data, bool $clear)
Validate the data.
bool
valid()
Check if validation passed.
bool
hasValue(string $field)
Whether or not a value exists by field.
getValue(string $field, bool $filtered)
Get a value by field.
getValues(variable $fields, bool $filtered)
Get the values by fields.
string
getLabel(string $field)
Get the label of a field.
setDefaultMessages(array $messages)
Set the default messages.
string
getDefaultMessage(string $type)
Get a default message for the type.
addMessage(string $field, string $message)
Add a message to the field.
getMessages()
Get the validation's messages.
Methods Details
  • public function getDi()

  • public function getData()

  • public function setRules(variable $rules)

  • public function getRules()

  • public function setFilters(variable $filters)

  • public function setLabels(variable $labels)

  • public function setAliases(variable $aliases)

  • public function setTranslate(variable $translate)

  • public function getTranslate()

  • public function setHumanLabels(variable $humanLabels)

  • public function __construct(array $data)

    Validation constructor. Fetch Di and set the data if given.
  • public function resolve(string $alias, string $field, variable $options)

    Resolve one rule.
  • public function rule(string $field, variable $validators, variable $options)

    Add one rule.
    
          $validation = new Ice\Validation();
          
          $validation->rule('email', 'required|email');
          $validation->rule('content', [
              'length' => [
                  'max' => 1000,
                  'messageMin' => 'Too long!',
                  'label' => 'Desctiption'
              ]
          ]);
         
  • public function rules(array $validators, bool $merge)

    Add multiple rules at once.
    
          $validation = new Ice\Validation();
          
          $validation->rules([
              'username' => 'required|length:4,24|notIn:admin,user,root|unique:users',
              'password'  => 'required|length:5,32',
              'repeatPassword'  => 'same:password',
              'email'  => 'email',
              'status'  => 'required|digit|in:0,1,2',
              'website'  => 'url',
              'title'  => 'length:,100',
              'age'  => 'required|between:18,21',
          ]);
         
  • public function validate(array $data, bool $clear)

    Validate the data.
  • public function valid()

    Check if validation passed.
  • public function hasValue(string $field)

    Whether or not a value exists by field.
  • public function getValue(string $field, bool $filtered)

    Get a value by field.
  • public function getValues(variable $fields, bool $filtered)

    Get the values by fields.
    Values are automatically filtered out if filters have been setted.
    
          // Get value for one field
          $validation->getValues('password');
          
          // Get values for multiple fields
          $validation->getValues(['fullName', 'about']);
          
          // Get all values
          $validation->getValues();
         
  • public function getLabel(string $field)

    Get the label of a field.
    Humanize a label if humanLabels attribute and filter service is available
  • public function setDefaultMessages(array $messages)

    Set the default messages.
  • public function getDefaultMessage(string $type)

    Get a default message for the type.
  • public function addMessage(string $field, string $message)

    Add a message to the field.
  • public function getMessages()

    Get the validation's messages.
  • internal function zephir_init_properties_Ice_Validation()