快速创建实例
@param array $attributes
@param boolean $runValidation
@return null|ActiveRecord
180803
public static function create(array $attributes, $runValidation = true)
    {
        $model = new static();
        $model->load($attributes, '');
        if ($model->save($runValidation)) {
            return $model;
        }
        return null;
    }
685137
public function actionValidate()
    {
        $rr = new RequestResponse();

        if (\Yii::$app->request->isAjax && !\Yii::$app->request->isPjax) {
            if (\Yii::$app->request->post('sx-model') && \Yii::$app->request->post('sx-model-value')) {
                $modelClass = \Yii::$app->request->post('sx-model');
                $modelValue = \Yii::$app->request->post('sx-model-value');

                /**
                 * @var $modelForm Form2Form
                 */
                $modelForm = $modelClass::find()->where(['id' => $modelValue])->one();
                $modelHasRelatedProperties = $modelForm->createModelFormSend();

                if (method_exists($modelHasRelatedProperties, "createPropertiesValidateModel")) {
                    $model = $modelHasRelatedProperties->createPropertiesValidateModel();
                } else {
                    $model = $modelHasRelatedProperties->getRelatedPropertiesModel();
                }

                return $rr->ajaxValidateForm($model);
            }
        }
    }
569780
private function validateArcAttributes( $linkbase, $from, $to, $attributes )
	{
		$result['priority'] = property_exists( $attributes, 'priority' ) ? (string) $attributes->priority	: "0";
		$result['use']		= property_exists( $attributes, 'use' ) 	 ? (string) $attributes->use		: XBRL_Constants::$xlinkUseOptional;
		$result['order']	= property_exists( $attributes, 'order' ) 	 ? (string) $attributes->order		: "1";
		$result['title']	= property_exists( $attributes, 'title' ) 	 ? (string) $attributes->title		: "";

		if ( filter_var( $result['priority'], FILTER_VALIDATE_INT ) === false )
		{
			$this->log()->taxonomy_validation( "3.5.3.9.5", "The arc priority value MUST be an integer",
				array(
					'linkbase' => $linkbase,
					'from' => $fromLabel,
					'to' => $toLabel,
					'priority' => $result['priority'],
				)
			);
		}

		if ( ! in_array( $result['use'], XBRL_Constants::$xlinkUseValues ) )
		{
			$this->log()->taxonomy_validation( "3.5.3.9.5", "The arc priority value MUST be an integer",
				array(
					'linkbase' => $linkbase,
					'from' => $fromLabel,
					'to' => $toLabel,
					'use' => $result['use'],
				)
			);
		}

		if ( ! is_numeric( $result['order'] ) )
		{
			$this->log()->taxonomy_validation( "3.5.3.9.5", "The arc order value MUST be numeric",
				array(
					'linkbase' => $linkbase,
					'from' => $fromLabel,
					'to' => $toLabel,
					'order' => $result['order'],
				)
			);
		}

		return $result;
	}
1212535
public function send(Submission $submission, bool $runValidation = true): bool
    {
        // Get the plugin settings and make sure they validate before doing anything
        $settings = Plugin::getInstance()->getSettings();
        if (!$settings->validate()) {
            throw new InvalidConfigException('The Contact Form settings don’t validate.');
        }

        if ($runValidation && !$submission->validate()) {
            Craft::info('Contact form submission not saved due to validation error.', __METHOD__);
            return false;
        }

        $mailer = Craft::$app->getMailer();

        // Prep the message
        $fromEmail = $this->getFromEmail($mailer->from);
        $fromName = $this->compileFromName($submission->fromName);
        $subject = $this->compileSubject($submission->subject);
        $textBody = $this->compileTextBody($submission);
        $htmlBody = $this->compileHtmlBody($textBody);

        $message = (new Message())
            ->setFrom([$fromEmail => $fromName])
            ->setReplyTo([$submission->fromEmail => $submission->fromName])
            ->setSubject($subject)
            ->setTextBody($textBody)
            ->setHtmlBody($htmlBody);

        if ($submission->attachment !== null) {
            foreach ($submission->attachment as $attachment) {
                if (!$attachment) {
                    continue;
                }
                $message->attach($attachment->tempName, [
                    'fileName' => $attachment->name,
                    'contentType' => FileHelper::getMimeType($attachment->tempName),
                ]);
            }
        }

        // Grab any "to" emails set in the plugin settings.
        $toEmails = is_string($settings->toEmail) ? StringHelper::split($settings->toEmail) : $settings->toEmail;

        // Fire a 'beforeSend' event
        $event = new SendEvent([
            'submission' => $submission,
            'message' => $message,
            'toEmails' => $toEmails,
        ]);
        $this->trigger(self::EVENT_BEFORE_SEND, $event);

        if ($event->isSpam) {
            Craft::info('Contact form submission suspected to be spam.', __METHOD__);
            return true;
        }

        foreach ($event->toEmails as $toEmail) {
            $message->setTo($toEmail);
            $mailer->send($message);
        }

        // Fire an 'afterSend' event
        if ($this->hasEventHandlers(self::EVENT_AFTER_SEND)) {
            $this->trigger(self::EVENT_AFTER_SEND, new SendEvent([
                'submission' => $submission,
                'message' => $message,
                'toEmails' => $event->toEmails,
            ]));
        }

        return true;
    }
