2015-03-13 23 views
1

在CakePhp 3中我有一個參數列表,我想在複選框中插入。我有這樣的代碼用cakephp 3.x設置class或div複選框的形式

<?php 
echo $this->Form->input('regions', [    
    'options' => $regions, 
    'value' => Hash::extract($ad->toArray(), 'regions.{n}.id'), 
    'class' => 'col-xs-4', 
    'type'  => 'select', 
    'multiple' => 'checkbox', 
    'label' => false, 
    'error' => [ 
     'attributes' => ['class' => 'col-xs-12 error-message inline'] 
    ],   
]); 
?> 

類COL-XS-4不會出現。我也插入

'div' => 'col-xs-4' 

而不是類。

如何在每個複選框元素中插入類或div?

這是生成的html:

<div class="input select"> 
    <input type="hidden" name="regions" value=""> 
    <div class="checkbox"><label for="regions-1"><input type="checkbox" name="regions[]" value="1" id="regions-1">Abruzzo</label></div> 
    <div class="checkbox"><label for="regions-2"><input type="checkbox" name="regions[]" value="2" id="regions-2">Basilicata</label></div> 
    <div class="checkbox"><label for="regions-3"><input type="checkbox" name="regions[]" value="3" id="regions-3">Calabria</label></div> 
    <div class="checkbox"><label for="regions-4"><input type="checkbox" name="regions[]" value="4" id="regions-4">Campania</label></div> 
    <div class="checkbox"><label for="regions-5"><input type="checkbox" name="regions[]" value="5" id="regions-5">Emilia Romagna</label></div> 
    <div class="checkbox"><label for="regions-6"><input type="checkbox" name="regions[]" value="6" id="regions-6">Friuli Venezia Giulia</label></div> 
    <div class="checkbox"><label for="regions-7"><input type="checkbox" name="regions[]" value="7" id="regions-7">Lazio</label></div> 
    <div class="checkbox"><label for="regions-8"><input type="checkbox" name="regions[]" value="8" id="regions-8">Liguria</label></div> 
    <div class="checkbox"><label for="regions-9"><input type="checkbox" name="regions[]" value="9" id="regions-9">Lombardia</label></div> 
    <div class="checkbox"><label for="regions-10"><input type="checkbox" name="regions[]" value="10" id="regions-10">Marche</label></div> 
    <div class="checkbox"><label for="regions-11"><input type="checkbox" name="regions[]" value="11" id="regions-11">Molise</label></div> 
    <div class="checkbox"><label for="regions-12"><input type="checkbox" name="regions[]" value="12" id="regions-12">Piemonte</label></div> 
    <div class="checkbox"><label for="regions-13"><input type="checkbox" name="regions[]" value="13" id="regions-13">Puglia</label></div> 
    <div class="checkbox"><label for="regions-14"><input type="checkbox" name="regions[]" value="14" id="regions-14">Sardegna</label></div> 
    <div class="checkbox"><label for="regions-15" class="selected"><input type="checkbox" name="regions[]" value="15" checked="checked" id="regions-15">Sicilia</label></div> 
    <div class="checkbox"><label for="regions-16"><input type="checkbox" name="regions[]" value="16" id="regions-16">Toscana</label></div> 
    <div class="checkbox"><label for="regions-17"><input type="checkbox" name="regions[]" value="17" id="regions-17">Trentino Alto Adige</label></div> 
    <div class="checkbox"><label for="regions-18"><input type="checkbox" name="regions[]" value="18" id="regions-18">Umbria</label></div> 
    <div class="checkbox"><label for="regions-19"><input type="checkbox" name="regions[]" value="19" id="regions-19">Valle d&#039;Aosta</label></div> 
    <div class="checkbox"><label for="regions-20"><input type="checkbox" name="regions[]" value="20" id="regions-20">Veneto</label></div> 
</div> 
+0

你應該表現出你正在努力產生精確的HTML。 – AgRizzo 2015-03-13 11:08:18

回答

0

我會嘗試傳遞div選項。

<?php 
echo $this->Form->input('regions', [ 
    'div' => [ 
     'class' => 'checkbox col-xs-4' 
    ] 
]); 
?> 

但是,您可能需要結束更改input field template

+0

沒有變化:( – DarioLap 2015-03-13 11:25:43

5

最後,我決定用:

<?php 
echo $this->Form->input('Regions',    
    [ 
    'templates' => [ 
     'checkboxWrapper' => '<div class="col-xs-4 checkbox">{{label}}</div>', 
    ], 
    'options' => $regions, 
    'value' => Hash::extract($ad->toArray(), 'regions.{n}.id'), 
    'type'  => 'select', 
    'multiple' => 'checkbox', 
    'label' => false, 
    'error' => [ 
     'attributes' => ['class' => 'col-xs-12 error-message inline'] 
    ],   
]); 
?> 

文檔瀏覽:http://book.cakephp.org/3.0/en/views/helpers/form.html#customizing-the-templates-formhelper-uses