Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Last active February 26, 2017 06:45
Show Gist options
  • Save aloha1003/318b03ec32f7c5c7a2cba6334013ac81 to your computer and use it in GitHub Desktop.
Save aloha1003/318b03ec32f7c5c7a2cba6334013ac81 to your computer and use it in GitHub Desktop.
動態調整validation 規則
<?php
class Model extends BaseModel {
public $defaultRuleAry = []; //預設規則
public function validation() { //驗證輸入
//根據 $this->defaultRuleAry 來驗證
return true;
}
public function save(array $options = array())
if ($this->validation()) {
return parent::save($options);
}
}
public function setRule($ruleAry = []) {
$this->defaultRuleAry = array_mere($this->defaultRuleAry, $ruleAry);
//針對空的規則先排除掉,或許你不希望執行預設規則某條規則,可以在ruleAry將那條規則設為null,透過array_filter會幫你把空的排除
$this->defaultRuleAry = array_filter($this->defaultRuleAry);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment