Drupal Commerce alter checkout form and custom validate

转载 2016年06月01日 15:06:30

Drupal Commerce: How to alter the checkout form and how to add the custom validation

Commerce has its own API http://api.drupalcommerce.org/, that we have used it to adapt the checkout form to our requirements.

Description of the required functionality

In the billing information of an order we have added the fields “Request invoice” and “NIF/CIF” (ID). By default the field “Request invoice” is not checked and “NIF/CIF” is hidden and not required. When the buyer checks the field “Request invoice” the field “NIF/CIF” becomes visible and required.

Proposed solution

  1. Adding the code that changes the field “NIF/CIF” when the user checks “Request invoice”. By using hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)
  2. Adding a callback to validate the form hook_commerce_checkout_pane_info_alter(&$checkout_panes)
  3. Adding the validation function commerce_billing_pane_validate(&$form, &$form_state, $checkout_pane, $order)

Step 1. Adding the alter form to add the behavior of the field. field_request_invoice

?
1
2
3
4
5
6
7
8
9
10
11
function example_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
  global $user;
  $form['customer_profile_billing']['field_cif_nif']['#states'] = array(
    'visible' => array(
      ':input[name="customer_profile_billing[field_request_invoice][und]"]' => array('checked' => TRUE),
    ),
    'required' => array(
      ':input[name="customer_profile_billing[field_request_invoice][und]"]' => array('checked' => TRUE),
    ),
  );
}

Step 2. Alter the customer_profile_billing panel to add the callback to the validation function

?
1
2
3
4
5
function example_commerce_checkout_pane_info_alter(&$checkout_panes) {
  if (isset($checkout_panes['customer_profile_billing'])) {
    $checkout_panes['customer_profile_billing']['callbacks']['checkout_form_validate'] = 'example_commerce_billing_pane_validate';
  }
}

Step 3. Adding the custom validation function

?
1
2
3
4
5
6
7
function example_commerce_billing_pane_validate(&$form, &$form_state, $checkout_pane, $order) {
  if ($form_state['values']['customer_profile_billing']['field_request_invoice']['und'][0]['value'] == 1 && empty($form_state['values']['customer_profile_billing']['field_cif_nif']['und'][0]['value'])) {
    form_set_error($checkout_pane['pane_id'] . '][field_cif_nif][und][0][value', t('DNI/CIF/NIF field is required.'));
    return FALSE;
  }
  return TRUE;
}

Now, the billing panel in the checkout form shows this

Commerce checkout validation

 

We hope this is of help. If you know another way to do this or you have another idea you can write a comment. Thanks!

Spring MVC Form Validation Example using Annotation and Custom Validator implementation

Spring MVC Form Validation Example using Annotation and Custom Validator implementation Pankaj April...

Spring MVC Display, Validate and Submit Form Example

In any web application, you often have to deal with forms. Application first displays a form and aft...
  • gmemai
  • gmemai
  • 2015年08月31日 15:49
  • 476

drupal7之学习在已存在的form表单中增加自定义验证(Learn how to add custom validation to an existing Drupal form)

注:原文链接:http://befused.com/drupal/form-validation(翻译水平有限,还望海涵) start: 表单验证是web系统中必不可少的一部分,你需要确保用户输入...
  • quuqu
  • quuqu
  • 2015年04月06日 22:46
  • 622

Undoing in Git - Reset, Checkout and Revert

Undoing in Git - Reset, Checkout and Revert Git provides multiple methods for fixing up mistakes ...
  • xqs83
  • xqs83
  • 2012年03月22日 12:41
  • 1169

jquery-form-validate

  • 2014年05月09日 14:19
  • 16KB
  • 下载

jquery-form-validate.1.2.zip

  • 2013年06月24日 13:04
  • 140KB
  • 下载

使用Custom.pll修改标准Form的LOV

在开发过程中,经常碰到需要修改标准Form中LOV的需求,一般来说都是做一些条件限制。在不修改标准Form的fmb文件的前提下,可以通过Custom.pll来实现。   一、Custom.pll的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Drupal Commerce alter checkout form and custom validate
举报原因:
原因补充:

(最多只允许输入30个字)