The good news is this advice is completely incorrect. According to the government website advice: “a reference must be fair and accurate” and can be challenged.
I was always told it's perfectly legal, but writing anything negative opens the path for future legal headaches. It's less hassle to only confirm the dates worked.
Source: https://www.gov.uk/work-reference