Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've been through the example and docs on this module and the fapi_validation module to see if I can understand how to validate a non-required text field which is also a multi-value field (either fixed or unlimited). I have tried using hook_form_alter() to achieve this without success. The site in question is Drupal 6 and the form is a node/add with various cck fields added.
Am I missing something obvious, here is my test code:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'booking_node_form') {
$form['field_code']['#rules'] = array('rule' => 'alpha_numeric', 'error' => 'Please only enter alpha numeric characters');
}
}
Any thoughts?
Comments
Comment #1
attiks CreditAttribution: attiks commentedCan you check the drupal.settings in the html to see what's in there
Comment #2
ghazlewood CreditAttribution: ghazlewood commentedThere's nothing for the field in question in the Drupal.settings - I had checked there but assumed that my code was wrong in some way. Do I need to make any changes considering this is not a required field (but still needs validating if it is provided, or that it is a multi-value text field?
Comment #3
attiks CreditAttribution: attiks commentedJust as a test: if you make the field required, does it have your validation?
Comment #4
ghazlewood CreditAttribution: ghazlewood commentedMaking it required but leaving it as a multi-value field makes no difference. It gets validated as required but the new rule doesn't appear. Changing it to be a single value text field, both required and not also doesn't work. I'm sure I'm missing the obvious here but whichever way I try it I can't get it to validate a custom rule.
Thanks for the suggestions!
Comment #5
ghazlewood CreditAttribution: ghazlewood commentedLooks like the rule is firing (although not perfectly) when i submit the form, just not when using JS with Clientside Validation.
Comment #6
attiks CreditAttribution: attiks commentedDid you define your rule in javascript as well? See #1281494: Extending Client side validation to build custom form validations. and/or #1286870: example for defining extra validation rules
Comment #7
ghazlewood CreditAttribution: ghazlewood commentedNo I hadn't thought this through before, I was expecting some magic pixie dust to make it work without having to write any more code. Silly me. I think I understand what I need to do now, apologies.