I am attempting to take the data the users submit through my form, and either do nothing, divide by 100, or multiply by 1,000,000 depending on the field.
I’m having trouble writing a beforeSave() function that can successfully do this. When I disable validation, the data submits, but the math that I intended to occur does not happen.
The reason I’m doing this is because if a user enters 2.16 for 2.16%, we want to store it as .0216. It’s just basic math after the form has been submitted and for the life of me I can’t get it to work.
Any advice would be appreciated. Haven’t found any good tutorials on this topic, so if anyone knows of anything that would be great.
This is the original array of data I get from the submit:
Array
(
[Subfirmdetail] => Array
(
[subfirm_id] => 4
[subfirmdetailtype_id] => 55
[value] => 20
[date] => 2011-12-02
[active] => yes
[modified] => 2011-12-02 19:00:06
[created] => 2011-12-02 19:00:06
)
)
Array
(
[Subfirmdetail] => Array
(
[subfirm_id] => 4
[subfirmdetailtype_id] => 56
[value] => 20
[date] => 2011-12-02
[active] => yes
[modified] => 2011-12-02 19:00:06
[created] => 2011-12-02 19:00:06
)
)
Array
(
[Subfirmdetail] => Array
(
[subfirm_id] => 4
[subfirmdetailtype_id] => 57
[value] => 20
[date] => 2011-12-02
[active] => yes
[modified] => 2011-12-02 19:00:06
[created] => 2011-12-02 19:00:06
)
)
Array
(
[Subfirmdetail] => Array
(
[subfirm_id] => 4
[subfirmdetailtype_id] => 58
[value] => 20
[date] => 2011-12-02
[active] => yes
[modified] => 2011-12-02 19:00:06
[created] => 2011-12-02 19:00:06
)
)
Array
(
[Subfirmdetail] => Array
(
[subfirm_id] => 4
[subfirmdetailtype_id] => 59
[value] => 20
[date] => 2011-12-02
[active] => yes
[modified] => 2011-12-02 19:00:06
[created] => 2011-12-02 19:00:06
)
)
Here is my beforeSave function:
function beforeSave() {
switch($this->data['Subfirmdetailtype']['calculation']) {
case 0:
$this->data['Subfirmdetail']['value'] = $this->data['Subfirmdetail']['value'];
break;
case 1:
$this->data['Subfirmdetail']['value'] = $this->divide($this->data['Subfirmdetail']['value']);
break;
case 2:
$this->data['Subfirmdetail']['value'] = $this->multiply($this->data['Subfirmdetail']['value']);
break;
}
pr($this->data);
return true;
}
And finally my add.ctp
<div class="subfirmdetails form">
<?php echo $this->Form->create('Subfirmdetail', array('name' => 'subfirmdetail'));?>
<fieldset>
<legend><?php __('Add Subfirmdetails'); ?></legend>
<?php if(!empty($this->params['named'])) { ?>
<div class="span-7" style="text-align: left;">
<?php
//asset allocation entry section
echo "<b><u><h3>Asset Allocation</h3></u></b>\n";
$num = 0;
foreach($allocations as $allocation) {
echo "<b>" . $allocation['Subfirmdetailtype']['description'] . "</b><br>\n";
echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $allocation['Subfirmdetailtype']['id'])) . "\n";
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('class' => 'quantity', 'label' => '', 'onblur' => 'calculateTotal();', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
$num++;
}
echo "<b>Total * (must equal 100%)</b>" . $this->Form->input('total', array('label' => '', 'id' => 'total_quantity', 'value' => 0, 'disabled' => 'disabled'));
?>
</div>
<div class="span-8">
<?php
//asset allocation entry section
echo "<b><u><h3>General Statistics</h3></u></b>\n";
//pr($generals);
foreach($generals as $general) {
echo "<b>" . $general['Subfirmdetailtype']['description'] . "</b><br>\n";
echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $general['Subfirmdetailtype']['id'])) . "\n";
if($general['Datatype']['name'] == "select") {
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'options' => $options[$general['Subfirmdetailtype']['id']]));
} else {
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => ''));
}
echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
$num++;
}
?>
</div>
<div class="span-8 last">
<?php
//asset allocation entry section
echo "<b><u><h3>Returns</h3></u></b>\n";
foreach($returns as $return) {
echo "<b>" . $return['Subfirmdetailtype']['description'] . "</b><br>\n";
echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $return['Subfirmdetailtype']['id'])) . "\n";
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
$num++;
}
?>
</div>
<div class="span-8" style="margin-top: 25px;">
<?php
//asset allocation entry section
echo "<b><u><h3>Volatility</h3></u></b>\n";
foreach($volatilities as $volatility) {
echo "<b>" . $volatility['Subfirmdetailtype']['description'] . "</b><br>\n";
echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $volatility['Subfirmdetailtype']['id'])) . "\n";
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
$num++;
}
?>
</div>
<div class="span-7 last" style="margin-top: 25px;">
<?php
//asset allocation entry section
echo "<b><u><h3>Upside/Downside</h3></u></b>\n";
foreach($upsidedownsides as $upsidedownside) {
echo "<b>" . $upsidedownside['Subfirmdetailtype']['description'] . "</b><br>\n";
echo $form->hidden('Subfirmdetail.' . $num . '.subfirm_id', array('value' => $this->params['named']['subfirm_id'])) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.subfirmdetailtype_id', array('value' => $upsidedownside['Subfirmdetailtype']['id'])) . "\n";
echo $this->Form->input('Subfirmdetail.' . $num . '.value', array('label' => '', 'onkeydown' => 'ForceNumericInput(this, true, true)')) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.date', array('value' => date("Y-m-d"))) . "\n";
echo $this->Form->hidden('Subfirmdetail.' . $num . '.active', array('value' => 'yes')) . "\n";
echo $this->Form->hidden('Subfirmdetailtype.' . $num . '.calculation') . "\n";
$num++;
}
?>
</div>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<?php } else {
echo "Sorry, you have not come from an appropriate subfirm page to add these details. Please visit a subfirm page before adding data.";
} //pr($generals); ?>
</div>
<script language="javascript">
//function to convert percentages entered in by users (e.g. 20% to 0.20)
function calculateTotal() {
var total = 0;
<?php $num = 0; ?>
$(".quantity").each(function() {
if (!isNaN(this.value) && this.value.length != 0) {
total += parseFloat(this.value);
}
});
total = total;
if (total != 100) {
total = total + "% - Sorry, total must equal 100%";
$("#total_quantity").val(total);
} else {
total = total + "%";
$("#total_quantity").val(total);
}
}
//function to force returns inputs to be numbers between 1 - 100
</script>
This is what I ended up using. Had to make some changes to my model as well pulling the data into the appropriate sections. If anyone ever needs to do something similar… I’m here to help.