Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Not sure this is the right place for this question but:
I'd like to validate a person's id number (not in drupal database) typed into a text field, against a list of valid numbers (a prebuilt array/text list, one id per line) — is this possible?
Thanks!!
Comments
Comment #1
svendecabooterYeah that shouldn't be too hard with a custom module that implements the proper hooks that are supplied by the webform_validation module.
Below is some (untested) code that should get you started if you put it in a module called 'idvalidation'.
Comment #2
svendecabooterComment #4
beckyjohnson CreditAttribution: beckyjohnson commentedI have a question about this line:
$valid_ids = array(1, 2, 3); // get this from database source instead
How do you get this from a database source? Can you query a content type that the value is stored in? If anyone is still reading this, I would really like to see an example of how to grab this data from another database.Thanks for any comments. I am trying to use webform validate to validate against a bunch of serial numbers formatted as such: 1234-1234-1234-1234.
Rebecca
Comment #5
svendecabooterHi,
Basically it heavily depends on your use case, so there's not one definite solution as to how to get the values you want to validate against. Given 100 sites it will probably be a different approach a 100 times... It also will vary a lot based on whether you are using Drupal 6 or 7.
I would suggest you look into the Database API that is provided by Drupal:
- http://api.drupal.org/api/drupal/includes--database.inc/group/database/6 for Drupal 6
- http://api.drupal.org/api/drupal/includes--database--database.inc/group/... for Drupal 7
If you'd like to get the data from a content type (i.e. node) that contains the values, you should familiarise yourself with the node API (e.g. the node_load function)
Hope that gets you on your way...
If you need more specific support to query the data, i suggest you open a new topic in the drupal.org forums or stop by on IRC #drupal-support, as there will be more people there to support you. Since your issues are most likely not specifically related to the webform_validation but to Drupal APIs in general, you might get more luck at these places getting help for your specific use case.
Good luck!
Sven
Comment #6
beckyjohnson CreditAttribution: beckyjohnson commentedIf I import the numbers into their own content type, could I find the numbers by querying that content type in my code? I got this idea from reading book on drupal module building....
Comment #7
svendecabooterShould be possible, depending on your coding skillset & set up structure. If you take the example above, you can write any PHP code to check for validation, and return the $errors array if a problem occurred.
Comment #8
TechNikh CreditAttribution: TechNikh commented+1 bookmarking..
Comment #9
stupiddingo CreditAttribution: stupiddingo commentedHere's two separate strategies based on @svendecabooter's example above to accomplish authorization code lookup that someone might find useful. These add custom errors as well.
1.) Validate against a user-entered (form author) list of validation codes
NOTE: This will allow one code to be used multiple times.
webform_validationauthcode.module
2.) Validate against a provided web service URL
webform_validationservice.module
Both will need .info files requiring webform and webform_validation.
Definitely must be better ways to accomplish this, but these two modules are stupid simple and work. Hope this helps someone, and I welcome improvements (new to Drupal and PHP).
Comment #10
beckyjohnson CreditAttribution: beckyjohnson commentedThank you for this work! It's really great to study. I ended up having a coworker help me make a module that added a table to my database and stored numbers I uploaded in CSV files and then , when the user entered a number, it got checked against that table.
Rebecca
Comment #11
mari3.14 CreditAttribution: mari3.14 commented@beckyjohnson, I was wondering whether you would mind sharing your custom module please. I am trying to do something very similar but the examples mentioned above are not enough for me to figure it out.
Ta.
Comment #12
Post_History CreditAttribution: Post_History commentedhello im trying the above mentioned methods and still cannot seem to be able see the rule as an option for webforms. am i missing a step not described here ?
Comment #13
Post_History CreditAttribution: Post_History commentednever mind specific values is enough for me i just had to change the length of the data that could be stored from 255 to 4000 !
Comment #14
dkre CreditAttribution: dkre commentedI've been struggling for about a day with implementing a database query for validation, this is based off svendecabooter's code above. (Huge thanks).
Hope this is helpful to someone.
Comment #16
mar111 CreditAttribution: mar111 commentedCan validate a Spanish bank account number? php code for this validation would be the next but I'm having to apply it to hook.
function ccc_valido($ccc)
{
//$ccc sería el 20770338793100254321
$valido = true;
///////////////////////////////////////////////////
// Dígito de control de la entidad y sucursal:
//Se multiplica cada dígito por su factor de peso
///////////////////////////////////////////////////
$suma = 0;
$suma += $ccc[0] * 4;
$suma += $ccc[1] * 8;
$suma += $ccc[2] * 5;
$suma += $ccc[3] * 10;
$suma += $ccc[4] * 9;
$suma += $ccc[5] * 7;
$suma += $ccc[6] * 3;
$suma += $ccc[7] * 6;
$division = floor($suma/11);
$resto = $suma - ($division * 11);
$primer_digito_control = 11 - $resto;
if($primer_digito_control == 11)
$primer_digito_control = 0;
if($primer_digito_control == 10)
$primer_digito_control = 1;
if($primer_digito_control != $ccc[8])
$valido = false;
///////////////////////////////////////////////////
// Dígito de control de la cuenta:
///////////////////////////////////////////////////
$suma = 0;
$suma += $ccc[10] * 1;
$suma += $ccc[11] * 2;
$suma += $ccc[12] * 4;
$suma += $ccc[13] * 8;
$suma += $ccc[14] * 5;
$suma += $ccc[15] * 10;
$suma += $ccc[16] * 9;
$suma += $ccc[17] * 7;
$suma += $ccc[18] * 3;
$suma += $ccc[19] * 6;
$division = floor($suma/11);
$resto = $suma-($division * 11);
$segundo_digito_control = 11- $resto;
if($segundo_digito_control == 11)
$segundo_digito_control = 0;
if($segundo_digito_control == 10)
$segundo_digito_control = 1;
if($segundo_digito_control != $ccc[9])
$valido = false;
return $valido;
}
Comment #17
mar111 CreditAttribution: mar111 commentedComment #18
mclinn CreditAttribution: mclinn commentedThis was a big help to me, thank you for posting!
I've been tempted to use the "dangerous, do not use!" PHP validator module but decided to play it safe. Rather than doing my form from scratch, using custom validations like this may save me a lot of time.
I did have trouble with the fetchfield() part of the code; rather than spend time investigating I just used the $val in my query, like this:
Also just used a "while" loop rather than fight with various ways to determine if the query had a result. Just validating one field here, so far it's working great!
Thanks again.
Comment #19
tmbridge CreditAttribution: tmbridge commentedHi,
I'm facing a similar predicament in that I cannot see my new rule in the list of validation rules. However, expanding the acceptable value of Specific Value(s) from 255 to 4000 would also work for me but I cannot find this setting anywhere?
Can you please direct me as to where to go to change this setting?
Thanks,
Tim