I reference 3 texts that talk about the damaging effects of legalism and the solution to it. One article is by Jeremy Myers, another is by Greg Albrecht and there is a reference to a book by Gregory A. Boyd.
Grace: the Antidote to Legalism
"Legalism is when people use the law of God in an attempt to gain favor with God."
Saying that we should not obey God's Law in order to try to gain favor with God is not at all the same as saying that that we should not obey God's Law. If God were legalistic for giving His Law and Jesus were legalistic for living in complete obedience to it and for teaching his followers to obey it by word and by example, then being legalistic would be being in good company, but that's not what legalism is. Legalism does not refer to whether someone thinks that one or many laws should be obeyed, but rather it is in regard to the manner in which someone obeyed the Law according to the letter exactly how it is written without regard for the spirit or intent behind the law. In short legalism is using God's Law for a purpose other than its intended purposes, of which attempting to gain favor with God is one example.
For instance:
Leviticus 19:12 “‘Do not swear falsely by my name and so profane the name of your God. I am the Lord.
Someone who was focused on obeying the spirit of this law would understand that its intent is for us to not swear falsely, whereas someone who was focused on obeying the letter of this law exactly how it is written would understand that we can swear falsely just as long as we don't do so in God's named, which incidentally is the issue that Jesus was criticizing the Pharisees concerning in Matthew 23:16-22.
However, in Philippians 3:8, Paul had been outwardly keeping the Law flawlessly, yet had not been focusing inwardly on growing in a relationship with Christ, so he had been completely missing the whole point of the Law and counted it all as rubbish. So someone can still be legalistic while outwardly obeying the Law correctly because they are missing the inward intent of teaching us how to walk in God's ways, how to express His character traits, and how to thereby grow in a relationship with Him. So obeying the Law according to the letter leads to death just as assuredly as refusing to submit to the Law because it undermines both the intention of what God was commanding us to do and why He was commanding us to do it.