0

Newbie question alert...

Ok so currently trying to Convert a user inputted value (a mass) from what ever Measure they wish to use, into Mg and then store it in the database.

Currently I have this:

public function setUnit($unit)
{
    $this->unit = $unit;

    return $unit;
}
public function getUnit()
{
    return $this->unit;
}
public function setSize($size)
{
    $unit = $this->unit;

    $this->size = $size = new Mass($size, $unit);
    $this->size = $size->toUnit('mg');

    return $this;
 }

The "new Mass" function is something created by TriplePoint for converting from measure to measure.

https://github.com/triplepoint/php-units-of-measure/

Now if I explicitly set the $unit variable to 'grams' and 'kg' or what ever I choose It will do the conversion however I don't want to force the user to use just one measure obviously. So I thought creating a new variable $unit and the setters/getters to go with it, the text entered to the unit box would then be accepted in the newMass function. Instead I get this:

 Unknown unit of measure ($unit)

Which I think means it is trying to use '$this->unit' as the variable and not the text in the text field.

Like I said I am really new to this stuff, sorry if I annoy anyone with this question.

Once I have this in place I wish to create an array for the user to choose from I think I can manage this myself though.

EDIT--


public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('prod')
        ->add('product_code')
        ->add('size')
        ->add('cost')
        ->add('discount')
        ->add('foodcat')
        ->add('unit', 'choice', array(
'choices'   => array(
    'g'   => 'G',
    'kg' => 'Kg',
    'mg'   => 'Mg',
    'oz'   => 'Oz',
    'lb'   => 'Lb',
),
'multiple'  => false,
));        
}

This is the code for my form.

So I wish the value of $unit to be what ever is selected from the unit choice array. Which is why I am trying to load it by using:

public function setSize($size)
{
    $unit = $this->unit;

Though I am thinking this is not the correct way to get the set variable from "public function setUnit"

Edit -

If I do :

        $unit = 'g';

    $this->size = $size = new Mass($size, $unit);
    $this->size = $size->toUnit('mg');

    return $this;

It works perfectly fine.

So really all Im asking is.. how do I access the variable set in the setUnit method?

0

1 Answer 1

1

The error message Unknown unit of measure ($unit) is giving you enough information.

In this line: $this->size = $size = new Mass($size, $unit);, the $unit is not recognized.

Sign up to request clarification or add additional context in comments.

1 Comment

The error is coming from this line: $this->size = $size = new Mass($size, $unit); The $unit variable is not being set from the form. The ->toUnit('mg') Works perfectly fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.