问题
In a Drupal Webform, I'd like to alter the submitted value (e.g. to strip any non-numeric character) when going through the validator.
I follow the validator readme to add in hook_webform_validation_validators
and implement the hook_webform_validation_validate
hook. However I cannot locate a return parameter to alter the submitted webform value.
For example, if a user enters $12,340
, I'd like to fail the submission and update the webform value to 12340
.When the user submits the second time, the new value 12340
will pass the validator and be saved.
回答1:
I don't think that the Webform Validation module allows you to change the submitted values. I've looked at how it implements the validation and you can do something similar in your own module if you want to change the submitted value.
The following code is taken in part from http://fleetthought.com/adding-additional-cck-validation-function-field-using-hookformalter and also from the Webform Validation module code.
function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'webform_client_form_') !== FALSE) {
// Simply add an additional link validate handler.
$first = array_shift($form['#validate']);
array_unshift($form['#validate'], $first, 'form_alterations_link_validate');
}
}
function form_alterations_link_validate($form, &$form_state) {
// Access submitted values through $form_state['values']['submitted']
}
In the form_alterations_link_validate
, you can use Drupal's form_set_value() method to change submitted form values during form validation.
来源:https://stackoverflow.com/questions/6728537/how-to-replace-field-value-in-a-drupal-webforms-validator