【问题标题】:I want to validate after submission of the form but it also validate before submission of the form using jquery in drupal forms我想在提交表单后进行验证,但它也在 drupal 表单中使用 jquery 在提交表单之前进行验证
【发布时间】:2017-02-22 09:50:03
【问题描述】:

在有一个带有两个值电子邮件和电话的单选按钮的联系表单中,我想在提交表单后进行验证,但它也在 drupal 表单中使用 jquery 提交表单之前进行验证.. 在我的表单中,当我单击电子邮件时,它会显示一个带有错误的电子邮件文本字段并隐藏电话文本字段,类似于我选择电话它同时显示电子邮件和电话文本字段但仅对于电话文本字段它显示电子邮件字段的错误它现在不显示错误我完成这项工作在单击提交按钮之前我的prblm是什么我更改了电子邮件和电话值它显示错误但我只想在提交表单后显示错误..

【问题讨论】:

标签: javascript php jquery forms drupal


【解决方案1】:

您应该能够在您的字段上使用#states 属性来执行您所要求的操作。您可以添加多个验证,它将为您创建所有 jquery。

例如:

// Show the email field when email selected for radiofld
$form['email'] = array(
  '#type' => 'textfield',
  '#title' => t('Email'),
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="radiofld"]' => array('value' => 'email'),
      ),
    ),
  ),
);

// Show error message for phone field
$form['phoneerr'] = array(
    '#type' => 'markup',
    '#markup' => 'No Phone selected',
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="radiofld"]' => array('value' => 'email'),
      ),
    ),
  ),
);  

// Show the phone field when phone selected for radiofld
$form['phone'] = array(
  '#type' => 'textfield',
  '#title' => t('Phone'),
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="radiofld"]' => array('value' => 'phone'),
      ),
    ),
  ),
);

// Show error message for email field
$form['emailerr'] = array(
    '#type' => 'markup',
    '#markup' => 'No Email selected',
  '#states' => array(
    'visible' => array(
      array(
        ':input[name="radiofld"]' => array('value' => 'phone'),
      ),
    ),
  ),
);  

【讨论】:

  • 谢谢 Angus Walker,但我想使用 jquery 验证它...你能帮忙吗?
  • 为什么要自己写jquery?在上面的示例中,Drupal 使用 jquery 并为您编写!
【解决方案2】:

如果你真的坚持独立使用 jquery,那么你应该把它放在一个函数中,并用 onclick/onchange 属性调用它:

$form['wayToContact'] = array(
       '#type' => 'radios',
       '#validated' => TRUE,
       '#title' => t('Best way to contact'),
       '#default_value' => 'Email',
       '#options' => array('Email' => $this->t('Email'), 'Phone' => $this->t('Phone')),
       '#attributes' => array(
            'class' => array('required')
            'onchange' => 'yourFunction(this)',
            'onclick' => 'yourFunction(this)'
       )
     );

【讨论】:

    猜你喜欢
    • 2016-09-25
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多