Oriancci comes with some pre-built variables for determining the validity of data.
class User extends \Oriancci\Model
{
static $validation = [
'firstName' => ['required' => true],
'lastName' => ['required' => true],
'email' => ['email' => true],
'birthday' => ['age' => ['min' => 18]]
];
}
Ensures that the value is not null
and not empty
.
Ensures that the value does not currently exist in the database under the same field.
Ensures that the value is a valid integer.
Ensures that the value is a valid floating-point number.
Ensures that the value only contains alphabetic characters.
Ensures that the value only contains alpha-numeric characters.
Ensures that the value is a valid email address.
Ensures that the length of the value is between two values.
Ensures that the value matches a regular expression.
Ensures that the value matches a regular expression (surrounded by ^ and $).
Ensures that the value in the date field is of a particular age.
You can declare your own validation methods by overring the Model->validate()
method. This can be used in conjunction with the declarations made in static::$validation
.
use Oriancci\Error;
class User extends \Oriancci\Model
{
static $validation = [
'firstName' => ['required' => true],
'lastName' => ['required' => true],
'email' => ['email' => true],
'birthday' => ['age' => ['min' => 18]]
];
public function validate()
{
if (empty($this->firstName)) {
$this->errors->append(new Error(['code' => 'REQUIRED']));
}
}
}