Skip to content

Commit

Permalink
allow form behaviors to alter owner model rules instead of just appen…
Browse files Browse the repository at this point in the history
…ding to them
  • Loading branch information
Jan Was committed Jan 30, 2015
1 parent 12504b0 commit 5b18747
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions components/CaptchaFormBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class CaptchaFormBehavior extends FormModelBehavior
/**
* @inheritdoc
*/
public function rules()
public function filterRules($rules = array())
{
$module = Yii::app()->controller !== null ? Yii::app()->controller->module : null;
$rules = array(
$behaviorRules = array(
array('verifyCode', 'captcha', 'captchaAction'=>($module !== null ? $module->getId() : 'usr').'/default/captcha'),
);
return $this->applyRuleOptions($rules);
return array_merge($rules, $this->applyRuleOptions($behaviorRules));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions components/ExpiredPasswordBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public function setPasswordTimeout($value)
/**
* @inheritdoc
*/
public function rules()
public function filterRules($rules = array())
{
$rules = array(
$behaviorRules = array(
array('password', 'passwordHasNotExpired', 'except'=>'reset, hybridauth, verifyOTP'),
);
return $this->applyRuleOptions($rules);
return array_merge($rules, $this->applyRuleOptions($behaviorRules));
}

public function passwordHasNotExpired()
Expand Down
6 changes: 3 additions & 3 deletions components/FormModelBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ abstract class FormModelBehavior extends CModelBehavior
private $_ruleOptions = array();

/**
* Validation rules for attributes of this behavior, that should be merged with rules in the owner model.
* Adds validation rules for attributes of this behavior or removes rules from the owner model.
* @return array validation rules
* @see CModel::rules()
*/
public function rules()
public function filterRules($rules = array())
{
return array();
return $rules;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions components/OneTimePasswordFormBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function events() {
/**
* @inheritdoc
*/
public function rules()
public function filterRules($rules = array())
{
$rules = array(
$behaviorRules = array(
array('oneTimePassword', 'filter', 'filter'=>'trim', 'on'=>'verifyOTP'),
array('oneTimePassword', 'default', 'setOnEmpty'=>true, 'value' => null, 'on'=>'verifyOTP'),
array('oneTimePassword', 'required', 'on'=>'verifyOTP'),
array('oneTimePassword', 'validOneTimePassword', 'except'=>'hybridauth'),
);
return $this->applyRuleOptions($rules);
return array_merge($rules, $this->applyRuleOptions($behaviorRules));
}

/**
Expand Down
9 changes: 5 additions & 4 deletions models/BaseUsrForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,17 @@ public function getBehaviorLabels()
}

/**
* Returns rules defined in attached behaviors that extend FormModelBehavior.
* Filters base rules through each attached behavior that extend FormModelBehavior,
* which may add their own rules or remove existing ones.
* @param array base form model rules
* @return array validation rules
* @see CModel::rules()
*/
public function getBehaviorRules()
public function filterRules($rules)
{
$rules = array();
foreach($this->_behaviors as $name=>$foo) {
if (($behavior=$this->asa($name)) instanceof FormModelBehavior)
$rules = array_merge($rules, $behavior->rules());
$rules = $behavior->filterRules($rules);
}
return $rules;
}
Expand Down
4 changes: 2 additions & 2 deletions models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public function getAvailableScenarios()
*/
public function rules()
{
$rules = array_merge(array(
$rules = $this->filterRules(array_merge(array(
array('username, password', 'filter', 'filter'=>'trim'),
array('username, password', 'required'),
array('rememberMe', 'boolean'),
array('password', 'authenticate'),
), $this->rulesAddScenario(parent::rules(), 'reset'), $this->getBehaviorRules());
), $this->rulesAddScenario(parent::rules(), 'reset')));

return $rules;
}
Expand Down
4 changes: 2 additions & 2 deletions models/ProfileForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function setPictureUploadRules($rules)
*/
public function rules()
{
return array_merge($this->getBehaviorRules(), array(
return $this->filterRules(array_merge(array(
array('username, email, firstName, lastName, removePicture', 'filter', 'filter'=>'trim'),
array('username, email, firstName, lastName, removePicture', 'default', 'setOnEmpty'=>true, 'value' => null),

Expand All @@ -61,7 +61,7 @@ public function rules()
array('email', 'email'),
array('removePicture', 'boolean'),
array('password', 'validCurrentPassword', 'except'=>'register'),
), $this->pictureUploadRules);
), $this->pictureUploadRules));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions models/RecoveryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RecoveryForm extends BasePasswordForm
* and password needs to be authenticated.
*/
public function rules() {
$rules = array_merge($this->getBehaviorRules(), array(
$rules = $this->filterRules(array_merge(array(
array('username, email', 'filter', 'filter'=>'trim'),
array('username, email', 'default', 'setOnEmpty'=>true, 'value' => null),
array('username, email', 'existingIdentity'),
Expand All @@ -32,7 +32,7 @@ public function rules() {
array('activationKey', 'default', 'setOnEmpty'=>true, 'value' => null, 'on'=>'reset,verify'),
array('activationKey', 'required', 'on'=>'reset,verify'),
array('activationKey', 'validActivationKey', 'on'=>'reset,verify'),
), $this->rulesAddScenario(parent::rules(), 'reset'));
), $this->rulesAddScenario(parent::rules(), 'reset')));

return $rules;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/LoginFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testWithBehavior()
$formLabels = $form->attributeLabels();
$form->attachBehavior('captcha', array('class' => 'CaptchaFormBehavior'));
$behaviorAttributes = $form->asa('captcha')->attributeNames();
$behaviorRules = $form->asa('captcha')->rules();
$behaviorRules = $form->asa('captcha')->filterRules();
$behaviorLabels = $form->asa('captcha')->attributeLabels();
$this->assertEquals(array_merge($formAttributes, $behaviorAttributes), $form->attributeNames());
$this->assertEquals(array_merge($formRules, $behaviorRules), $form->rules());
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/ProfileFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function testWithBehavior()
$formLabels = $form->attributeLabels();
$form->attachBehavior('captcha', array('class' => 'CaptchaFormBehavior'));
$behaviorAttributes = $form->asa('captcha')->attributeNames();
$behaviorRules = $form->asa('captcha')->rules();
$behaviorRules = $form->asa('captcha')->filterRules();
$behaviorLabels = $form->asa('captcha')->attributeLabels();
$this->assertEquals(array_merge($formAttributes, $behaviorAttributes), $form->attributeNames());
$this->assertEquals(array_merge($behaviorRules, $formRules), $form->rules());
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/RecoveryFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testWithBehavior()
$formLabels = $form->attributeLabels();
$form->attachBehavior('captcha', array('class' => 'CaptchaFormBehavior'));
$behaviorAttributes = $form->asa('captcha')->attributeNames();
$behaviorRules = $form->asa('captcha')->rules();
$behaviorRules = $form->asa('captcha')->filterRules();
$behaviorLabels = $form->asa('captcha')->attributeLabels();
$this->assertEquals(array_merge($formAttributes, $behaviorAttributes), $form->attributeNames());
$this->assertEquals(array_merge($behaviorRules, $formRules), $form->rules());
Expand Down

0 comments on commit 5b18747

Please sign in to comment.