I want to show a form with CakePHP where users can select an employee. Cake shows the inputfield as "select" which is good. Unfortunately the select input field only shows the id of the employee rather than the prename and lastname.
Model:
Reminder:
worker_id (reference to Worker model)
Worker:
id
prename
lastname
Here is the PHP part:
echo $this->Form->create('Reminder',
array('action' => 'add',
'inputDefaults' => array(
'label' => false,
'div' => false
)
)
);
echo $this->Form->input('worker_id', array('empty' => 'Choose an employee'));
echo $this->Form->button('Save', array('type' => 'submit', 'class' => 'btn btn-green'));
I did not find a way to show the prename and lastname of class Worker but keep the id as option value in the select field.
How can this be done?
You have need to create $your_listing as below
$your_listing = array('2'=>'John Fleming','3'=>'Justin Bond');
and used this array in your form helper as below
echo $this->Form->input('worker_id', array('empty' => 'Choose an employee','options'=>$your_listing));
So in above example, select box become
<select name="worker_id">
<option id="">Choose an employee</option>
<option id="2">John Fleming</option>
<option id="3">Justin Bond</option>
</select>
You can use an array to specify the options of the select input.
$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
)
echo $this->Form->input('worker_id', $options, array('empty' => 'Choose an employee'));
In you case your labels should be Worker.id => Worker.prename + Worker.lastname
Related
I have a dropDownList like follwing:
<?php echo TbHtml::dropdownList('cls', $sel, CHtml::listData($classes, 'type_class', 'name_class')); ?>
That generates a select option like that:
<select id="cls" name="cls">
<option value="1">Main Class</option>
<option value="2">Other Class</option>
</select>
When I click on a button, I need to get the value, in my example 1 or 2.
The button is built like following:
echo $this->widget('booster.widgets.TbButton', array(
'buttonType' => 'link',
'context' => 'warning',
'icon' => 'fa fa-file-pdf-o',
'label' => 'Submit',
'size' => 'small',
'url' => array(
'//wseder/excel/wws',
'code_from_select' => $VALUE_THAT_I_NEED // here i need the value
),
'htmlOptions' => array(
'class' => 'pull-rigth btn-nxt-prv',
'style' => 'margin-left:5px'
)
), true);
How can I access to the value selected from the select?
How I understand you creating select on html page and button on html page. When you click button you need get select val and put it in button. If so, you can not do it, because php do not have access to select val on html page. You should use javascript (jquery) to read select value and insert it where you need.
I want radio button within table, without label and one radio button for one table row, while all grouped in one single group, so user can select single table row by selecting one radio button from the group ...
In pure html it looks like this:
<div class="radio">
<label>
<input type="radio" name="memberPaymentsRadio" value="mp<?php echo $key;?>" aria-label="...">
</label>
</div>
What would be the code in cakephp to get the same html code?
I tried like this:
foreach($payments as $key => $payment){
...
echo $form->input('memberPaymentsRadio',
array(
'div' => array('class' => 'radio'),
'label' => false,
'type' => 'radio',
'aria-label' => '...',
'options' => array('mp'.$key),
'before' => '<label>',
'after' => '</label>'
)
);
}
but I'm getting radio buttons, one per table row, and all within specific table column, but with labels 'mp0', 'mp1', which is not what I was looking for...
You can use like this
$this->Form->radio('memberPaymentsRadio', array('mp'.$key => ""),array('class' => 'radio', 'before' => '<label>','after' => '</label>','label' => false));
When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)
I am a new cake php developer.I am trying to insert multiple record but i cant.My table Structure is given below:
Table Name :
agents
==============================================
id | Contact | Name | email_add | Address
==============================================
Controller Code :
public function send_money()
{
$this->layout='agent';
$this->Agent->create();
$this->Agent->set($this->data);
if(empty($this->data) == false)
{
if($this->Agent->save($this->data))
{
$this->Session->setFlash('Information Added Successfully.');
$this->redirect('send_money');
}
}
else
{
$this->set('errors', $this->Agent->invalidFields());
}
}
Model Code Is :
<?php
App::uses('AppModel', 'Model');
/**
* Admin Login Model
*
*/
class Agent extends AppModel
{
public $name='Agent';
public $usetables='agents';
public $validate = array(
'contact' => array(
'contact_not_empty' => array(
'rule' => 'notEmpty',
'message' => 'Please Give Contact No',
'last' => true
),
),
'name' =>array(
'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
'allowEmpty' => false,
'message' => 'Please Enter Name.'
),
'email_add' => array(
'email_add' => array(
'rule' => 'email',
'allowEmpty' => true,
'message' => 'Please Enter Valid Email',
'last' => true
)),
);
}
?>
Its not possible to insert records with this code.What should I do?
Let me explain everything.
First of all your html form must be looks like following.
<?php echo $this->Form->create('User'); ?>
<tr>
<td>
<?php
echo $this->Form->input('User.0.address',array
(
'div' => null,
'label' => false,
'class' => 'span required'
));
?>
<?php
echo $this->Form->input('User.1.address',array
(
'div' => null,
'label' => false,
'class' => 'span required'
));
?>
<?php
echo $this->Form->input('User.2.address',array
(
'div' => null,
'label' => false,
'class' => 'span required'
));
?>
</td>
<td>
<input type="submit" value="Add" >
</td>
</tr>
<?php echo $this->Form->end(); ?>
As you can see to save many record at same time using cakephp you must define it as above it will parse input fields as array this is cakephp convention.
I mean User.0.address for first array element
User.1.address for second array element
User.2.address for third array element and so on.
Now.
In Controller file.
<?php
function add()
{
$this->User->saveAll($this->data['User']);
}
And yes here you are done saving multiple record at same time.
I just gave you how cakephp works all you need to do is set above hint as per your need.
Best of luck...
Cheers...
Feel Free to ask... :)
Try this saveall() in your query except save, hope this helps
if($this->Agent->saveall($this->data))
Let me know if it work.
I think this
php echo $this->Form->create('Agents', array('action' => 'send_money'));?>
should be replaced with
php echo $this->Form->create('Agent', array('action' => 'send_money'));?>
and use saveall() in place of save.
I create 2 arrays from a model, 1 being already selected values from the user, and the 2nd available values which the user can then also select. This is for an edit page. I want to populate a multi-select input box with the values of both models, but want the already chosen values (1st array) highlighted. It creates the models fine, and using array_merge() I merge both the arrays as the options, but the selected does not highlight the correct fields. Any tips?
// Controller:
$ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
$ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));
// In the view:
echo $this->Form->input('SurveyIvrNumbers.id',array(
'empty' => '-- Select IVR Number(s) --',
'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
'selected' => $ivrNumbersSelected,
'class' => 'textbox',
'multiple' => true,
'div' => array(
'class' => 'field'
),
'label' => array(
'class' => 'label-tooltip',
'title' => '', //tool tips
'text' => 'IVR Numbers: (you can select multiple numbers)'
),
'after' => '<p class="field-help"></p>'
));
If you set $this->request->data to the record you are currently editing CakePHP will automatically populate this data for you!
// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id);
// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all'));
// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array(
'empty' => '-- Select IVR Number(s) --', // plus other options
);