2012-12-14 47 views

回答

1

menu_valid_path() menu.inc(它是Drupal Core的一部分)的功能就是這麼做的。

爲了回答您的具體問題:

不使用任何第三方模塊,是能夠驗證,並在同一時間檢查,如果在文本框中輸入任何網址是否有效在Drupal 6

是的。

但是,您將需要創建一個簡單的自定義模塊。

假設:

  1. form idmy_form_1
  2. 相關字段的名稱是my_path_field_1

在你MODULENAME.module文件:

<?php 
/** 
* Modifies the existing form element 'my_path_field_1' to add 
* 'MODULENAME_path_validate' function to validation array. 
* 
* (MYMODULE_path_validate is defined below) 
*/ 
function MODULENAME_form_alter(&$form, $form_state, $form_id) { 
    switch ($form_id) { 
    case 'my_form_1' : 
     $form['my_path_field_1']['#element_validate'] = array('MODULENAME_path_validate'); 
    break; 
} 
// Note, you could use hook_form_FORM_ID_alter(&$form, &$form_state) 
// instead of the above to simplify things if the only thing this module 
// does is validite one field for a valid path. 

/** 
* Validates the my_path_field_1 using Drupal's built-in menu_valid_path() 
* function. Returns a form error if the field does not contain a valid path 
* or the current user does not have access to the path's permission. 
*/ 
function MODULENAME_path_validate($element, &$form_state) { 
    if (!menu_valid_path($element)) { 
    form_error($element, t('The path entered does not exist or you do not have permission to access it.')); 
    } 
} 
+0

感謝亞當.. 。!這已經足夠明確:) –

+0

這似乎不起作用。如果我輸入的鏈接應該是404,它仍然有效。 – Kezaia