// define a CActiveRecord class class User extends CActiveRecord { public function rules() { return array( array('name, email', 'required'), array('email', 'email'), ); } public function afterValidate() { // do something after validation if ($this->isNewRecord) { $this->created_at = time(); } else { $this->updated_at = time(); } return parent::afterValidate(); } } // create a user object $user = new User(); // set user attributes $user->name = 'John Doe'; $user->email = 'john.doe@example.com'; // validate user if ($user->validate()) { // save user $user->save(); }
// define a CActiveRecord class class Product extends CActiveRecord { public function rules() { return array( array('name, description, price', 'required'), array('price', 'numerical', 'integerOnly'=>true), ); } public function afterValidate() { // do something after validation if ($this->price > 100) { $this->price = 100; } return parent::afterValidate(); } } // create a product object $product = new Product(); // set product attributes $product->name = 'Apple iPhone XR'; $product->description = '6.1-inch Liquid Retina display, A12 Bionic chip, Face ID, and more'; $product->price = 1200; // validate product if ($product->validate()) { // save product $product->save(); echo "Product saved successfully!"; } else { echo "Validation failed!"; }In the above example, afterValidate() method is used to check the product price. If the price is greater than 100, it is automatically set to 100. This is done after the validation of the product has been successfully completed. Package Library: Yii Framework (CActiveRecord is a core component of Yii Framework)