Yii2 source code learning notes (13th), yii2 source code learning notes _ PHP Tutorial

Source: Internet
Author: User
Yii2 source code learning notes (13th) and yii2 source code learning notes. Yii2 source code learning note (13th), yii2 source code learning Note model class DynamicModel is mainly used to implement data verification in the model yii2baseDynamicModel. php1? Php2 ** 3 * @ link yii2 source code learning notes (13th) and yii2 source code learning Notes

DynamicModel is used to verify data in the model. yii2 \ base \ DynamicModel. php

1
 128], 21 * ['email ', 'Email'], 22 *]); 23 * if ($ model-> hasErrors ()) {24 * // validation fails 25 *} else {26 * // validation succeeds 27 *} 28 *} 29*"30*31 * The above example shows how to validate' $ name 'and' $ email 'with the help of DynamicModel. 32 * The example above demonstrates how to use DynamicModel to verify The username '$ name' and The mailbox' $ email '33 * The [[validateData ()] method creates an instance of DynamicModel, defines the att Ributes 34 * using the given data ('name' and 'Email 'in this example), and then CILS [[Model: validate ()]. the 35 * validateData () method creates a DynamicModel instance object. Define the Model feature by specifying the data, and then call the Model: validate () method. 36 * You can check the validation result by [[hasErrors ()], like you do with a normal model. 37 * You may also access the dynamic attributes defined through the model instance, e.g ., 38 * you can use [[hasErrors ()] to obtain the verification result 39 * '$ model-> name' and' $ model-> email '. 40*41 * Alternatively, you may use the following more "classic" syntax to perform ad-hoc data validation: 42, you can also use the following more "classic (traditional )" Syntax to execute temporary data verification 43 * "php 44 * $ model = new DynamicModel (compact ('name', 'Email ')); 45 * $ model-> addRule (['name', 'Email '], 'string', ['Max' => 128]) 46 *-> addRule ('email ', 'Email') 47 *-> validate (); 48*"49*50 * DynamicModel implements the above ad-hoc data validation feature by supporting the so-called 51 *" dynamic attributes ". it basically allows an attribute to be defined dynamically through Its constructor 52 * or [[defineAttribute ()]. 53 * implements the "dynamic attribute" supported by the preceding special data model verification function ". You can use its constructor or [[defineAttribute ()] to define an attribute 54 * @ author Qiang Xue.
 
  
55 * @ since 2.0 56 */57 class DynamicModel extends Model 58 {59 private $ _ attributes = []; // the dynamic attributes in the dynamic model are 60 61 62/** 63 * Constructors. constructor is used to assign passed attributes to _ attributes. it is convenient to use 64 * @ param array $ attributes the dynamic attributes (name-value pairs, or names) being defined the defined dynamic attribute 65 * @ param array $ config the configuration array to be applied to this object. configuration array used for this object. 66 */67 public function _ construct (array $ attributes = [], $ config = []) 68 {69 foreach ($ attributes as $ name => $ value) {// traverse the passed property 70 if (is_integer ($ name) {// if it is an integer, only the property name is passed in, write the attribute name to _ attributes 71 $ this-> _ attributes [$ value] = null; 72} else {73 $ this-> _ attributes [$ name] = $ value; // key-value pairs are written into 74} 75} 76 parent ::__ construct ($ config ); // call the parent class configuration 77} 78 79/** 80 * @ inheritdoc override _ get method, from _ attrib In utes, set the value to 81 */82 public function _ get ($ name) 83 {84 if (array_key_exists ($ name, $ this-> _ attributes )) {85 // if the passed $ name exists in the array _ attributes, the value 86 return $ this-> _ attributes [$ name] from _ attributes; 87} else {// otherwise, the _ get method of the parent class is called to obtain the attribute value 88 return parent ::__ get ($ name ); 89} 90} 91 92/** 93 * @ inheritdoc rewrite _ set method, set the _ attributes value 94 */95 public function _ set ($ name, $ value) 96 {97 if (array_key_exists ($ name, $ This-> _ attributes) {98 // if the passed $ name exists in array _ attributes, set the value of the dynamic attribute $ name to $ value 99 $ this-> _ attributes [$ name] = $ value; 100} else {101 parent :: __set ($ name, $ value ); // call the _ set method of the parent class to set the attribute value 102} 103} 104 105/** 106 * @ inheritdoc to rewrite the _ isset method as above, determine whether to set the $ name value in _ attributes to 107 */108 public function _ isset ($ name) 109 {110 if (array_key_exists ($ name, $ this-> _ attributes )) {111 return isset ($ this-> _ attributes [$ name ]); 112} else {113 return parent ::__ isset ($ name); 114} 115} 116 117/** 118 * @ inheritdoc same as above, rewrite the _ unset method, delete the $ name attribute value 119 */120 public function _ unset ($ name) 121 {122 if (array_key_exists ($ name, $ this-> _ attributes) in _ attributes )) {123 unset ($ this-> _ attributes [$ name]); 124} else {125 parent ::__ unset ($ name ); 126} 127} 128 129/** 130 * Defines an attribute. method for defining dynamic attributes: 131 * @ param string $ name the attribute n Ame attribute name 132 * @ param mixed $ value the attribute value 133 */134 public function defineAttribute ($ name, $ value = null) 135 {136 $ this-> _ attributes [$ name] = $ value; 137} 138 139/** 140 * Undefines an attribute. method for deleting dynamic attributes: 141 * @ param string $ name the attribute name attribute 142 */143 public function undefineAttribute ($ name) 144 {145 unset ($ this-> _ attributes [$ name]); 146} 147 148/** 149 * Adds a validation Rule to this model. add verification rules 150 * You can also directly manipulate [[validators] to add or remove validation rules.151 * This method provides a shortcut.152 * You can directly call [validators] to add or delete a verification rule. verification rules, this method provides a short method 153 * @ param string | array $ attributes the attribute (s) to be validated by the rule to verify the attribute 154 * @ param mixed $ validator the validator for the rule. this can be a built-in validator name, 155 * a method Name of the model class, an anonymous function, or a validator class name.156 * rule verification. This is the name of a built-in validators, the method name of a model class, the name of an anonymous function or a validators class. 157 * @ param array $ options the options (name-value pairs) to be applied to the validator158 * (name-value) applied to validators 159 * @ return static the model itself 160 */161 public function addRule ($ attributes, $ validator, $ options = []) 162 {163 $ validators = $ this-> getValidators (); // All verification rule objects 164 // Generate a Validator object, and insert the 165 $ validators-> append (Validator: createValidator ($ validator, $ this, (array) $ Attributes, $ options); 166 167 return $ this; 168} 169 170/** 171 * Validates the given data with the specified validation rules. use the specified rule to verify that the given data is 172 * This method will create a DynamicModel instance, populate it with the data to be validated, 173 * create the specified validation rules, and then validate the data using these rules.174 * @ param array $ data the data (name-value pairs) to be validated175 * @ Param array $ rules the validation rules. please refer to [[Model: rules ()] on the format of this parameter.176 * @ return static the model instance that contains the data being validated177 * @ throws InvalidConfigException if a validation rule is not specified correctly.178 */179 public static function validateData (array $ data, $ rules = []) 180 {181/* @ var $ model DynamicModel */182 $ model = ne W static ($ data); // instantiate the call class, assign $ data to _ attributes183 if (! Empty ($ rules) {184 $ validators = $ model-> getValidators (); // Obtain all defined validation rules 185 foreach ($ rules as $ rule) {186 if ($ rule instanceof Validator) {187 $ validators-> append ($ rule); // if $ rule is an instance of Validator, add to $ validators 188} elseif (is_array ($ rule) & isset ($ rule [0], $ rule [1]) {// attributes, validator type189 // if $ rule is an array, judge whether the dynamic attribute and verification type exist. create a Validator object and add it to $ validators: 190 $ validator = Validator :: createValidator ($ rule [1], $ model, (array) $ rule [0], array_slice ($ rule, 2); 191 $ validators-> append ($ validator ); 192} else {// throw an exception 193 throw new InvalidConfigException ('invalid validation rule: a rule must specify both attribute names and validator type. '); 194} 195} 196} 197 198 $ model-> validate (); // run the verification 199 200 return $ model; 201} 202 203/** 204 * @ inheritdoc returns all dynamic attributes 205 */206 public function attributes () 207 {208 return array_keys ($ this-> _ attributes ); 209} 210}
 

Example (13), yii2 source code learning Note model class DynamicModel is mainly used to implement data verification in the model yii2 \ base \ DynamicModel. php 1? Php 2/** 3 * @ link http ://...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.