Skip to content

Commit

Permalink
Merge pull request #6 from wkf928592/master
Browse files Browse the repository at this point in the history
add client validation
  • Loading branch information
Borales committed Mar 16, 2016
2 parents 51eec1f + 5d016dd commit b587882
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/PhoneInputValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use yii\validators\Validator;
use yii\helpers\Html;
use yii\helpers\Json;

/**
* Validates the given attribute value with the PhoneNumberUtil library.
Expand Down Expand Up @@ -37,4 +39,27 @@ protected function validateValue($value)
}
return $valid ? null : [$this->message, []];
}
}

/**
* @inheritdoc
*/
public function clientValidateAttribute($model, $attribute, $view) {

$telInputId = Html::getInputId($model, $attribute);
$options = Json::htmlEncode([
'message' => Yii::$app->getI18n()->format($this->message, [
'attribute' => $model->getAttributeLabel($attribute)
], Yii::$app->language)
]);

return <<<JS
var options = $options,
telInput = $("#$telInputId");
if($.trim(telInput.val())){
if(!telInput.intlTelInput("isValidNumber")){
messages.push(options.message);
}
}
JS;
}
}

0 comments on commit b587882

Please sign in to comment.