GreedSX
GreedSX

Reputation: 15

Q: How to pass an value to controller action with f:form action & textflield

I created an Extension in Typo3 8.7 where I want to book some short floating value. I created a Fluid form which should send the information to the Controller. But it's not working for me. I am pretty sure that I'm just missing some basics there. So how can I send a float value from the textfield to the controller $money variable?

The Fluid form looks something like this

<f:form action="addbalance" >
    <f:form.textfield property="money"/>
    <f:form.submit value="send"/>
</f:form>

That's my Controller

public function addBalanceAction(\Awesome\Creditext\Domain\Model\Member $Member, $money)
{
    $this->addFlashMessage('Updated assets', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::OK);
    $Member->addBooking($money);
    $this->MemberRepository->update($Member);
    $this->redirect('list');
}

the addBooking function

public function addBooking($value)
{
    $this->ownbalance += $value;
}

Upvotes: 1

Views: 4372

Answers (3)

Azich
Azich

Reputation: 101

Like rudy-gnodde stated in his answer, the 'property' attribute can only be used when passing a object to the form. If the action rendering your template with the form passed the 'member' object to the template, you could bind it like this

<f:form action="addBalance" object="{member}">
    <f:form.textfield property="ownbalance" />
</f:form>

But since you are adding that amount to an existing value instead of a simple set, this solution won't quite work out for you. You should provide the 'name' attribute instead of 'property' and don't pass the object.

<f:form action="addBalance">
    <f:form.hidden name="member" value="{member}" />
    <f:form.textfield name="balanceToAdd" />
    <f:form.submit value="send" />
</f:form>

Note this requires the 'member' instance to update passed to the template.

Your Controller should look like this

/**
 * @param \Awesome\Creditext\Domain\Model\Member $member
 * @param float $balanceToAdd
 */
public function addBalanceAction(\Awesome\Creditext\Domain\Model\Member $member, $balanceToAdd){
    // code to add the balance goes here
}

Extbase converts your input values based on the phpdoc annotations so you need to get those right.

Upvotes: 1

Ren&#233; Pflamm
Ren&#233; Pflamm

Reputation: 3354

The "magic" is in your PHP Doc... The Extbase System analyse your php doc and convert the values:

/**
 * @param \Awesome\Creditext\Domain\Model\Member $Member
 * @param float                                  $money
 */
public function addBalanceAction(\Awesome\Creditext\Domain\Model\Member $Member, $money)
{
  ...
}

But don't foget: php use the dot as seperation of decimals. So if you use a comma to seperate, it is recommended to replace the comma by a dot in your initializeAction like this:

public function initializeAddBalanceAction()
{
  if ($this->request->hasArgument('money')) {
    $money = str_replace(',', '.', $this->request->getArgument('money');
    $this->request->setArgument('money', $money);
  }
}

But be aware to do more checks, because the comma is used as thousand seperator and if your frontend user used dot and comma to seperate this can be break the float again.

As Rudy write in his answer, the attribute should name and not property.

Upvotes: 0

Rudy Gnodde
Rudy Gnodde

Reputation: 4575

Use name instead of property, like <f:form.textfield name="money" />. property is used when you add an object to f:form and want the field to show/edit one of its properties. For example:

<f:form action="edit" object="{person}">
    <f:form.textfield property="name" />
</f:form>

This field will show the name property of the given person object.

Upvotes: 1

Related Questions