/** * @return bool * Динамическая валидация * @TODO Углубленная валидация списков (с проверкой наличия базы), изображений */ public function beforeValidate() { /** @var \common\models\Option $option */ $option = Option::findOne($this->option_id); if ($option) { switch ($option->type) { case 1: //число целое //число целое case 4: //выключатель //выключатель case 8: //список дочерних документов //список дочерних документов case 9: //список потомков документа //список потомков документа case 10: //список пользователей $this->validators[] = Validator::createValidator('integer', $this, 'value'); break; case 2: //число $this->validators[] = Validator::createValidator('double', $this, 'value'); break; case 3: //строка //строка case 5: //текст //текст case 6: //файл (выбор) $this->validators[] = Validator::createValidator('string', $this, 'value'); break; case 7: //изображение (загрузка) $this->validators[] = Validator::createValidator('image', $this, 'file', ['minHeight' => 100, 'skipOnEmpty' => true]); break; case 11: //регулярное выражение $pattern = $option->param ? $option->param : '\\w'; $this->validators[] = Validator::createValidator('match', $this, 'value', ['pattern' => $pattern]); break; } if ($option->require) { if ($option->type == 7) { if (!$this->value) { $this->validators[] = Validator::createValidator('required', $this, 'file'); } } else { $this->validators[] = Validator::createValidator('required', $this, 'value'); } } } return true; }
/** * Returns the maximum number of allowed requests and the window size. * @param \yii\web\Request $request the current request * @param \yii\base\Action $action the action to be executed * @return array an array of two elements. The first element is the maximum number of allowed requests, * and the second element is the size of the window in seconds. */ public function getRateLimit($request, $action) { $allow = 10; $window = 600; $route = $action->controller->route; $rate_limit_name = Module::getApiName($route); $rate_limit_option = Option::findOne(['name' => $rate_limit_name]); if ($rate_limit_option) { try { $option = \yii\helpers\Json::decode($rate_limit_option->value); if (!array_key_exists('allow', $option) || !is_numeric($option['allow']) || !is_int($option['allow']) || $option['allow'] < 0 || $option['allow'] > Option::RATE_LIMIT_ALLOW_MAX) { throw new \yii\base\InvalidParamException('Invalid allow value.'); } if (!array_key_exists('window', $option) || !is_numeric($option['window']) || !is_int($option['window']) || $option['window'] < 0 || $option['allow'] > Option::RATE_LIMIT_WINDOW_MAX) { throw new \yii\base\InvalidParamException('Invalid window value.'); } } catch (\Exception $ex) { $option['allow'] = $allow; $option['window'] = $window; } return [$option['allow'], $option['window']]; } return [$allow, $window]; }
/** * Finds the Option model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return Option the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Option::findOne($id)) !== null) { return $model; } throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.')); }
/** * Finds the Option model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * * @param integer $id * * @return Option the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Option::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Отображение дополнительного поля * Используется при добавлении значения * мультиполя * @return mixed */ public function actionAjaxoption() { $id = Yii::$app->request->post('id'); $new_field = Yii::$app->request->post('newfield'); $option = Option::findOne($id); return $this->renderAjax('_field', ['option' => $option, 'option_id' => $id, 'field_id' => 'multi_new_' . $new_field]); }