1
我有两个表yii2:如何拥有IP检查范围验证在创建和更新表单
labels (id , name, mask, subnetID, ...)
ip (id , ip, labelID[foreign key], ...)
在我labelsController
我:
public function actionCheckrange($id,$ip){
$ip = $_REQUEST['ip'];
$iptolong = ip2long($ip);
$label = $this->findModel($id);
$netID = $label->netID;
$mask = $label->mask;
$range = $this->findIpRange($netID, $mask);
if($iptolong <= $range['lastIP'] && $iptolong >= $range['firstIP']){
return true;
}
else{
echo "<div class='alert alert-danger alert-dismissable'>".
"<a href='#' class='close' data-dismiss='alert' aria-label='close'>×</a>".
"IP is not in range".
"</div>";
return false;
}
}
我也可以回到$范围['firstIP '],$范围[' lastIP'],以及在我IpController:
public function actionCreate()
{
$model = new Ip();
if ($model->load(Yii::$app->request->post())) {
$label = $model->label;
if ($model->save())
{
Yii::$app->session->setFlash('success', 'OK');
return $this->redirect(Yii::$app->request->referrer);
} else {
Yii::$app->session->setFlash('error', 'Error');
return $this->redirect(Yii::$app->request->referrer);
}
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
在我的IP形式
:
<?= $form->field($model, 'ip')->widget(Select2::classname(), [
'language' => 'fa',
'options' =>
[
'placeholder' => 'انتخاب IP' ,
'dir' => 'ltr',
'onchange' => '$.post("/labels/checkrange/"+$("#w0").val()+"?ip="+$(this).val(),function(data) {
$("#range-warninig").html(data);
});'
],
'pluginOptions' => [
'allowClear' => true,
'dropdownAutoWidth'=>true,
'tags' => true,
],
]); ?>
在“IP”_form用户首先选择范围(标签)然后ip-address,我们应该检查这个ip是否存在于选定的范围! 问题是如何检查IP地址是否在选定的范围(标签),并阻止用户创建不存在于选定范围内的IP?
我以前读过这个!其实我想检查基于选定的IP范围(“标签”)的范围。我们不能使用模型验证器,如['ip_address','ip','ranges'=> ['192.168.0.0/24']] !!!!!!!!!我应该通过POST请求cidr验证ip! –
所以,你的问题是如何决定IP地址是否在特定范围内? –
在“IP”_form用户首先选择范围,然后选择ip地址,我们应该检查这个ip是否存在于所选范围内! –