Skip to content

Commit

Permalink
Add js validations to NSync js plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tafid committed Nov 25, 2015
1 parent 42d2249 commit 8705a2e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
32 changes: 30 additions & 2 deletions src/assets/js/NSyncPluginAsset.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
/**
* Created by tofid on 23.11.15.
*/

if (!String.prototype.endsWith) {
Object.defineProperty(String.prototype, 'endsWith', {
value: function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
}
});
}
;
(function ($, window, document, undefined) {
var pluginName = "NSync";
Expand Down Expand Up @@ -31,10 +43,26 @@
init: function (event) {
this.addInlineFormListener(event);
this.addDynamicFormListener(event);
this.fieldslistener(event);
},
isStateChanged: function () {
return this.getDynamicElementsCount() !== this.getInlineElementsCount();
},
fieldslistener: function (event) {
var that = this;
var domain_name = $(this.element).find('.domain_name').eq(0).val();
$(this.element).on('change keyup input click', function (event) {
var DWContainer = $(that.element).find(that.options.dynamicFormWidgetContainer);
DWContainer.find(that.options.dynamicFormWidgetItem).each(function (i, elem) {
var containerFields = $(elem).find('input');
if (containerFields.eq(0).val().endsWith(domain_name)) {
containerFields.eq(1).attr('disabled', false);
} else {
containerFields.eq(1).attr('disabled', true);
}
});
});
},

// Inline Processing
addInlineFormListener: function (event) {
Expand All @@ -56,7 +84,7 @@
if (elem.ip !== '') {
nsInlineField += '/' + elem.ip;
}
nsInlineField += ', ';
nsInlineField += ',';
});

$(this.element).find(this.options.inlineFieldSelector).val(nsInlineField.replace(/,\s*$/g, ''));
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DomainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function actions()
],
'view' => [
'class' => 'hipanel\actions\ViewAction',
'findOptions' => ['with_dns' => 1],
'findOptions' => ['with_nsips' => 1],
'data' => function ($action) {
return [
'domainContactInfo' => Domain::perform('GetContactsInfo', ['id' => $action->getId()]),
Expand Down
2 changes: 1 addition & 1 deletion src/models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function rules()
[['foa_sent_to'], 'email'],
[['url_fwval' ,'mailval', 'parkval', 'soa', 'dns', 'counters'], 'safe'],
[['registrant', 'admin', 'tech', 'billing'], 'integer'],
[['block', 'epp_client_id', 'nameservers'], 'safe'],
[['block', 'epp_client_id', 'nameservers', 'nsips'], 'safe'],
[['note'], 'safe', 'on' => ['set-note','default']],

[['registrant','admin','tech','billing'], 'safe', 'on' => ['set-contacts']],
Expand Down
2 changes: 1 addition & 1 deletion src/models/Ns.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function rules()
[['name'], FqdnValueValidator::className()],
[['ip'], 'ip'],
[['ip'], function ($attribute, $params) {
if (StringHelper::endsWith($this->name, $this->domain_name)) {
if (!StringHelper::endsWith($this->name, $this->domain_name)) {
$this->addError($attribute, Yii::t('app', Yii::t('app', 'To assign the IP, NS must be a child from main domain')));
}
}],
Expand Down
2 changes: 1 addition & 1 deletion src/views/domain/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
?>
<?= NsWidget::widget([
'model' => $model,
'attribute' => 'nameservers',
'attribute' => 'nsips',
]); ?>

</div>
Expand Down
7 changes: 4 additions & 3 deletions src/widgets/NsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ private function createNsModels($string)
$models = [];
foreach (explode(',', $string) as $item) {
if (strpos($item, '/')) {
$data = [
'name' => '',
'ip' => '',
$ns_ip = explode('/', $item);
$data['Ns'] = [
'name' => $ns_ip[0],
'ip' => $ns_ip[1],
];
} else {
$data['Ns']['name'] = $item;
Expand Down
13 changes: 8 additions & 5 deletions src/widgets/views/ns.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use hipanel\helpers\StringHelper;
use hipanel\modules\domain\assets\NSyncPluginAsset;
use hipanel\widgets\Pjax;
use wbraganca\dynamicform\DynamicFormWidget;
Expand All @@ -8,9 +9,7 @@
use yii\helpers\Url;

Yii::$app->assetManager->forceCopy = true;

NSyncPluginAsset::register($this);

?>

<?php Pjax::begin(['id' => 'nss-pjax-container', 'enablePushState' => false, 'enableReplaceState' => true]) ?>
Expand Down Expand Up @@ -42,7 +41,7 @@

<div class="row" style="margin-top: 15pt;">
<div class="col-md-10 inline-form-selector">
<?= Html::activeTextInput($model, 'nameservers', [
<?= Html::activeTextInput($model, 'nsips', [
'class' => 'form-control',
'placeholder' => $model->getAttributeLabel('nameservers'),
'autocomplete' => 'off',
Expand Down Expand Up @@ -75,18 +74,20 @@
'formFields' => [
'name',
'ip',
'domain_name',
],
]) ?>

<div class="container-items">
<?php foreach ($nsModels as $i => $nsModel): ?>
<?= Html::activeHiddenInput($nsModel, "[$i]domain_name") ?>
<?= Html::activeHiddenInput($nsModel, "[$i]domain_name", ['value' => $model->domain, 'class' => 'domain_name']) ?>
<div class="item">
<div class="row" style="margin-bottom: 5pt">
<div class="col-md-5">
<?= $form->field($nsModel, "[$i]name")->textInput(['placeholder' => $nsModel->getAttributeLabel('name')])->label(false) ?>
</div>
<div class="col-md-5">
<?= $form->field($nsModel, "[$i]ip")->textInput(['placeholder' => $nsModel->getAttributeLabel('ip')])->label(false) ?>
<?= $form->field($nsModel, "[$i]ip")->textInput(['disabled' => !StringHelper::endsWith($nsModel->name, $model->domain), 'placeholder' => $nsModel->getAttributeLabel('ip')])->label(false) ?>
</div>
<div class="col-md-2 text-right">
<div class="btn-group" role="group">
Expand All @@ -97,7 +98,9 @@ class="glyphicon glyphicon-minus"></i></button>
</div>
</div>
</div>
<?= Html::activeHiddenInput($nsModel, "[$i]domain_name", ['value' => $model->domain]) ?>
</div>

<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
Expand Down

0 comments on commit 8705a2e

Please sign in to comment.