Scenario: I created custom field type and need to add my own custom constraint/validation prior to saving.

Problem: failed to add custom constraint plugin so I implemented the function in

public function preSave() {
$return = $this->custom_check($this->value, $this->getSetting('check_options'));
  drupal_set_message($return[1]->__toString(), 'error');
  $url = \Drupal::service('path.current')->getPath();
  $response = new RedirectResponse($url);

function custom_check($value, $check_options){
//checking goes here

I user drupal_set_message() and RedirectResponse as I couldn't get the $form or $formState of the node/%nid/edit to set the error at field and dor form rebuid.

I know that solution is not the best practice and I have to create my own constraints and validation but I spent time with no luck to work and I didn't found a working example that suits my case escpicially I need to validate based on field settings so I have to pass two parameters value and setting.

Please help getting the current form array and formstate in presave to rebuild instead of redirect. or show me how to write simple constraint that takes parameters and how to add it to my custom defined field type.

Thanks in advance.

  • 1
    You should ask how to solve your actual problem instead. There is no good solution for this. Your approach is just wrong and it can't properly work.
    – Berdir
    Aug 16, 2016 at 19:57

1 Answer 1


Form validation checks the user input before it gets submitted. The submitted values don't necessarily go into the database, they can be used for other things. Presave can check values before they are stored in the database. The values don't need to come from a form, they can be generated by other code. So those two things are not connected.

If you want to validate a form, you have to use form validation. Presave doesn't know about forms. It is no problem to double check in presave, then you prevent wrong values to enter into the database from any code. But if you want to give the user a feedback in the form, use form validation. In presave you can only throw an exception after submit.

  • I need to make the form validation to be generic in any form has that field attached, I don't need to use hook alter for every bundle form!! Aug 16, 2016 at 11:49
  • You could make a custom form element.
    – 4uk4
    Aug 16, 2016 at 11:50
  • 1
    yes, you should extend that field type, and give another id / name to it, and add your own custom constraint. That's the clear way to go!
    – ssibal
    Aug 16, 2016 at 11:51
  • Yes that is right, but unfortunately I couldn't make it and didn't find working example after hours of search. Aug 16, 2016 at 11:55
  • 1
    You can add constraints to field types, and then whenever that field is validated, your constraint will be called. This is how field validation is supposed to work so that your entity or whatever will work seemlessly whether or not it is saved in a form context or a web services context.
    – mradcliffe
    Aug 16, 2016 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.