0

I'm doing some custom stuff with programmatically saving fields with EMW:

$wrapper->my_field->set('my_value'); 

Problem is, this doesn't validate before trying to save the field. How can I validate the value against the field before trying to save it?

3
  • Are you trying to validate the value against the field of the entity you saved before? Is it an edit action?
    – Andrew Nim
    Mar 17, 2017 at 8:31
  • Yes it's editing, but whether it's editing or a new entity I don't see the difference.
    – Chris
    Mar 17, 2017 at 8:53
  • Yes, i just misunderstood the question. zhilevan is right, you have to add your custom validations using for example some regular expressions etc depending on what you need for your field.
    – Andrew Nim
    Mar 17, 2017 at 8:58

1 Answer 1

0

install entity_validator module .

Unlike Drupal 8, Drupal 7 has not validation for entity objects what so ever. One of your modules can create a node object without any title or a language and you don't want that.

for drupal 8 Use Entity Validation before save your fields.

as a example

 $violations = $typed_data->validate();
    $definition = DataDefinition::create('integer')
      ->addConstraint('Range', ['min' => 5]);
    $typed_data = \Drupal::typedDataManager()->create($definition, 10);
  if ($violations->count() == 0) { // this means there is not any validation error
     // save Entity
  }
4
  • @Smalution Oops, so there is no convenient way, you can write your own custom validator and check items before saving or wait for entity wrapper exceptions :D
    – Yuseferi
    Mar 17, 2017 at 8:41
  • Whaaat? How does Drupal itself do it? There must be a function somewhere!
    – Chris
    Mar 17, 2017 at 8:52
  • @Chris Unlike Drupal 8, Drupal 7 has not validation for entity objects what so ever.
    – Yuseferi
    Mar 17, 2017 at 8:57
  • @Smalution Check the Answer again, I update it.
    – Yuseferi
    Mar 17, 2017 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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