public function setConstraint($fieldName, $constraint)
    {
        $class = get_class($constraint);
        // remove existing constraint if it already exists
        if ($this->getConstraint($fieldName, $class)) {
            $this->removeConstraint($fieldName, $class);
        }

        $this->constraints[$fieldName][$class] = $constraint;

        if ($this->form) {
            $dataField = $this->form->Fields()->dataFieldByName($fieldName);
            $constraint->setField($dataField);
            if ($this->parsleyEnabled) {
                // If there is no field, output a clear error message before trying to apply parsley
                if (!$dataField) {
                    throw new Exception("You have set a constraint on '$fieldName' but it does not exist in the FieldList.");
                }
                $constraint->applyParsley();
            }
        }

        return $this;
    }