0

Maybe someone could help me ...

Ive got a webform with an input (postal code). I want its maxlength to be 5.

When users fill it, once the 5 digits entered, an autocomplete callback retrieves corresponding cities.

When a city is chosen, the input content is rewritten like "XXXXX - CITY". Now the field length is more than 5.

When the form is submitted, it is not validated.

I would like to bypass the validation of this specific property (maxlength) ...

Is it possible ?

5
  • Sorry to say that, but I don't understand it.
    – Mołot
    Commented Aug 1, 2014 at 11:38
  • ok im gonna rewrite it ;)
    – Math666
    Commented Aug 1, 2014 at 11:41
  • 1
    Maybe you could provide an example of what text doesn't go through and what text should go through Commented Aug 1, 2014 at 11:42
  • is it more comprehensive now ?
    – Math666
    Commented Aug 1, 2014 at 11:55
  • Maxlength is specified thanks to webform default property, but I also have webform validation that I could use ...
    – Math666
    Commented Aug 1, 2014 at 11:56

1 Answer 1

0

you will need to write your own validation function - borrowed from here:

Adding custom validation rules - Webform Validation hooks

Last updated January 7, 2014. Created by svendecabooter on September 11, 2010. Edited by Liam Morland, wundo, crystaldawn. Log in to edit this page.

This page describes how to write custom validation rules for your webforms. This requires installation of the Webform and Webform Validation modules.

The following code should be implemented in your own custom module.

Adding custom validation rules The following steps will let you add custom validators through your module:

1/ Implement hook_webform_validation_validators(). This hook implementation should return an array of validator key => options array entries. The options array can contain the following configuration keys:

name (required): name of the validator

component types (required): defines which component types can be validated by this validator. Specify 'all' to allow all types

custom_error (optional): define whether a user can specify a custom error message upon creating the validation rule.

custom_data (optional): define whether custom data can be added to the validation rule

min_components (optional): define the minimum number of components to be selected for creating a validation rule

max_components (optional): define the maximum number of components to be selected for creating a validation rule

description (optional): provide a descriptive explanation about the validator See function webform_validation_webform_validation_validators() in

webform_validation.validators.inc for a live example

2/ Implement hook hook_webform_validation_validate($validator_name, $items, $components, $rule). This hook gets passed 4 parameters, which will allow you to react to your custom validator (or any other validator for that matter). Explanation about these parameters:

$validator_name: this is the validator name (i.e. array key as entered in hook_webform_validation_validators)

$items: array containing user submitted entries to be validated. $components: this array contains the definitions of the webform components in your form

$rule: this array contains the details of your validation rule See function webform_validation_webform_validation_validate() in webform_validation.validators.inc for a live example

Additional hooks The hook hook_webform_validation($type, $op, $data) can be used to react on various webform_validation based actions.`

$type - possible values: 'rule'

$op - possible values: 'add', 'edit', 'delete'

$data - array with rule data in case of $op add/edit, rule id in case of $op delete.

The hook hook_webform_validator_alter(&$validators) can be used to alter the array of validators that is being generated by hook_webform_validation_validators().

$validators - array of validators as supplied by modules implementing hook_webform_validation_validators(). Example

<?php
/**
 * Implements hook_webform_validation_validators().
 */
function idvalidation_webform_validation_validators() {
  return array(
    'validate_id' => array(
      'name' => "Validate ID",
      'component_types' => array(
        'textfield',
      ),
    )
  );
}
/**
 * Implements hook_webform_validation_validate().
 */
function idvalidation_webform_validation_validate($validator_name, $items, $components, $rule) {
  if ($items) {
    switch ($validator_name) {
      case 'validate_id':
        $valid_ids = array(1, 2, 3); // get this from database source instead
        foreach ($items as $key => $val) {
          if ($val && (!in_array($val, $valid_ids))) {
            $errors[$key] = t('%item is not a valid ID', array('%item' => $components[$key]['name']));
          }
        }
        return $errors;
        break;
    }
  }
}
?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.