Form API

Dẫn nhập

Như chúng ta đã biết, nói đến ứng dụng web là phải nói đến form. Người lập trình web rất thường xuyên gặp lỗi bảo mật khi sử lý form nhập liệu. Công việc xử lý form nhiều lúc cũng rất nhiêu khê.

Drupal là một PHP web framework, do đó chắc chắn là phải hỗ trợ việc xử lý form. FormAPI mà Drupal cung cấp vô cùng mạnh mẽ, cung cấp cách thức xây dựng form dễ dàng, kiểm tra tính hợp lệ của dữ liệu cũng đơn giản, tùy biến giao diện tự do.

Tạo form

Bằng formAPI, các phần tử của form được định nghĩa bằng các mảng với các khóa tuân theo chuẩn (Drupal). Chúng ta tạo một cái form xem nào:

<?php
// tạo một form
$form['foo'] = array(
  
'#type' => 'textfield',
  
'#title' => t('bar'),
  
'#default_value' => $edit['foo'],
  
'#size' => 60,
  
'#maxlength' => 64,
  
'#description' => t('baz'),
);
?>

và thêm vào một nút submit:

<?php
$form
['submit'] = array(
  
'#type' => 'submit',
  
'#value' => t('Save'),
);
?>

Một số điểm cần chú ý:

  1. Tên thuộc của một phần tử được định nghĩa trong mảng $form. Thí dụ, nếu một phần tử của form được định nghĩa dạng:
    <?php
    $form
    ['account_settings']['username']
    ?>

    Thì tên của thuộc tính là 'username'. Khi form được submit, PHP code có thể xử lyd khóa này thông qua biến $_POST['edit']['user'] (hoặc $form_values)
  2. Kiểu của phần tử form được định nghĩa ở khóa ['#type']
  3. Các khóa thuộc tính của phần tử được định nghĩa dạng một chuỗi, bắt đầu bằng dấu sharp (#).
  4. Thứ tự định nghĩa các khóa thuộc tính là không quan trọng. Và một số thuộc tính, bạn cũng không nhất thiết phải định nghĩa, phần lớn chúng sẽ được gán giá trị mặc định nếu không được định nghĩa.
  5. Không sử dụng khóa thuộc tính '#value' cho những phần tử form có thể thay đổi bởi người dùng. Nếu cần thì nên dùng khóa thuộc tính '#default_value'.

Bây giờ chúng ta sẽ xem xét một ví dụ sử dụng FormAPI cụ thể hơn:

<?php
function test_page() {
  
// Access log settings:
  
$options = array(
    
'1' => t('Enabled'), 
    
'0' => t('Disabled')
  );
  
$form['access'] = array(
    
'#type' => 'fieldset',
    
'#title' => t('Access log settings'),
    
'#tree' => TRUE,
  );
  
$form['access']['log'] = array
  (
    
'#type' => 'radios',
    
'#title' => t('Log'),
    
'#default_value' =>  variable_get('log'0),
    
'#options' => $options,
    
'#description' => t('The log.'),
  );
  
$period drupal_map_assoc(
    array(
360010800216003240043200864001728002592006048001209600241920048384009676800),
    
'format_interval'
  
);
  
$form['access']['timer'] = array(
    
'#type' => 'select',
    
'#title' => t('Discard logs older than'),
    
'#default_value' => variable_get('timer'259200),
    
'#options' => $period,
    
'#description' => t('The timer.'),
  );
  
// Description
  
$form['details'] = array(
    
'#type' => 'fieldset',
    
'#title' => t('Details'),
    
'#collapsible' => TRUE,
    
'#collapsed' => TRUE,
  );
  
$form['details']['description'] = array(
    
'#type' => 'textarea',
    
'#title' => t('Describe it'),
    
'#default_value' =>  variable_get('description'''),
    
'#cols' => 60,
    
'#rows' => 5,
    
'#description' => t('Log description.'),
  );
  
$form['details']['admin'] = array(
    
'#type' => 'checkbox',
    
'#title' => t('Only admin can view'),
    
'#default_value' => variable_get('admin'0),
  );
  
$form['name'] = array(
    
'#type' => 'textfield',
    
'#title' => t('Name'),
    
'#size' => 30,
    
'#maxlength' => 64,
    
'#description' => t('Enter the name for this group of settings'),
  );
  
$form['hidden'] = array('#type' => 'value''#value' => 'is_it_here');
  
$form['submit'] = array('#type' => 'submit''#value' => t('Save'));
  
$output drupal_get_form('test_page'$form);
  return 
$output;
}
?>

------------------------------------------------------------------
Xem thêm:
[1] Thử nhanh php code nhanh bằng Drupal

Comments

Unsupported operand types in

Unsupported operand types in C:\wamp\www\drupal-5.2\includes\form.inc on line 341
bao loi nay minh khong biet lam sao. Minh moi bat dau xem drupal thoi.
Cuu em voi
EmGaiVuiVe

Bạn có thể gửi script

Bạn có thể gửi script của bạn lên đây được không?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <sup> <br> <p> <h3>
  • Lines and paragraphs break automatically.
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.