CakePHP: multiple checkbox with another html attribute

Here is the situation:

i has a stages table having the following structure

  | id | name | editable| ----------------------------------------------------------------------------- | open | Open | 1 | | paysheet_review | Paysheet Submitted (Awaiting Office Approval) | 0 | | to_pay | Paysheet Approved (Awaiting Payment) | 0 | | paid | Paid | 1 | | purgatory | Flip or Refund | 0 | | closed | Deal Closed | 1 | ----------------------------------------------------------------------------- 

I create several checkboxes

In the controller

 <?php $_stages = $this->Stage->find("list");?> 

In view

 <?php echo $this->Form->input("stage", array("multiple" => "checkbox", "options" => $_stages));?> 

which creates the following html

 <div class="control-group"> <label class="control-label" for="DealStage">Stage</label> <input type="hidden" value="" id="DealStage_" name="data[Deal][stage]"> <div class="controls"> <label class="checkbox"><input type="checkbox" id="DealStageOpen" value="open" name="data[Deal][stage][]">Open</label> <label class="checkbox"><input type="checkbox" id="DealStagePaysheetReview" value="paysheet_review" name="data[Deal][stage][]">Paysheet Submitted (Awaiting Office Approval)</label> <label class="checkbox"><input type="checkbox" id="DealStageToPay" value="to_pay" name="data[Deal][stage][]">Paysheet Approved (Awaiting Payment)</label> <label class="checkbox"><input type="checkbox" id="DealStagesPaid" value="paid" name="data[Deal][stage][]">Paid</label> <label class="checkbox"><input type="checkbox" id="DealStagePurgatory" value="purgatory" name="data[Deal][stage][]">Flip or Refund</label> <label class="checkbox"><input type="checkbox" id="DealStageClosed" value="closed" name="data[Deal][stage][]">Deal Closed</label> </div> </div> 

Now I want to pass editable columns from the database as a header for each checkbox. Is there a way to create a multiple checkbox in CakePHP and add a title attribute like for every type of input type. so that I can use jQuery before check OR uncheck only editable steps.

+4
source share
3 answers

Create separate checkboxes

There is no means to give individual attributes to a flag if they are generated by a single call, but are easily achievable by generating each flag separately. To do this, make sure that the stage data is passed to the view:

 // in Controller action $stages = $this->Stage->find("all"); $this->set('stages', $stages); 

And in the view, we simply loop over the $stages variable to generate the flags:

 // in view file foreach ($stages as $stage) { $id = $stage['Stage']['id']; $name = $stage['Stage']['name']; $editable = $stage['Stage']['editable']; echo $this->Form->input( 'Deal.stage.' . $id, ['value' => $id, 'label' => $name, 'type' => 'checkbox', 'data-editable' => $editable] ); } 

I donโ€™t quite understand what you want to do with this editable property, but to access it from js it would be:

 // in js file $('input[type=checkbox]'].each(function() { var editable = $(this).data('editable'); ... }); 
+3
source

Unfortunately, I cannot find a way to add attributes to $options as you want. What I usually do is simply perform this role manually, forget FormHelper and write the old html.

If this is not an option for you, and you want to continue to use Formhelper for this, I can suggest a js approach.

First you need to change find('list') to find('all') to get an editable attribute.

 <?php $_stages = $this->Stage->find("all");?> 

and pass it on to the view. Now in the view you need to change the array so that it looks like old

 <?php $stageHelper = array(); foreach($_stages as $stage) $stageHelper[$stage['Stage']['id']] = $stage['Stage']['name']; echo $this->Form->input("stage", array("multiple" => "checkbox", "options" => stageHelper)); ?> 

So far, you have the same thing as now. This is where js comes in. In the view below, preferably add something like

 <script type="text/javascript"> $(function() { var stages = <?=json_encode($_stages);?>; $('name="data[Deal][stage][]"').each(function() { var currentElement = $(this); var value = currentElement.val(); //compare value of checkbox with json variable for(var i in stages) if(stages[i].Stage.value == value) { //add title attribute to the element if we have a match currentElement.attr('title', stages[i].Stage.editable); } }); }); </script> 

Something along these lines should be done.

Personally, I find it less difficult to do this with html only. But there is an alternative approach to do this if you want it.

+1
source

This type of solution you want does not exist in CakePHP right now. But you could create a new Form Helper, I mean the Custom method in Helper, to override this method and create a new attribute in the helper method, for example:

 <?php echo $this->MyForm->input("stage", array("multiple" => "checkbox", "options" => $_stages, 'editable' => $editables));?> 

Why you can get editable parameters using another find('list', array('fields' => array('id', 'editable') ));

+1
source

Source: https://habr.com/ru/post/1492318/


All Articles