CakePHP验证数组适用于许多领域

In a cake model I have 6 fields that all have the same validation rules. Is there a way to assign this rule to all 6 fields without having to copy-paste the array?

E.g.

public $currency_validate = array(
        'rule1'=>array(
            'rule'    => array('comparison', '>=', 0),
            'message' => 'Must be between 0 and 1'
            ),
        'rule2' => array(
            'rule'    => array('comparison', '<=', 1),
            'message' => 'Must be between 0 and 1'
            )
        );
public $validate = array(
            'usd' => $this->currency_validate,
            'gbp' => $this->currency_validate,
            'eur' => $this->currency_validate,
            //etc
        );

Does not work as one is not allowed to dynamical assign properties in a class.

You can add rules into the beforeValidate(array $options = array()) method:

public $currency_validate = array(
    'rule1'=>array(
        'rule'    => array('comparison', '>=', 0),
        'message' => 'Must be between 0 and 1'
        ),
    'rule2' => array(
        'rule'    => array('comparison', '<=', 1),
        'message' => 'Must be between 0 and 1'
        )
    );
public $validate = array(
        'usd' => array(),
        'gbp' => array(),
        'eur' => array(),
        //etc
    );

public function beforeValidate(array $options = array()) {
    $this->validate = array(
        'usd' => $this->currency_validate,
        'gbp' => $this->currency_validate,
        'eur' => $this->currency_validate,
        //etc
    );
    return true; //otherwise the current save() execution will abort 
}