I am trying to validate a Webform (textfield) component against a field in an Ubercart order custom field.
Example:
I have a custom field (voucher number) that gets automatically created when an Ubercart order is placed. Then I have a Webform where I have a multi-page form and on the first page, I have a single field (voucher number) that the user should enter from the invoice they got when placing their order.
I'm having a difficult time having the webform component checking against a db field. Here is the code that I (somewhat) had working:
function mymodule_webform_validation_validators() {
return array(
'validate_voucher' => array(
'name' => t('Validate Vouchers'),
'component_types' => array(
'textfield',
),
'description' => t('Verifies that the voucher number exists in the order data table.'),
)
);
}
function mymodule_webform_validation_validate($validator_name, $items, $components, $rule) {
if ($items) {
$errors = array();
switch ($validator_name) {
case 'validate_voucher':
$voucher_num = db_query("SELECT field_voucher_number_value FROM {field_data_field_voucher_number} WHERE entity_id > 0")->fetchField();
$voucher_number = explode(" ", $voucher_num);
foreach ($items as $key => $val) {
if (!in_array($val, $voucher_number)) {
$errors[$key] = t('%item is not a valid voucher number', array('%item' => $components[$key]['value']));
}
}
return $errors;
}
}
}
But unfortunately it does not work. The most difficult part is having the webform field check against the db field. Because I am not requiring login to use the form, there is nothing to really build the WHERE clause (like uid) so I really need some help figuring out how to check against the db field.
Comments
Comment #1
marty.true CreditAttribution: marty.true commentedI got it figured out and it is working well...
So I will post here in case anyone else wants a recipe for how to tap into a node field in the database, to check/validate against:
Comment #2
Liam Morland