diff --git a/app/network/forms.py b/app/network/forms.py index 256875aeb0eddb47da45827d5c27d62d89605223..ad7b7a1e7c0bbe6574b132acba1eb37561d7897e 100644 --- a/app/network/forms.py +++ b/app/network/forms.py @@ -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: diff --git a/app/static/js/hosts.js b/app/static/js/hosts.js index 75ac21571872027148100dc9d8c003b7666ec8fd..78bac16547a39db47a81899eec87bca19ad815c3 100644 --- a/app/static/js/hosts.js +++ b/app/static/js/hosts.js @@ -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(); });