It's legal to put clauses in contracts, but employment tribunals almost invariably strike them down. Unless something clearly nefarious is going on, they are utterly unenforceable.
IANAL but this looks to me (sorry, in Spanish) like an example of one that was enforced without anything particularly nefarious, and included the employee having to return the compensation and pay an additional amount for damages on top of that. https://cdn.cuestioneslaborales.es/wp-content/uploads/2014/1...