如果不使用任何第三方模块,是否可以同时验证并检查在文本框中输入的任何URL在Drupal 6中是否有效?一些示例代码将不胜感激。
答案 0 :(得分:1)
menu_valid_path()函数就是这样做的。
回答您的具体问题:
不使用任何第三方模块,是否可以同时验证并检查在文本框中输入的任何URL在Drupal 6中是否有效
是强>
但是,您需要创建一个简单的自定义模块。
我们假设:
my_form_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.'));
}
}