RICHMOND (CN) – The Fourth Circuit Thursday has sided with a Virginia teen who claimed police officers violated his right to privacy by forcing him to masturbate as they gathered evidence against him for “sexting” his underage girlfriend.

Trey Sims, of Prince William County, Virginia, was 17 when he was arrested in 2014 for sending sexually explicit photographs and videos of himself to his 15-year-old girlfriend.

During the investigation of the case, a detective obtained a warrant to detain the boy, then demanded he masturbate while being filmed.

As described in the ruling written by U.S. Circuit Judge Barbara Milano Keenan, the detective hoped to get photos of Sims’ erect penis to compare with evidence gathered from the girlfriend’s cellphone.

After Sims failed to achieve an erection, the detective threatened to take the boy to a local hospital where he would be injected with “erection-producing drugs” if he did not comply, the ruling says.

When Sims continued to be unable to achieve an erection, the detective took a photo of the boy’s flaccid penis. According to published reports, the detective later secured a second warrant to photograph the boy, but it was never executed.

A criminal court found there was enough evidence to convict Sims, but did “not make a finding of guilt” and placed him on probation before later dismissing the charges entirely.

But Sims filed a federal lawsuit against the detective, arguing the demand that he masturbate for investigators violated his 4th Amendment rights.

A lower court dismissed the case on the grounds the detective had qualified immunity, but the Fourth Circuit found otherwise on Thursday.

“A reasonable police officer would have known that attempting to obtain a photograph of a minor child’s erect penis, by ordering the child to masturbate in the presence of others, would unlawfully invade the child’s right of privacy,” Judge Keenen wrote in the 29-page opinion.

She went on to say the detective’s conduct “affronted the basic protections of the Fourth Amendment … which at its core protects personal privacy and dignity against unjustified intrusion by governmental actors.”

Keenen also rebuked the detective for asking the teen to masturbate in the locker room of a juvenile detention center, surrounded by three armed police officers.

“This sexually intrusive search was rendered more egregious by being conducted in a manner that would instill fear in Sims,” she wrote, remanding the case for further proceedings.

But in a dissent, U.S. Circuit Judge Robert Bruce King said his colleagues in the majority got the decision wrong and that he would have granted the detective’s qualified immunity claim.

King argued the majority failed to recognize that the detective was acting on the advice of counsel and at all times adhered to the court order.

The majority’s ruling — that any reasonable law enforcement officer would have recognized that the search warrant violated a clearly established constitutional right — is not supported by any precedent, much less the compelling precedent that would ‘have placed the statutory or constitutional question beyond debate,'” King wrote.

“In my view, [the detective]’s actions were entirely consistent with applicable law and the Fourth Amendment,” he said.