/**
  * Formats response data in JSONP format.
  * @param Response $response
  */
 protected function formatJsonp($response)
 {
     $response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8');
     if (is_array($response->data) && isset($response->data['data'], $response->data['callback'])) {
         $response->content = sprintf('%s(%s);', $response->data['callback'], Json::htmlEncode($response->data['data']));
     } elseif ($response->data !== null) {
         $response->content = '';
         Leaps::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__);
     }
 }
 /**
  * @inheritdoc
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     $pattern = Html::escapeJsRegularExpression($this->pattern);
     $options = ['pattern' => new JsExpression($pattern), 'not' => $this->not, 'message' => Leaps::$app->getI18n()->format($this->message, ['attribute' => $model->getAttributeLabel($attribute)], Leaps::$app->language)];
     if ($this->skipOnEmpty) {
         $options['skipOnEmpty'] = 1;
     }
     ValidationAsset::register($view);
     return 'leaps.validation.regularExpression(value, messages, ' . Json::htmlEncode($options) . ');';
 }
Ejemplo n.º 3
0
 /**
  * Parses a HTTP request body.
  * @param string $rawBody the raw HTTP request body.
  * @param string $contentType the content type specified for the request body.
  * @return array parameters parsed from the request body
  * @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`.
  */
 public function parse($rawBody, $contentType)
 {
     try {
         return Json::decode($rawBody, $this->asArray);
     } catch (InvalidParamException $e) {
         if ($this->throwException) {
             throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage(), 0, $e);
         }
         return null;
     }
 }
Ejemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     if (strpos($this->pattern, '{schemes}') !== false) {
         $pattern = str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern);
     } else {
         $pattern = $this->pattern;
     }
     $options = ['pattern' => new JsExpression($pattern), 'message' => Leaps::$app->getI18n()->format($this->message, ['attribute' => $model->getAttributeLabel($attribute)], Leaps::$app->language), 'enableIDN' => (bool) $this->enableIDN];
     if ($this->skipOnEmpty) {
         $options['skipOnEmpty'] = 1;
     }
     if ($this->defaultScheme !== null) {
         $options['defaultScheme'] = $this->defaultScheme;
     }
     ValidationAsset::register($view);
     if ($this->enableIDN) {
         PunycodeAsset::register($view);
     }
     return 'leaps.validation.url(value, messages, ' . Json::htmlEncode($options) . ');';
 }
Ejemplo n.º 5
0
 /**
  * Registers the needed client script and options.
  */
 public function registerClientScript()
 {
     $js = '';
     $view = $this->getView();
     $this->initClientOptions();
     if (!empty($this->mask)) {
         $this->clientOptions['mask'] = $this->mask;
     }
     $this->hashPluginOptions($view);
     if (is_array($this->definitions) && !empty($this->definitions)) {
         $js .= '$.extend($.' . self::PLUGIN_NAME . '.defaults.definitions, ' . Json::htmlEncode($this->definitions) . ");\n";
     }
     if (is_array($this->aliases) && !empty($this->aliases)) {
         $js .= '$.extend($.' . self::PLUGIN_NAME . '.defaults.aliases, ' . Json::htmlEncode($this->aliases) . ");\n";
     }
     $id = $this->options['id'];
     $js .= '$("#' . $id . '").' . self::PLUGIN_NAME . "(" . $this->_hashVar . ");\n";
     MaskedInputAsset::register($view);
     $view->registerJs($js);
 }
Ejemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     $label = $model->getAttributeLabel($attribute);
     $options = ['pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern), 'message' => Leaps::$app->getI18n()->format($this->message, ['attribute' => $label], Leaps::$app->language)];
     if ($this->min !== null) {
         $options['min'] = is_string($this->min) ? (double) $this->min : $this->min;
         $options['tooSmall'] = Leaps::$app->getI18n()->format($this->tooSmall, ['attribute' => $label, 'min' => $this->min], Leaps::$app->language);
     }
     if ($this->max !== null) {
         $options['max'] = is_string($this->max) ? (double) $this->max : $this->max;
         $options['tooBig'] = Leaps::$app->getI18n()->format($this->tooBig, ['attribute' => $label, 'max' => $this->max], Leaps::$app->language);
     }
     if ($this->skipOnEmpty) {
         $options['skipOnEmpty'] = 1;
     }
     ValidationAsset::register($view);
     return 'leaps.validation.number(value, messages, ' . Json::htmlEncode($options) . ');';
 }
