mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-12 11:40:19 +08:00
w
This commit is contained in:
@@ -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,16 +352,17 @@ 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
|
|||||||
*/
|
*/
|
||||||
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
|
|||||||
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
|
|||||||
*/
|
*/
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -558,21 +554,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.
|
||||||
@@ -582,8 +563,9 @@ 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user