开发者

How do I apply webform validation in drupal 7?

开发者 https://www.devze.com 2023-03-22 07:55 出处:网络
I have webforms in my drupal 7 website. What I want is to validate my webform fields. The webformcontains a phone field which should accept numeric field and should contain 10 numbers only. Is there a

I have webforms in my drupal 7 website. What I want is to validate my webform fields. The webform contains a phone field which should accept numeric field and should contain 10 numbers only. Is there any module for this or will I have to code 开发者_StackOverflow中文版for this.


Use hook_form_alter() to apply custom validation in drupal

create module e.g. mymodule

file mymodule.module

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
    print $form_id;

    if($form_id=='webform_client_form_1') //Change webform id according to you webformid
    {
        $form['#validate'][]='mymodule_form_validate';
        return $form;
    }
}

function mymodule_form_validate($form,&$form_state)
{
    //where "phone" is field name of webform phone field
    $phoneval = $form_state['values']['submitted']['phone'];

    if($phoneval=='')
    {
        form_set_error('phone','Please fill the form field');
    }
    // Then use regular expression to validate it.
    // In above example i have check if phonefield is empty or not.
}

If you want to more to detail how to use hook_form_alter() visit this link http://www.codeinsects.com/drupal-hook-system-part-2.html


There is a module named Webform Validation where we can set validation rules for each fields.

Here is an excerpt from its project page:

... adds an extra tab to each webform node, allowing you to specify validation rules for your webform components. You can create one or more of the predefined validation rules, and select which webform component(s) should be validated against those. By using the hooks provided by this module, you can also define your own validation rules in your own modules.

0

精彩评论

暂无评论...
验证码 换一张
取 消