Last active
February 26, 2017 06:45
-
-
Save aloha1003/318b03ec32f7c5c7a2cba6334013ac81 to your computer and use it in GitHub Desktop.
動態調整validation 規則
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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