859505
public function validate($inputs)
    {
        if (empty($this->rules)) {
            throw new \RuntimeException('You must set rules for validator.');
            return false;
        }

        if (empty($inputs)) {
            throw new \RuntimeException('Empty array passed to validate method');
        }

        $this->validator($inputs)->addRule();

        if (!$this->run()) {
            $this->setErrors($this->validation->getErrors());

            return false;
        }

        return true;
    }
720308
public function setValidationProperty(array $properties)
    {
        if (Helpers::isKeyExists($properties, 'validation')) {
            $this->validationRules = is_array($properties['validation']) ? $properties['validation'] : Helpers::removeEmptyItems(explode('|', $properties['validation']));
        }

        if (Helpers::isNewerThanOrEqualTo('5.2') && $this->isNullable() && !in_array('nullable', $this->validationRules)) {
            $this->validationRules[] = 'nullable';
        }

        if ($this->isBoolean() && !in_array('boolean', $this->validationRules)) {
            $this->validationRules[] = 'boolean';
        }

        if ($this->isFile() && !in_array('file', $this->validationRules)) {
            $this->validationRules[] = 'file';
        }
        if ($this->isMultipleAnswers() && !in_array('array', $this->validationRules)) {
            $this->validationRules[] = 'array';
        }

        if (in_array($this->getEloquentDataMethod(), ['char', 'string']) && in_array($this->htmlType, ['text', 'textarea'])) {
            if (!in_array('string', $this->validationRules)) {
                $this->validationRules[] = 'string';
            }

            if (!Helpers::inArraySearch($this->validationRules, 'min')) {
                $this->validationRules[] = sprintf('min:%s', $this->getMinLength());
            }

            if (!Helpers::inArraySearch($this->validationRules, 'max') && !is_null($this->getMaxLength())) {
                $this->validationRules[] = sprintf('max:%s', $this->getMaxLength());
            }
        }

        $params = [];

        if (Helpers::isKeyExists($properties, 'data-type-params')) {
            $params = $this->getDataTypeParams((array) $properties['data-type-params']);
        }

        if ($this->htmlType == 'number' || (in_array($this->getEloquentDataMethod(), ['decimal', 'double', 'float'])
            && isset($params[0]) && ($length = intval($params[0])) > 0
            && isset($params[1]) && ($decimal = intval($params[1])) > 0)) {
            if (!in_array('numeric', $this->validationRules)) {
                $this->validationRules[] = 'numeric';
            }

            if (!Helpers::inArraySearch($this->validationRules, 'min') && !is_null($minValue = $this->getMinValue())) {
                $this->validationRules[] = sprintf('min:%s', $minValue);
            }

            if (!Helpers::inArraySearch($this->validationRules, 'max') && !is_null($maxValue = $this->getMaxValue())) {
                $this->validationRules[] = sprintf('max:%s', $maxValue);
            }
        }

        return $this;
    }
677450
private function validate()
    {
        if (!class_exists($this->class)) {
            throw new \RuntimeException(sprintf('Class %s does not exist, is this the correct namespace?', $this->class));
        }
        if (!in_array($this->method, get_class_methods($this->class))) {
            throw new \RuntimeException(sprintf('The method "%s" does not exist in "%s" class.', $this->method, $this->class));
        }
    }
