Commit 518bc86b by Qiang Xue

w

parent 5f8a714e
...@@ -330,20 +330,20 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -330,20 +330,20 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Creates validator objects based on the specification in {@link rules}. * Creates validator objects based on the validation rules specified in [[rules]].
* This method is mainly used internally. * Unlike [[getValidators]], calling this method each time, it will return a new list of validators.
* @return Vector validators built based on {@link rules()}. * @return Vector validators
*/ */
public function createValidators() public function createValidators()
{ {
$validators = new Vector; $validators = new Vector;
foreach ($this->rules() as $rule) { foreach ($this->rules() as $rule) {
if (isset($rule[0], $rule[1])) { // attributes, validator type if (isset($rule[0], $rule[1])) { // attributes, validator type
$validators->add(\yii\validators\Validator::createValidator($rule[1], $this, $rule[0], array_slice($rule, 2))); $validator = \yii\validators\Validator::createValidator($rule[1], $this, $rule[0], array_slice($rule, 2));
$validators->add($validator);
} }
else { else {
throw new Exception(spr'{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.', throw new Exception('Invalid validation rule: a rule must specify both attribute names and validator type.');
array('{class}' => get_class($this))));
} }
} }
return $validators; return $validators;
...@@ -352,17 +352,18 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -352,17 +352,18 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
/** /**
* Returns a value indicating whether the attribute is required. * Returns a value indicating whether the attribute is required.
* This is determined by checking if the attribute is associated with a * This is determined by checking if the attribute is associated with a
* {@link CRequiredValidator} validation rule in the current {@link scenario}. * [[\yii\validators\RequiredValidator|required]] validation rule in the
* current {@link scenario}.
* @param string $attribute attribute name * @param string $attribute attribute name
* @return boolean whether the attribute is required * @return boolean whether the attribute is required
*/ */
public function isAttributeRequired($attribute) public function isAttributeRequired($attribute)
{ {
foreach ($this->getActiveValidators($attribute) as $validator) foreach ($this->getActiveValidators($attribute) as $validator) {
{ if ($validator instanceof \yii\validators\RequiredValidator) {
if ($validator instanceof CRequiredValidator)
return true; return true;
} }
}
return false; return false;
} }
...@@ -373,8 +374,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -373,8 +374,13 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/ */
public function isAttributeSafe($attribute) public function isAttributeSafe($attribute)
{ {
$attributes = $this->getSafeAttributeNames(); $validators = $this->getActiveValidators();
return in_array($attribute, $attributes); foreach ($validators as $validator) {
if (!$validator->safe) {
return false;
}
}
return $validators !== array();
} }
/** /**
...@@ -387,12 +393,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -387,12 +393,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
public function getAttributeLabel($attribute) public function getAttributeLabel($attribute)
{ {
$labels = $this->attributeLabels(); $labels = $this->attributeLabels();
if (isset($labels[$attribute])) { return isset($labels[$attribute]) ? $labels[$attribute] : $this->generateAttributeLabel($attribute)
return $labels[$attribute];
}
else {
return $this->generateAttributeLabel($attribute);
}
} }
/** /**
...@@ -402,12 +403,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -402,12 +403,7 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/ */
public function hasErrors($attribute = null) public function hasErrors($attribute = null)
{ {
if ($attribute === null) { return $attribute === null ? !empty($this->_errors) : isset($this->_errors[$attribute]);
return !empty($this->_errors);
}
else {
return isset($this->_errors[$attribute]);
}
} }
/** /**
...@@ -559,21 +555,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -559,21 +555,6 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
} }
/** /**
* Sets the attributes to be null.
* @param array $names list of attributes to be set null. If this parameter is not given,
* all attributes as specified by [[attributeNames]] will have their values unset.
*/
public function unsetAttributes($names = null)
{
if ($names === null) {
$names = $this->attributeNames();
}
foreach ($names as $name) {
$this->$name = null;
}
}
/**
* This method is invoked when an unsafe attribute is being massively assigned. * This method is invoked when an unsafe attribute is being massively assigned.
* The default implementation will log a warning message if YII_DEBUG is on. * The default implementation will log a warning message if YII_DEBUG is on.
* It does nothing otherwise. * It does nothing otherwise.
...@@ -582,9 +563,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess ...@@ -582,9 +563,10 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
*/ */
public function onUnsafeAttribute($name, $value) public function onUnsafeAttribute($name, $value)
{ {
if (YII_DEBUG) if (YII_DEBUG) {
\Yii::warning(sprintf('Failed to set unsafe attribute "%s" in "%s".', $name, get_class($this)); \Yii::warning(sprintf('Failed to set unsafe attribute "%s" in "%s".', $name, get_class($this));
} }
}
/** /**
* Returns the scenario that this model is used in. * Returns the scenario that this model is used in.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment