Jenis bidang form Subform
From Joomla! Documentation
Jenis bidang form subform ini menyediakan suatu metode dalam menggunakan form XML di dalam form atau menggunakan kembali form di dalam form yang sudah ada sebelumnya. Jika atribut multiple diatur ke true maka form yang disertakan akan dapat diulang.
Bidangnya memiliki tata letak "predefined" yang sudah ada untuk menampilkan subform tersebut baik sebagai sebuah tabel atau div, serta mendukung tata letak kustom.
Sebuah contoh definisi bidang XML untuk mode tunggal:
<field name="field-name" type="subform"
formsource="path/to/exampleform.xml"
label="Subform Field" description="Subform Field Description" />
Contoh definisi bidang XML untuk mode multiple:
<field name="field-name" type="subform"
formsource="path/to/exampleform.xml" multiple="true"
label="Subform Field" description="Subform Field Description" />
Contoh XML exampleform.xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<field name="example_text" type="text" label="Example Text" />
<field name="example_textarea" type="textarea" label="Example Textarea" cols="40" rows="8" />
</form>
Sebuah contoh XML exampleform.xml dengan fieldsets
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fieldset name="section1" label="Section1">
<field name="example_text" type="text" label="Example Text" />
<field name="example_textarea" type="textarea" label="Example Textarea" cols="40" rows="8" />
</fieldset>
<fieldset name="section2" label="Section2">
<field name="example_list" type="list" default="1" class="advancedSelect" label="Example List">
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>
</fieldset>
</form>
The subform XML may also be specified inline as an alternative to placing the subform XML in a separate file. The following example illustrates this:
<?xml version="1.0" encoding="UTF-8"?>
<field
name="field-name"
type="subform"
label="Subform Field"
description="Subform Field Description"
multiple="true"
min="1"
max="10"
>
<form>
<field
name="example_text"
type="text"
label="Example Text"
/>
<field
name="example_textarea"
type="textarea"
label="Example Textarea"
cols="40"
rows="8"
/>
</form>
</field>
Atribut bidang:
- type (wajib) haruslah subform.
- name (wajib) adalah sebuah nama unik untuk bidangnya.
- label (wajib) (dapat diterjemahkan) adalah judul bidangnya yang deskriptif.
- description (opsional) (dapat diterjemahkan) adalah teks yang akan ditampilkan sebagai tooltip ketika pengguna mengarahkan kursor ke atas kotak dropdown.
- required (opsional) Bidang harus diisi sebelum form dikirimkan.
- message (opsional) adalah pesan galat yang akan ditampilkan menggantikan pesan standar.
- default (opsional) adalah nilai standar, merupakan string JSON.
- formsource (harus ada) adalah sumber dari form tersebut. Merupakan suatu jalur relatif ke berkas XML-nya (relatif ke berkas root pemasangan Joomla anda) atau sebuah form yang valid yang dapat ditemukan oleh JForm::getInstance().
- multiple (opsional) apakah bidang subform tersebut bisa diulang atau tidak.
- min (opsional) jumlah minimal pengulangan untuk mode multiple. Standar: 0.
- max (opsional) jumlah maksimal pengulangan untuk mode multiple. Standar: 1000.
- groupByFieldset (opsional) apakah bidang subform tersebut dikelompokkan berdasarkan fieldset-nya (true atau false). Standar: false.
- buttons (opsional) tombol mana yang akan ditampilkan dalam mode multiple. Standar: add,remove,move.
- layout (opsional) adalah nama tata letak yang digunakan saat menampilkan bidang subform.
- validate (optional) should be set to SubForm (note that this is case-sensitive!) to ensure that fields in the subform are individually validated. Default: Fields in the subform are not validated, even if validation rules are specified.
Tata letak yang tersedia:
- joomla.form.field.subform.default me-render subform di dalam kontainer div, tanpa dukungan pengulangan. Standar untuk mode tunggal.
- joomla.form.field.subform.repeatable me-render subform di dalam kontainer div, yang digunakan untuk mode multiple. Mendukung groupByFieldset.
- joomla.form.field.subform.repeatable-table me-render subform sebagai sebuah tabel, yang digunakan untuk mode multiple. Mendukung groupByFieldset. Standarnya masing-masing bidang di-render sebagai kolom tabel, tapi jika groupByFieldset=true maka masing-masingnya akan di-render sebagai kolom tabel.
Perhatian
Jika bidang subform anda memiliki logika JavaScript maka kemungkinan ia tidak akan bekerja di mode multiple, karena ia tidak melihat bidangnya ditambahkan oleh bidang subform secara dinamis. Jika hal ini terjadi maka anda harus menyesuaikan bidang tersebut untuk mendukungnya. Contoh lain yang barangkali bermanfaat:
jQuery(document).ready(function(){
... here the code for setup your field as usual...
jQuery(document).on('subform-row-add', function(event, row){
... here is the code to set up the fields in the new row ...
})
});
Because of this some extra Joomla! fields may not work for now.
Fields Validation and Filters
The subform form field does not provide the Validation and Filters for child fields.
Addition: Since a security fix in Joomla 3.9.7 the filter="example"
attributes in subform child fields are supported and the fields will be validated; but NOT in custom form fields that extend the JFormFieldSubform
class. You have to adapt such custom fields yourself!
Beware!
All extensions that use subform fields MUST add an attribute filter
to their subform child fields of type editor
, textarea
, text
(maybe others, too) since Joomla 3.9.7 like it's common for "normal" JForm fields, if you want to allow HTML input. Otherwise the validation falls back to STRING, which is the common behavior for "normal" JForm fields.
Examples:
filter="safehtml"
filter="JComponentHelper::filterText"
filter="raw" (bad decision in most cases)
Contoh
Masalah
Setelah menambahkan baris baru, select tidak bisa dibuat "chosen".
Solusi
Berikut adalah sebuah contoh bagaimana inisiasi ulang jQuery Chosen pada baris pengulangan yang baru ditambahkan:
jQuery(document).ready(function(){
jQuery(document).on('subform-row-add', function(event, row){
jQuery(row).find('select').chosen();
})
});
Atau sebuah potongan kode PHP yang digunakan di dalam, misalnya di plugin anda pada metode **onBeforeCompileHead** atau di dalam view komponen anda.
$doc = JFactory::getDocument();
$js = '
jQuery(document).on(\'subform-row-add\', function(event, row){
jQuery(row).find(\'select\').chosen();
})
';
$doc->addScriptDeclaration($js);
Dengan begitu maka baris yang baru ditambahkan sekarang bisa dibuat "chosen"
Problem
Subform data not getting stored to database on custom component.
Solution
Add the following line to the beginning of your corresponding table class:
protected $_jsonEncode = array('fieldnamehere');
More information Here.