1167870
public function save($runValidation = true, $attributes = null)
    {
        if (parent::save($runValidation, $attributes)) {
            return true;
        } else {
            $event = new ModelEvent();
            $this->trigger(static::EVENT_AFTER_SAVE_FAIL, $event);

            return false;
        }
    }
1045951
public function insertAfter($node, $runValidation = true, $attributes = null) {
        $this->operation = self::OPERATION_INSERT_AFTER;
        $this->node = $node;

        return $this->owner->save($runValidation, $attributes);
    }
666635
public function update(array $attributes)
    {
        return $this->validate($attributes) ? (boolean) Language::where('id', $attributes['id'])->update($attributes) : false;
    }
999328
public function validate(array $attributes): bool
    {
        $rules = $this->getScenarios();

        foreach ($rules as $rule) {
            $names = array_map('trim', explode(',', $rule[0]));
            array_walk($names, function ($name) use ($attributes, $rule) {
                $this->validateValue($attributes, $name, $rule);
            });
            unset($names);
        }

        unset($rules);

        return count($this->errors) === 0;
    }
1151364
function(attrs, options) {
      if (!options.validate || !this.validate) return true;
      attrs = _.extend({}, this.attributes, attrs);
      var error = this.validationError = this.validate(attrs, options) || null;
      if (!error) return true;
      this.trigger('invalid', this, error, _.extend(options, {validationError: error}));
      return false;
    }
801335
public function runValidate(array $rules = null)
    {
        // skip validation on empty rules
        if ($rules === null) {
            return true;
        }

        $success = true;
        // list each rule as single one
        foreach ($rules as $rule) {
            // 0 = field (property) name, 1 = filter name, 2 = filter value
            if ($rule[0] === null || $rule[1] === null) {
                continue;
            }

            $propertyName = $rule[0];
            $validationRule = $rule[1];
            $validationValue = null;
            if (isset($rule[2])) {
                $validationValue = $rule[2];
            }

            // check if target field defined as array and make recursive validation
            if (Any::isArray($propertyName)) {
                $cumulateValidation = true;
                foreach ($propertyName as $attrNme) {
                    // end false condition
                    if (!$this->validateRecursive($attrNme, $validationRule, $validationValue)) {
                        $cumulateValidation = false;
                    }
                }
                // assign total
                $validate = $cumulateValidation;
            } else {
                $validate = $this->validateRecursive($propertyName, $validationRule, $validationValue);
            }

            // do not change condition on "true" check's (end-false-condition)
            if ($validate === false) {
                $success = false;
            }
        }

        return $success;
    }
802805
public function validateVatNumber($attribute, $value, $parameters, $validator)
    {
        $validator->setCustomMessages([
            'vat_number' => $validator->getTranslator()->get('vatnumber-validator::validation.vat_number'),
        ]);

        try {
            return VatCalculator::isValidVATNumber($value);
        } catch (VATCheckUnavailableException $e) {
            return false;
        }
    }
1153633
protected function validate(array $attributes, string $name, array $parameters): bool
    {
        if (! is_null($attributes[$name])) {
            return true;
        }

        $this->addError($name, $parameters, '%s 不能为 Null');

        return false;
    }
623982
protected function saveStateAttribute($runValidation)
	{
		/* @var $owner \yii\db\ActiveRecord */
		$owner = $this->owner;

		//catch unchanged state attribute
		if (!$this->hasChangedStateAttribute()) return false;

		//fetch relevant configs
		$curCfg = $this->getStateConfig($owner->getOldAttribute($this->stateAttribute));
		$nextCfg = $this->getStateConfig();

		//call leave state callback on old state
		if (isset($curCfg['leaveStateCallback'])) {
			call_user_func($curCfg['leaveStateCallback'], $owner, $curCfg);
		}

		//save it
		if ($owner->save($runValidation, [$this->stateAttribute])) {
			//call enter state callback on new state
			if (isset($nextCfg['enterStateCallback'])) {
				call_user_func($nextCfg['enterStateCallback'], $owner, $nextCfg);
			}
			return true;
		} else {
			return false;
		}
	}
396353
public function validate($value, $validator, $attributes)
    {
        $validator = $this->getValidator($validator);

        $result = $validator->validate($value, $attributes);

        return $negative ? ! $result : $result;
    }