How long must you have left on your passport to travel to Spain?

It depends on your nationality.

Citizens of other EU and Schengen countries do not need any time left on their passports.

Citizens of other countries normally need a passport which is valid for 3 months after the date they intend to leave the Schengen area.