Constrained least-squares methods for partial differential equations