What is the model number of the washer?
Usually there is a wire mesh filter in the water inlet hose connection where it screws into the machine or in some machines the filter is in the machine at the same connection point.
Check if there is a filter there or not and if it is there that it is not damaged. Even city water sometimes has grit in it (old pipes?) which could possibly lodge in the water inlet solenoid valve in the machine, preventing a good seal when it is closed.
If everything checks out then try filling the machine once or twice with cold water and see if this flushes any debris out of the valve and that it closes properly after that.
If it doesn’t then get the machine repaired under warranty as it might be a faulty cold water inlet solenoid valve.
In the meantime to prevent it from filling when it is switched off, simply turn off the cold water tap that supplies the machine and only turn it on for as long as needed when washing and then turn it off again, until the problem is fixed.