| 6 |
function jsv_init() { |
function jsv_init() { |
| 7 |
drupal_add_js(drupal_get_path('module', 'jsv').'/js/validator.js'); |
drupal_add_js(drupal_get_path('module', 'jsv').'/js/validator.js'); |
| 8 |
drupal_add_css(drupal_get_path('module', 'jsv').'/css/validator.css'); |
drupal_add_css(drupal_get_path('module', 'jsv').'/css/validator.css'); |
| 9 |
} |
} |
|
|
|
|
/** |
|
|
* Implementation of hook_menu(). |
|
|
*/ |
|
|
function jsv_menu($may_cache){ |
|
|
$items = array(); |
|
|
//if ($may_cache){ |
|
|
$items[] = array( |
|
|
'path' => 'test', |
|
|
'title' => t('Validator test'), |
|
|
'callback' => 'drupal_get_form', |
|
|
'callback arguments' => array('jsv_form_test'), |
|
|
'access' => TRUE, |
|
|
); |
|
|
$items[] = array( |
|
|
'path' => 'validate/username/'.arg(2), |
|
|
'title' => t(''), |
|
|
'callback' => 'validate_username', |
|
|
'callback arguments' => array(arg(2)), |
|
|
'access' => TRUE, |
|
|
); |
|
|
//} |
|
|
|
|
|
return $items; |
|
|
} |
|
|
|
|
|
function jsv_form_test(){ |
|
|
$form['numeric'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('numeric'), |
|
|
'#attributes' => array('class' => 'numeric'), |
|
|
); |
|
|
$form['email'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('email'), |
|
|
'#attributes' => array('class' => 'email'), |
|
|
); |
|
|
$form['request'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('User request'), |
|
|
'#attributes' => array('class' => 'request', 'validator-path' => 'validate/username'), |
|
|
); |
|
|
$form['request-name'] = array( |
|
|
'#type' => 'select', |
|
|
'#title' => t('User request'), |
|
|
'#options' => array( |
|
|
'username1' => 'username1', |
|
|
'username2' => 'username2', |
|
|
'admin' => 'admin', |
|
|
), |
|
|
'#attributes' => array('class' => 'request', 'validator-path' => 'validate/username'), |
|
|
); |
|
|
$form['submit'] = array( |
|
|
'#type' => 'submit', |
|
|
'#value' => t('Submit'), |
|
|
); |
|
|
|
|
|
return $form; |
|
|
} |
|
|
|
|
|
function validate_username($value){ |
|
|
$output = array(); |
|
|
$value = check_plain($value); |
|
|
|
|
|
$output['valid'] = TRUE; |
|
|
$results = db_query("SELECT * FROM users WHERE name = '%s'", $value); |
|
|
while ($result = db_fetch_array($results)){ |
|
|
$output['valid'] = FALSE; |
|
|
$output['message'] = 'Username <strong>'.$result['name'].'</strong> is already taken.'; |
|
|
} |
|
|
|
|
|
echo json_encode($output); |
|
|
} |
|