Problem
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.
Example
I have an integer field Salary added to a custom node type. (Along with several other fields which work as expected.)
Properties:
- 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)
$ewrapper->field_salary->set($form_state['values']['field_salary']);
$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',
);
$ewrapper->save(true);
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.
EDIT:
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.