A Boston-area Roman Catholic girls' high school violated a gay man's rights when it revoked an offer for him to work as director of food services after he revealed on a form that he was married to another man, a state court has ruled.

Matthew Barrett last year sued Fontbonne Academy in Milton, Massachusetts, south of Boston, saying it had discriminated against him when it revoked a job offer made in 2013 after learning of his sexual orientation.

The school argued that as a Catholic institution, it had an obligation for all staff to model the values of the church, which teaches that homosexual activity is immoral.

Massachusetts Superior Court Justice Douglas Wilkins late Wednesday rejected that argument, noting that Barrett had been offered a job that did not involve teaching ethics but simply preparing food.

RELATED: Closing arguments set in Massachusetts teen's murder trial

"He was not denied employment for any advocacy of same-sex marriage or gay rights; he only listed his husband as an emergency contact on a 'new hire' form," Wilkins wrote.

He noted that Fontbonne also employs people who are not Catholic as well as those who are married outside the church.

"I'm ecstatic," Barrett said following the ruling. "What happened to me was wrong, and I truly hope it doesn't happen to anyone else."

Fontbonne said in a statement: "We have received the court's decision in this matter and are considering our options."

Massachusetts in 2004 became the first U.S. state to legalize gay marriage. A June ruling by the U.S. Supreme Court made it legal nationwide.