Hi Leyan. No, it’s not legal to tell you that it is compulsory to purchase an optional warranty.
However, you’ll have to prove that that’s what you were told. Unless you have evidence, the salesman will be able to swear on a stack of bibles that he never said such a thing and that it was actually you who specifically wanted the warranty. It’s all about what you can prove, and verbal assurances are worthless.