Yii 1.1: pcclassexistsvalidator

Validator class to make sure a value assigned is a valid class name
3 followers

This extension provides a validator class that enables to define an AR class attribute to be containing a valid class name. This is useful in occasions such as when you have a class file that refers to other models in your system. For example, 'views counter', 'like functionality' and more... .

Usage

  • This extension is designed as an external validation class. To use it, insert snippet like the one demonstrated below to the relevant AR class 'rules' method:
public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        // more rules here...
        array('model_name', 'PcClassExistsValidator'),
        // more rules here...
    );
}
  • In order to change its configuration options, pass them as parameters in the array shown above (in rules()).
  • You have to make sure that each class that its name is supposed to be the value of the property you want to 'validate' using this validator class, has been 'imported' by Yii before the validation takes place. This typically means updating the 'import' attribute of CWebApplication in config/main.php config file in advance. I didn't find any slicker solution that will achieve "presence" of the class names in PHP's global namespace prior to running the validation process (if you have an idea please let me know).
  • Validator will output log messages (using Yii::log) in case of errors for easy debugging (if you're using the log facility).

Configuration options:

The following are the class public attributes via which the configuration is passed, along with their default values:

/* @var bool $allowEmpty Whether the attribute is allowed to be empty. */
public $allowEmpty = false;
/* @var string $emptyMessage the message to be displayed if an empty value is validated while 'allowEmpty' is false */
public $emptyMessage = "{attribute} cannot be blank";
public $errorMessage = " is not a valid class name. Make sure to configure config/main.php 'import' section to load all relevant classes.";

Resources

Change Log

  • v1.0 (23 Dec 2012): Initial version release.

Be the first person to leave a comment

Please to leave your comment.

Create extension