Where did the notion come from?
The short answer is you need to read up on Eleanor of Aquitaine, mother of Richard the Lionheart, whose court in Aquitaine is credited in popularising notions of Chivalry in the Plantagenet domains.
The long answer involves a centuries long effort by the Catholic Church to mitigate the wholesale slaughter of the day. This includes trying to ban fighting on the sabbath or holy days; not using terror weapons like crossbows on Christians (fill your boots on Muslims) and not slaughtering non-combatants, which leads on to knights actually protecting The Simple since they are needed to grow the crops and prevent famines.
You may want to argue that all these policies were part of the church’s campaign to be recognised as suzerain over Christendom, and over Christian monarchs, by interfering in what was the rightful business of Kings and spoiling their fun. Plenty of Kings made that argument.
The ideals of chivalry arose from a combination of tradition, Christian morality, and romantic literature. The powerful were not supposed to abuse the weak, although the lower classes were also expected to know their place in society as well.
While European knights are sometimes today compared with the Japanese samurai class, their respective codes of behaviour were different. European society was influenced by Christian ideals of morality, while Japanese social norms were quite a bit different.
As to whether everyone in those days behaved according to law, custom, and moral codes, you could ask the same question about people today.
Thank you, so there is no doubt there was a knightly/chivalric code there, in the sense that there were rules that they were supposed to abide by, although for the most part they seem to be rules about how they behave among themselves and other members of their class (and presumably their families and womenfolk), but little hard evidence that anyone paid much more than lip service to them.
What there isn't, and which presumably there would be a lot of if it really was the case, is any evidence that the knights as a class ever actually protected the weak and the poor (other than perhaps the serfs and peasants that belonged to his feudal lord).
What there is however, as pointed out above, is plenty of evidence that the knights were very effective stormtroopers when it came to the slaughter of anyone who their masters, either spiritual or temporal, decided needed to have manners put on them.
Surely the finest examples of knightly enterprise, and certainly the ones most extensively documented, are the crusades, and throughout the accounts of the crusades there is one litany of brutal slaughter of the weak and vulnerable after another, often the slaughter of Christians as much as infidels (the same thing went on with the Muslims too let it be said, the idea of knightly Saracens is a bit of a myth too I think).
Urban called on the Frankish knights to take up the crusade because
"this land which you inhabit, shut in on all sides by the seas and surrounded by the mountain peaks, is too narrow for your large population; nor does it abound in wealth; and it furnishes scarcely food enough for its cultivators. Hence it is that you murder one another, that you wage war, and that frequently you perish by mutual wounds. Let therefore hatred depart from among you, let your quarrels end, let wars cease"
In the First Crusade from Antioch to Jerusalem the crusaders happily slaughtered men, women and children, Christian, Muslim and Jew, (in Antioch the crusaders indulged in cannibalism, hardly a sign of knightly virtue). In Jerusalem the crusader chroniclers happily recorded how the knights waded in blood up to their ankles of civilians of all faiths.
In the Fourth Crusade it got worse, the knights allowed themselves to be used as mercenaries by the leader of Venice to destroy his commercial rivals in Constantinople, the capital of Eastern Christianity. The crusader knights obliged and besieged and sacked this heartland of the ancient Christian faith and massacred its inhabitants, robbing churches, murdering priests and raping nuns. All a hard day's work of rapine and pillage for Europe's supposedly knightly noble classes.
TLDR: In short when it comes to the actual historical record, rather than the romanticised poetry and ballads of later years, all the evidence points to Europe's knights being a bunch of murdering thugs, robbers and rapists, rather than protectors of the weak and poor that was later claimed for them.