Ejemplo n.º 7
0
 /**
  * @inheritdoc
  */
 public function clientValidateAttribute($model, $attribute, $view)
 {
     $options = ['pattern' => new JsExpression($this->pattern), 'fullPattern' => new JsExpression($this->fullPattern), 'allowName' => $this->allowName, 'message' => Leaps::$app->getI18n()->format($this->message, ['attribute' => $model->getAttributeLabel($attribute)], Leaps::$app->language), 'enableIDN' => (bool) $this->enableIDN];
     if ($this->skipOnEmpty) {
         $options['skipOnEmpty'] = 1;
     }
     ValidationAsset::register($view);
     if ($this->enableIDN) {
         PunycodeAsset::register($view);
     }
     return 'leaps.validation.email(value, messages, ' . Json::htmlEncode($options) . ');';
 }
Ejemplo n.º 8
0
 /**
  * Registers the needed JavaScript.
  */
 public function registerClientScript()
 {
     $id = $this->options['id'];
     $this->clientOptions['push'] = $this->enablePushState;
     $this->clientOptions['replace'] = $this->enableReplaceState;
     $this->clientOptions['timeout'] = $this->timeout;
     $this->clientOptions['scrollTo'] = $this->scrollTo;
     $options = Json::htmlEncode($this->clientOptions);
     $js = '';
     if ($this->linkSelector !== false) {
         $linkSelector = Json::htmlEncode($this->linkSelector !== null ? $this->linkSelector : '#' . $id . ' a');
         $js .= "jQuery(document).pjax({$linkSelector}, \"#{$id}\", {$options});";
     }
     if ($this->formSelector !== false) {
         $formSelector = Json::htmlEncode($this->formSelector !== null ? $this->formSelector : '#' . $id . ' form[data-pjax]');
         $js .= "\njQuery(document).on('submit', {$formSelector}, function (event) {jQuery.pjax.submit(event, '#{$id}', {$options});});";
     }
     $view = $this->getView();
     PjaxAsset::register($view);
     if ($js !== '') {
         $view->registerJs($js);
     }
 }
Ejemplo n.º 9
0
 /**
  * Runs the widget.
  */
 public function run()
 {
     $id = $this->options['id'];
     $options = Json::htmlEncode($this->getClientOptions());
     $view = $this->getView();
     GridViewAsset::register($view);
     $view->registerJs("jQuery('#{$id}').leapsGridView({$options});");
     parent::run();
 }
Ejemplo n.º 10
0
 /**
  * Registers the needed JavaScript.
  */
 public function registerClientScript()
 {
     $options = $this->getClientOptions();
     $options = empty($options) ? '' : Json::htmlEncode($options);
     $id = $this->imageOptions['id'];
     $view = $this->getView();
     CaptchaAsset::register($view);
     $view->registerJs("jQuery('#{$id}').leapsCaptcha({$options});");
 }
Ejemplo n.º 11
0
 /**
  * Runs the widget.
  * This registers the necessary javascript code and renders the form close tag.
  * @throws InvalidCallException if `beginField()` and `endField()` calls are not matching
  */
 public function run()
 {
     if (!empty($this->_fields)) {
         throw new InvalidCallException('Each beginField() should have a matching endField() call.');
     }
     if ($this->enableClientScript) {
         $id = $this->options['id'];
         $options = Json::htmlEncode($this->getClientOptions());
         $attributes = Json::htmlEncode($this->attributes);
         $view = $this->getView();
         ActiveFormAsset::register($view);
         $view->registerJs("jQuery('#{$id}').leapsActiveForm({$attributes}, {$options});");
     }
     echo Html::endForm();
 }