Skip to content
Snippets Groups Projects
Commit 9e500391 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Fix exception when no network selected

1. It's possible to erase the network when creating a host.
The exception raised was due to the ip_in_range validator
where a select using the network_id is performed.
This is now catched to raise a ValidationError.
2. Add some javascript to remove the is-invalid class on the network and
IP fields when changing them

JIRA INFRA-810 #action In Progress
parent 49e0b4f2
No related branches found
No related tags found
No related merge requests found
......@@ -56,6 +56,10 @@ def starts_with_hostname(form, field):
def ip_in_network(form, field):
"""Check that the IP is in the network"""
network_id_field = form["network_id"]
if not network_id_field.data:
raise validators.ValidationError(
"Can't validate the IP. No network was selected."
)
network = models.Network.query.get(network_id_field.data)
ip = ipaddress.ip_address(field.data)
if ip not in network.network_ip:
......
......@@ -36,6 +36,7 @@ $(document).ready(function() {
// and update the IP field
var network_id = $("#network_id").val();
if ( network_id !== "" ) {
$("#ip").removeClass("is-invalid");
$.getJSON(
$SCRIPT_ROOT + "/network/_retrieve_first_available_ip/" + network_id,
function(json) {
......@@ -83,6 +84,13 @@ $(document).ready(function() {
set_default_ip();
}
// On page load if the network_id is invalid, it's because an empty entry was submitted
// remove the default value to make it clear
if( ($("#hostForm").length || $("#interfaceForm").length) && $("#network_id").hasClass("is-invalid") ) {
var network_id_selectize = $("#network_id")[0].selectize;
network_id_selectize.clear(true);
}
// Clear the default value so that the user can start typing directly
// or click a dropdown item
$("#network_id").next(".selectize-control").on( 'click', function () {
......@@ -92,6 +100,8 @@ $(document).ready(function() {
// Set the default IP when changing network
$("#network_id").on('change', function() {
$(this).removeClass("is-invalid");
$(this).next(".selectize-control").removeClass("is-invalid");
set_default_ip();
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment