我有两张桌子
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;
}
}
此外,我还可以在我的IpController中返回$ range ['firstIP'],$ range ['lastIP']:
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用户首先选择范围(Label)然后选择ip-address,我们应该检查这个ip是否存在于所选范围内! 问题是如何检查ip-adress是否在所选范围(标签)中并阻止用户创建在所选范围内不存在的IP?