we would prefer to see://new object to data table $result=$Teacher->validate (true),Save (); Not://new object to data table $result=$Teacher->validate (true)->save ($Teacher-GetData ()); The reason is very simple, the above writing is the true face Object. The correct idea is that we assign a value to an object and then validate the Object. obviously, the above wording is closer to what we really think. Thinkphp is an open source software that we can find in github. Will the next contributor be you? here, we rewrite the Think\model and Think\validate classes in order to achieve better Validation. If you do not want to move the source code of the framework, you can skip the following sections in this section BELOW. Only need to be later involved in the validation operation, using the$Teacher->validate (true)->save ($Teacher-GetData ()); first, the Think\model class file is located in thinkphp/library/think/model.php 1, we find the save method, before Overwriting: public functionSave$data= [],$where= [],$sequence=NULL) { if(!Empty($data)) { //data automatically verify the strength of Qian Feng php-php training if(!$this->validatedata ($data)) { return false; } //assigning values to data Objects foreach($data as $key=$value) { $this->setattr ($key,$value,$data); } if(!Empty($where)) { $this->isupdate =true; } } //detection Field if(!Empty($this-FIELD)) { $this-DB (); foreach($this->data as $key=$val) { if(!In_array($key,$this-FIELD)) { unset($this->data[$key]); } } }...after Rewriting: public functionSave$data= [],$where= [],$sequence=NULL) { if(!Empty($data)) { //Automatic data validation if(!$this->validatedata ($data)) { return false; } //assigning values to data Objects foreach($data as $key=$value) { $this->setattr ($key,$value,$data); } if(!Empty($where)) { $this->isupdate =true; } //If no data is passed in, the new and updated are verified Separately---dream Maxnerva}Else { //updated to verify only the changed values if($this->isupdate &&!Empty($this-change )) { foreach($this->change as $value) { $data[$value] =$this->getdata ($value); } //new, Verify all Values}Else { $data=$this-GetData (); } //Automatic data validation if(!$this->validatedata ($data,$this-Isupdate)) { return false; } } //detection Field if(!Empty($this-FIELD)) { $this-DB (); foreach($this->data as $key=$val) { if(!In_array($key,$this-FIELD)) { unset($this->data[$key]); } } }... 2, we'll Find the ValidateData method in the file before Overwriting:/** * Automatically verify data * @access protected * @param array $data Verify data * @return BOOL*/ protected functionValidateData ($data)...$validate= Loader::validate ($name); if(!Empty($scene)) { $validate->scene ($scene); } } if(!$validate->check ($data)) {...after Rewriting:/** * Automatically verify data * @access protected * @param array $data Verify data * @param bool $isUpdate * @return BOOL */ protected functionValidateData ($data,$isUpdate=false) ...$validate= Loader::validate ($name); if(!Empty($scene)) { $validate->scene ($scene); } } //the Data Update operation verifies only the fields that have Changed---dream Maxnerva if($isUpdate) { $validate->remakerule ($data); } if(!$validate->check ($data)) {...second, The Think\validate class file is located in thinkphp/library/think/validate.PHP Adds a Remakerule method to the class, such as after I add it after the rule () method. /** * Regenerate rule (delete update field rule not present in Rule) * @param array $datas the data to validate * @return */ public functionRemakerule ($datas= []) { $rule= []; if(Is_array($datas)) { foreach($datas as $key=$data) { if(array_key_exists($key,$this-Rule)) { $rule[$key] =$this->rule[$key]; } } } $this->rule =$rule; With the above code, we can use this in the C layer. //new object to data table $result=$Teacher->validate (true),Save (); Test:
ThinkPHP5.0 change the validation method for the Framework: object->validate (true)->save ();