Whenever I submit a nothing to an integer field using the EntityMetadataWrapper I get the following error.

What's the correct format for submitting a null value to an integer field? (I don't want 0, but a blank value).

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of C:\XAMPP\htdocs\drupal\sites\all\modules\entity\includes\entity.wrapper.inc).

How do you clear a field value with entity_metadata_wrapper()?

The post above implies I should be able to use:

$wrapper->title = '';

However this still throws the error.


I have an integer field Salary added to a custom node type. (Along with several other fields which work as expected.)


  • Not required
  • Has no minimum value
  • No default value
  • Can only hold one value.

I have a custom form which looks like this. This is the entry field:

$form['field_salary'] = array(
    '#type' => 'textfield',
    '#title' => t('Salary'),
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('Please enter a valid number'),

This is the submit function taken from here:

function postajob_form_submit($form, &$form_state) {

 $values = array(
      'type' => 'job_advert',
      'uid' => $user->uid,
      'status' => 1,
      'comment' => 1,
      'promote' => 0,
    $entity = entity_create('node', $values);

    $ewrapper = entity_metadata_wrapper('node', $entity);

    //.... (other fields in the form are here)

    $ref_nid = 15;

    $my_date = new DateTime('January 1, 2013');
    $entity->field_my_date[LANGUAGE_NONE][0] = array(
       'value' => date_format($my_date, 'Y-m-d'),
       'timezone' => 'UTC',
       'timezone_db' => 'UTC',

    entity_save('node', $entity);


My guess is that it can't hold nothing? But that seems weird because it's not required.

I can for example create the node using the drupal default form with no errors and it works fine.


The solution from Wayne, removes the error but what I was hoping for was the ability to submit a blank value without it throwing an error. Unfortunately the following doesn't work.

'#default_value' => NULL, 

I've considered using a string field instead of an integer one and then validating only for numbers, but this means I'll lose the ability to use the numeric range search facets, again not ideal.

2 Answers 2


Have you tried to check if the value exists before setting it?

//.... (other fields in the form are here)
if (!empty($form_state['values']['field_salary'])) {

This way you're not touching the value if it's not set.

  • Yep I just had to do this for another bug. Really nice way of getting around that. Secretly annoyed I could not think of it myself. Commented Feb 5, 2014 at 17:32

Try setting the #default_value of the textfield. If the field is left blank no value is submitted with the form for that name (ie. $form_state['values']['field_salary']), so essentially attempting to apply a NULL value to the metadata.

Optionally, you could cast the value to an int:

(int) $form_state['values']['field_salary']; // OR
intval($form_state['values']['field_salary']); // both => 0 (zero) when NULL

which may not be a bad idea besides to ensure only numerical values are applied. This is, of course, assuming that you do not validate that before hand.

  • The default value option does remove the error (hooray), but unfortunately it does have the problem that it puts a value into the integer field. There's a difference between offering a salary of 0 and leaving the field blank. I'm trying to get the second one. Any ideas how I would get it to accept a blank field? (tried default value = NULL) Commented Aug 21, 2013 at 19:25
  • Try giving an empty string first ( "" ), but I will bet that wont work either (Drupal forms are the bane of my existence). You could also do (isset($value)) ? $value : ""; in the submit to try and force the empty string. An even worse hack would be to send a single space and then trim() the value. Commented Aug 21, 2013 at 19:47
  • Sorry, I am not entirely familiar with all the search facets, but would sending a default value of -1 work? You will probably need to write a conditional (for the facet) or validation (of the form) or both to achieve exactly what ya want. Commented Aug 21, 2013 at 20:15
  • So the empty string and blank value hack don't work, I found this thread which seems to suggest it should. Will post when I find an answer. Commented Aug 22, 2013 at 9:23

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.