hook _menu 实现
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
// created the custom fields using form api
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
$form['phone']= array(
'#type' => 'textfield',
'#title' => t('Phone Number'),
'#maxlength' => 10,
'#size' => 10,
);
$form['email']= array(
'#type' => 'textfield',
'#title' => 'Email',
'#rules' => array(
'email',
),
);
// New form field added to permit entry of year of birth.
// The data entered into this field will be validated with
// the default validation function.
$form['address'] = array(
'#type' => 'textfield',
'#title' => "address",
);
$form['choice'] = array(
'#type' => 'radios',
'#title' => t('Gender'),
'#options' => array(
t('Male'),
t('Female'),
t('Transgender'),
)
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
'#submit' => array('my_module_my_form_submit'),
);
return $form;
}
Done some validation for the form fields
function my_module_my_form_validate($form, &$form_state) {
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$phone = $form_state['values']['phone'];
$name = $form_state['values']['name'];
$email = $form_state['values']['email'];
$address = $form_state['values']['address'];
$choice = $form_state['values']['choice'];
if (!$name) {
form_set_error('name', 'Please enter your name.');
}
if (!$phone) {
form_set_error('phone', 'Please enter your Phone Number.');
}
if (!$email) {
form_set_error('email', 'Please enter your Email.');
}
else if (!preg_match($regex, $email)) {
form_set_error('Email', 'Enter correct Email');
}
if (!$address) {
form_set_error('address', 'Please enter your Address.');
}
if (!isset($choice) )
{
form_set_error('choice', 'Please enter your Gender.');
}
if (!preg_match('/^[1-9][0-9]*$/',$phone)) {
form_set_error('phone', 'Enter correct phone number');
}
}
function my_module_my_form_submit($form,&$form_state) {
drupal_set_message(t('The form has been submitted.'));
}
?> 我是 drupal 的新手,我需要知道如何使用 Form Api 在 drupal 中实现节点保存
- 这些字段在提交中验证我需要将表单结果保存在 Employee 类型的内容页面中
- 我创建了员工内容类型,字段如下
姓名电话电子邮件地址性别我想将表单值保存在员工类型的内容中,其中员工是已经创建的内容类型请帮助我,我是drupal的新手