Update VxLan.php - Add input validations to model (#6899)

* Update VxLan.php - Add multiple input validations to model

* Update VxLan.php - add isFieldChanged and gettext function
This commit is contained in:
Monviech 2023-10-02 13:14:20 +02:00 committed by GitHub
parent 21bd623835
commit ef9c2b4df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
<?php
/*
* Copyright (C) 2019 Deciso B.V.
* Copyright (C) 2023 Deciso B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -28,8 +28,63 @@
namespace OPNsense\Interfaces;
use Phalcon\Messages\Message;
use OPNsense\Base\BaseModel;
class VxLan extends BaseModel
{
public function performValidation($validateFullModel = false)
{
$messages = parent::performValidation($validateFullModel);
// Initialize variables
foreach ($this->vxlan->iterateItems() as $vxlan) {
$vxlangroup = (string) $vxlan->vxlangroup;
$vxlanremote = (string) $vxlan->vxlanremote;
$vxlandev = (string) $vxlan->vxlandev;
// Validate that values in Fields have been changed, prevents configuration save lockout when invalid data is present.
if ($validateFullModel ||
$vxlan->vxlangroup->isFieldChanged() ||
$vxlan->vxlanremote->isFieldChanged() ||
$vxlan->vxlandev->isFieldChanged()
) {
// Validation 1: At least one of vxlangroup and vxlanremote must be populated, but not both.
if ((!empty($vxlangroup) && !empty($vxlanremote)) ||
(empty($vxlangroup) && empty($vxlanremote))
) {
$messages->appendMessage(new Message(
gettext("Remote address -or- Multicast group has to be specified"),
"vxlan.vxlanremote",
"GroupOrRemote"
));
$messages->appendMessage(new Message(
gettext("Multicast group -or- Remote address has to be specified"),
"vxlan.vxlangroup",
"GroupOrRemote"
));
}
// Validation 2: If vxlanremote is populated, vxlandev must be an empty string.
if (!empty($vxlanremote) && !empty($vxlandev)) {
$messages->appendMessage(new Message(
gettext("Remote address is specified, Device must be None"),
"vxlan.vxlandev",
"DeviceRequirementForRemote"
));
}
// Validation 3: If vxlangroup is populated, vxlandev must not be an empty string.
if (!empty($vxlangroup) && empty($vxlandev)) {
$messages->appendMessage(new Message(
gettext("Multicast group is specified, a Device must also be specified"),
"vxlan.vxlandev",
"DeviceRequirementForGroup"
));
}
}
}
return $messages;
}
}