Cakephp 1.2 – một số hàm hepler thường dùng (Phần 2)
Sau đây sẽ là một số hàm hepler thường dùng trong form:
8. form->input: Tạo ra thẻ html input
Cú pháp:
$form->input(string $fieldName, array $options = array()) ;
Thẻ input trong cakephp là thẻ được sử dụng nhiều nhất, hiệu quả nhất và phức tạp nhất trong hepler form của cakephp. Trước tiên ta hãy tìm hiểu cách sinh ra các phương thức tự động trong Formhepler.
a. Quy cách sinh tự động các thẻ HTML.
Thẻ input sẽ dựa vào loại dữ liệu (type) của model để tự động sinh ra các thẻ html tương ứng. Sau đây là một bảng liệt kê các thẻ html trả về tương ứng với loại dữ liệu:
| Kiểu dữ liệu | Thẻ form trả về |
|---|---|
| string (char, varchar, etc.) | text |
| boolean, tinyint(1) | checkbox |
| text | textarea |
| text, with name of password, passwd, or psword | password |
| date | day, month, and year selects |
| datetime, timestamp | day, month, year, hour, minute, and meridian selects |
| time | hour, minute, and meridian selects |
Cho ví dụ, giả sử model User bao gồm các trường là username(varchar), password(varchar), approved(datetime) và quote(text). Chúng ta có thể chỉ sử dụng một phương thức input trong FormHepler là có thể tạo ra được tất cả các thẻ html khác nhau.
Ví dụ:
<?php echo $form->create(); ?>
<?php
echo $form->input('username'); //text
echo $form->input('password'); //password
echo $form->input('approved'); //day, month, year, hour, minute, meridian
echo $form->input('quote'); //textarea
?>
<?php echo $form->end('Add'); ?>
Ta cũng có thể mở rộng một vài thuộc tính trong thẻ input như sau:
echo $form->input('birth_dt', array( 'label' => 'Date of birth' , 'dateFormat' => 'DMY' , 'minYear' => date('Y') - 70, 'maxYear' => date('Y') - 18 ));
b. Quy ước tên trường trong phương thức input
Form hepler rất thông minh, khi bạn xác định tên một trường với phương thức của form helper, cakephp sẽ tự động sử dụng tên model hiện đại để dịnh dạng theo cấu trúc dưới đây.
<input type=”text” id=”ModelnameFieldname” name=”data[Modelname][fieldname]“>
Bạn cũng có thể chỉ ra tên model bằng truyển tên model như tham số đầu tiên (Modelname.fieldname).
<?php echo $form->input(‘Modelname.fieldname’); ?>
Trong trường hợp bạn muốn sử dụng nhiều tên trường giống nhau, chúng ta có thể tạo ra một mảng array và lưu chúng vào CSDL bằng phương thức saveAll() theo quy ước như ví dụ sau:
<?php
echo $form->input('Modelname.0.fieldname');
echo $form->input('Modelname.1.fieldname');
?>
//Dữ liệu trả về HTML
<input type="text" id="Modelname0Fieldname" name="data[Modelname][0][fieldname]">
<input type="text" id="Modelname1Fieldname" name="data[Modelname][1][fieldname]">
c. Các options trong thẻ input ($options)
c.1. $options['type']
Bạn có thể chỉ định loại của thẻ input tạo ra giống như trong html. Type sẽ được gán hợp lệ như sau: file, password, text, textarea ….
Ví dụ:
<?php echo $form->input('field', array('type' => 'file')); ?>
//Dữ liệu trả về HTML
<div class="input">
<label for="UserField">Field</label>
<input type="file" name="data[User][field]" value="" id="UserField" />
</div>
c.2. $options['options']
Từ khóa này sẽ tạo ra cho bạn một thẻ select input hoặc một thẻ radio group. Nếu muốn sử dụng với thẻ radio, bạn phải dùng thêm $option['type']. Mặc định sẽ tạo ra một thẻ select input. (Lưu ý: từ khóa options sẽ luôn được gán với một mảng array)
Ví dụ:
<?php echo $form->input('field', array('options' => array(1,2,3,4,5))); ?>
//Dữ liệu trả về HTML
<div class="input">
<label for="UserField">Field</label>
<select name="data[User][field]" id="UserField">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
</div>
Bạn cũng có thể sử dụng cặp key-value như sau:
<?php echo $form->input('field', array('options' => array(
'Value 1'=>'Label 1',
'Value 2'=>'Label 2',
'Value 3'=>'Label 3'
))); ?>
//Dữ liệu trả về HTML
<div class="input">
<label for="UserField">Field</label>
<select name="data[User][field]" id="UserField">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
<option value="Value 3">Label 3</option>
</select>
</div>
c.3. $options['multiple']
Nếu ‘mutiple’ được gán là true cho một thẻ input mà đầu ra là thẻ select input, thì bạn sẽ có thể lựa chọn nhiều tùy chọn selection cùng một lúc.
Ví dụ:
$form->input('Model.field', array( 'type' => 'select', 'multiple' => true ));
c.4. $options['maxLenght']
Xác định số ký tự cho phép nhập vào trong một thẻ text input.
c.5. $options['label']
Gán nhãn cho một thẻ input khi thẻ đó được hiển thị
Ví dụ:
<?php echo $form->input( 'User.name', array( 'label' => 'The User Alias' ) );?> //Dữ liệu trả về dạng HTML <div class="input"> <label for="UserName">The User Alias</label> <input name="data[User][name]" type="text" value="" id="UserName" /> </div>
Chúng ta cũng có thể gán key label là false nếu không muốn hiển thị nhãn label trong thẻ
Ví dụ:
<?php echo $form->input( 'User.name', array( 'label' => false ) ); ?> //Dữ liệu trả về dạng HTML <div class="input"> <input name="data[User][name]" type="text" value="" id="UserName" /> </div>
c.6. $options['id']
Gán giá trị DOM id của một thẻ input.
c.7. $options['default']
Sử dụng kết hợp với thẻ select input. Gán một giá trị là defaul trong tập hợp các tùy chọn đó khi hiển thị .
Ví dụ;
<?php echo $form->input('country', array('options'=>$countries, 'default'=>'US')); ?>
c.8. $options['empty']
Khi sử dụng với thẻ select list, một thẻ option với giá trị rỗng trong list chọn của thẻ input. Nếu muốn có một giá trị rỗng trong option, ta chỉ việc truyển giá trị rỗng.
Ví dụ:
<?php echo $form->input('field', array('options' => array(1,2,3,4,5), 'empty' => '(choose one)')); ?>
//Dữ liệu trả về dạng HTML
<div class="input">
<label for="UserField">Field</label>
<select name="data[User][field]" id="UserField">
<option value="">(choose one)</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>
</div>
9. form->hidden: Tạo ra một thẻ input hidden
Cú pháp
$form->hidden(string $fieldName, array $options)
Ví dụ:
<?php echo $form->hidden('field', array('id' => 'field')); ?>
//Dữ liệu trả về HTML
<input type="hidden" name="data[ModelName][field]" id="field" />
10. form->password: Tạo ra một thẻ input dạng password
Cú pháp:
$form->password(string $fieldName, array $options);
Ví dụ:
<?php echo $form->password('pass', array('id' => 'pass')); ?>
//Dữ liệu trả về HTML
<input type="password" name="data[ModelName][pass]" id="pass" />
11. form->text: Tạo ra một thẻ input text
Cú pháp:
$form->text(string $fieldName, array $options);
Ví dụ:
<?php echo $form->text('field', array('id' => 'field')); ?>
//Dữ liệu trả về HTML
<input type="text" name="data[ModelName][field]" id="field" />
12. form->textarea: Tạo ra một thể input textarea
Cú pháp:
$form->textarea(string $fieldName, array $options)
Ví dụ
<?php echo $form->textarea('field', array('id' => 'field')); ?>
//Dữ liệu trả về HTML
<textarea name="data[ModelName][field]" id="field" />
13. form->submit: Tạo ra một nút submit
Cú pháp:
$form->submit(string $caption, array $options)
Tạo ra một nút button submit với nhãn là $captió. Nếu $caption là một URL của một image, thì nút submit sẽ hiện thị như một images.
Ví dụ:
<?php echo $form->submit('save', array('id' => 'field')); ?>
//Dữ liệu trả về HTML
<button type="submit" name="data[ModelName][save]" id="field" />
14. form->checkbox: Tạo ra một thẻ checkbox
Cú pháp:
$form->checkbox(string $fieldName, array $options)
Tạo ra một thẻ checkbox. Phương thức này cũng sẽ tạo ra một thẻ input hidden ẩn để phục vụ gửi dữ liệu lên.
Ví dụ
<?php echo $form->checkbox('done'); ?>
//Dữ liệu trả về dạng HTML
<input type="hidden" name="data[ModelName][done]" value="0" id="UserDone_" />
<input type="checkbox" name="data[ModelName][done]" value="1" id="UserDone" />
15. form->radio: tạo ra một thẻ radio button.
Cú pháp:
$form->radio(string $fieldName, array $options, array $attributes)
Tạo ra một thẻ radio button input. $attributes['value'] để gán giá trị được set mặc định. Thuộc tính radio sẽ tạo ra một label và fieldset với giá trị mặc định, nếu muốn thay đổi thì gán $attributes['legend'] là false.
Ví dụ:
<?php echo $form->radio('User.sex', array(0 => 'male', 1 => 'female'), array('value' => '1')); ?>
//Dữ liệu trả về dạng html
<fieldset>
<legend>Sex</legend>
<input type="radio" name="data[User][sex]" id="UserSex0" value="0" />
<label for="UserSex0">male</label><input type="radio" name="data[User][sex]" id="UserSex1" value="1" checked="checked" />
<label for="UserSex1">female</label>
</fieldset>
16. form->select: tạo ra một thẻ select
Cú pháp:
$form->select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)
Tạo ra một thẻ input select, với các item nằm trong $options, với tùy chọn $selected ta có thẻ chọn một item mặc định được chọn. Với giá trị $showEmpty gán là false, nếu muốn hiện ra một lựa chọn với giá trị trống làm mặc định.
Ví dụ:
<?php echo $form->select('age', array(0 => 'male', 1 => 'female'), '1'); ?>
//Dữ liệu trả về HTML
<select name="data[age]" id="age">
<option value=""></option>
<option value="0">male</option>
<option value="1" selected="selected">female</option>
</select>
17. form->file: Tạo ra một thẻ input file
Cú pháp:
$form->file(string $fieldName, array $options)
Ví dụ:
<?php echo $form->file('fileName', array('id' => 'fileName')); ?>
//Dữ liệu trả về dạng HTML
<input type="file" name="data[fileName]" id="fileName" value="" />
18. form->submitImage: Tạo ra một nút button submit ảnh
Cú pháp:
$form->submitImage(string $path, array $options);
Tạo ra một nút button submit ảnh theo đường chỉ định URL trong $path
Ví dụ:
<?php echo $form->submit('anh.jpg', array('id' => 'anh1')); ?>
//Dữ liệu trả về dạng HTML
<div class="submit"><input type="image" src="/projectName/img/anh.jpg" id="anh1" /></div>
(Chú ý: Tất cả các phương thức được giới thiệu từ phần 9 trở đi đều có thể dùng phương thức input ở phần 8 thay thế được)

echo __(‘Hi’ . $i->php(),true) . ‘1′;
Bài viết của bạn khá đầy đủ.
Mình có một vấn đề như sau:
Cụ thể như sau:
Ví dụ: Mình có 1 controller là AController và 1 Model là BModel
customController extends AppController(){
var $name = ‘B’;
var $uses = array(‘B’);
var $components = array(‘Auth’,'Acl’),
function login(){
}
}
và mình có file login.ctp trong views/b
và nội dung file login.ctp như sau
create(‘B’,array(‘url’=>array(‘controller’=>’a',’action’=>’login’)));
?>
khi view source login.ctp mình muốn action của form login.ctp như sau
action = ‘myweb/a/login’
nhưng thực tế thì nó không in ra đúng như mong muốn.
Vấn đề ở đay là hàm $form->create(tenmodel,$opttion) chắc mình sử dụng ko đúng.
Hi vọng bạn hiểu và giúp mình.
Thanks in advanced!