use ModelValidate\Validator; class User extends Model { public $name; } $validator = new Validator(); $validator->rule('name', 'required'); $user = new User(['name' => '']); if (! $validator->validate($user)) { echo $validator->errors('name'); }
$validator->rule('email', 'email'); $user = new User(['email' => 'invalid_email']); if (! $validator->validate($user)) { echo $validator->errors('email'); }
class User extends Model { public $email; } $validator->rule('email', 'unique', function($value, $object) { $users = User::find_all(['email' => $value]); return sizeof($users) == 0; }); $user1 = User::create(['email' => 'test@example.com']); $user2 = new User(['email' => 'test@example.com']); if (! $validator->validate($user2)) { echo $validator->errors('email'); }Overall, PHP Model Validate is a useful library for validating model attributes in PHP applications. It offers many different rules and allows custom validation functions to be used.