Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:17:23+00:00 2026-05-27T10:17:23+00:00

I am attempting to take the data the users submit through my form, and

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:17:24+00:00Added an answer on May 27, 2026 at 10:17 am

    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.

    function divide($number) {
                return $number / 100;
            }
    
            function multiply($number) {
                return $number * 1000000;
            }
    
            function beforeSave() {
                switch($this->data['Subfirmdetail']['calculation']) {
                    case "divide":
                        $this->data['Subfirmdetail']['value'] = $this->divide($this->data['Subfirmdetail']['value']);
                        break;
                    case "multiply":
                        $this->data['Subfirmdetail']['value'] = $this->multiply($this->data['Subfirmdetail']['value']);
                        break;
                    case "none":
                    default:
                        $this->data['Subfirmdetail']['value'] = $this->data['Subfirmdetail']['value'];
                        break;
                }
    
                return true;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Attempting to take the data in my gridview and convert it back into a
I am attempting a very simple form designed to take user input into a
I am attempting to take a list of objects, and turn that list into
I'm attempting to take Excel 2003 and connect it to SQL Server 2000 to
Basically Im attempting to take a php / jquery / css / html web
I'm attempting to take an NSImage and convert it to a string which I
Attempting to use the data series from this example no longer passes the JSONLint
What is the best way to take a data file that contains a header
In my model, the field Length is a TimeSpan with the following Data Annotation:
I am attempting to take the Name and ID fields from each object, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.