You need to use the SObject.addError
method. Note that you can only pass in records from the trigger context as a parameter value.
I would recommend modifying your method to accept the trigger records. Best practice is to put the error message in a Custom Label
so you can configure this user-facing copy quickly and easily. Something like:
public static void validateNoActiveContacts(List<Account>Map<Id, Account> records)
{
Set<Id> accountsWithActiveContactwithActiveContact = new Map<Id>([
SELECT Id FROM Account WHERE Id IN (
SELECT AccountId FROM Contact
WHERE Inactive = false
AND AccountId IN :records .keySet()
)
]).keySet();
for (AccountId recordaccountId : recordswithActiveContact)
{
if (accountsWithActiveContactrecords.containsget(record.Id)accountId)
record.addError(Label.Active_Contact_Error);
}
}