Reference

Guide

invalid()

Runs a number of validators on a set of data and checks if the data is invalid

invalid(array $data = [ ], array $rules = [ ], array $messages = [ ]): bool|array

Parameters

Name Type Default Description
$data array [ ]
$rules array [ ]
$messages array [ ]

Return type

bool|array

Examples

$data = [
  'username' => 'Homer',
  'fname'    => 'Homer',
  'lname'    => 'Simpson',
  'email'    => 'home@simpsonscom'
];

$rules = [
  'username' => array('required'),
  'fname'    => array('required', 'max' => 2),
  'lname'    => array('required', 'min' => 20),
  'email'    => array('required', 'email')
];

$messages = [
  'fname' => 'Please enter a valid first name',
  'lname' => 'Please enter a valid last name',
  'email' => 'Please enter a valid email address'
];

if($invalid = invalid($data, $rules, $messages)) {
  dump($invalid);
} else {
  dump('Nice data!');
}

You can also separately define a message for each validation rule:

$messages = [
  'fname' => 'Please enter a valid first name',
  'lname' => 'Please enter a valid last name',
  'email' => [
    'Please enter an email address',
    'The email address is invalid'
  ]
];

Source code

kirby/config/helpers.php#L297