bstaub
bstaub

Reputation: 53

TYPO3 Extbase: how to post form data to controller

I can't upload some Extbase form data to a controller. get null value in controller. this is what i did.

ext_localconf.php

        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
        'Webtech.Wttempro2',
        'Jobdeskform',
        [
            'Jobdeskform' => 'createbewerber, formMailBs'
        ],
        // non-cacheable actions
        [
            'Jobdeskform' => 'createbewerber, formMailBs'
        ]
    );

ext_tables.php

        \TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerPlugin(
        'Webtech.Wttempro2',
        'Jobdeskform',
        'Jobdesk Form'
    );

Jobdeskform/CreateBewerber.html

                        <f:form action="formMailBs" controller="Jobdeskform" object="{jobdeskform}" objectName="jobdeskform" method="post">
                        <label>Refno</label><br>
                        <input type="text" property="refno" required/><br>
                        <label>Vorname</label><br>
                        <input type="text" property="vorname" required/><br>
                        <f:form.button>Submit</f:form.button>
                    </f:form>

Model/Jobdeskform.php is that simple. it just use member variables with getter and setters.

/**
 * 
 * Tempro data for jobdeskform
 * 
 */
class Jobdeskform extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
    /**
     * refno
     *
     * @var string
     */
    protected $refno = '';

    /**
     * vorname
     *
     * @var string
     */
    protected $vorname = '';


    /**
     * Returns the refno
     *
     * @return int $refno
     */
    public function getRefno()
    {
        return $this->refno;
    }

    /**
     * Sets the refno
     *
     * @param int $refno
     * @return void
     */
    public function setRefno($refno)
    {
        $this->refno = $refno;
    }

    /**
     * @return string
     */
    public function getVorname()
    {
        return $this->vorname;
    }

    /**
     * @param string $vorname
     * @return void
     */
    public function setVorname($vorname)
    {
        $this->vorname = $vorname;
    }

}

class JobdeskformController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController{

/**
 * action createBewerber
 *
 * @return void
 */
public function createbewerberAction()
{
    $baseuri = $this->request->getRequestUri();
    $parts = parse_url($baseuri);
    parse_str($parts['query'], $query);
    $this->view->assign('refno', htmlspecialchars($query['refno']));
}


/**
 * action formMailBs
 *
 * @param \Webtech\Wttempro2\Domain\Model\Jobdeskform $jobdeskform
 * @return void
 */
public function formMailBsAction(\Webtech\Wttempro2\Domain\Model\Jobdeskform $jobdeskform = NULL)
{

    \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($jobdeskform, 'FormObject:');
    //\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($jobdeskform->refno, 'Refno:');
    //\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($refno, 'Refno:');
    //\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($vorname, 'Vorname:');

    die('DEBUG!!');


}

}

When i debug the form i get null value in variable $jobdeskform. i have entered some data in the controller. why i don't see the entered form data in the controller debug output? maybee i did't see something important. thanks for help.

Upvotes: 2

Views: 5669

Answers (1)

Steffen M&#228;chtel
Steffen M&#228;chtel

Reputation: 1021

No name attribute in input fields:

Your input fields in html have no "name" attribute. They are currently created with pure html, you can create the fields with fluid form textfields:

<f:form action="formMailBs" controller="Jobdeskform" objectName="jobdeskform" method="post">
    <label>Refno</label><br>
    <f:form.textfield property="refno" /><br>
    <label>Vorname</label><br>
    <f:form.textfield property="vorname" /><br>
    <f:form.button>Submit</f:form.button>
</f:form>

Result:

<input type="text" name="tx_example_jobdeskform[jobdeskform][refno]">
<input type="text" name="tx_example_jobdeskform[jobdeskform][vorname]">

Any easy way to debug the post values is to output the whole $_POST array in your controller. With your current input fields, there are no values submited (no name attribute).

/**
 * action formMailBs
 *
 * @param \Webtech\Wttempro2\Domain\Model\Jobdeskform $jobdeskform
 * @return void
 */
public function formMailBsAction(\Webtech\Wttempro2\Domain\Model\Jobdeskform $jobdeskform = NULL)
{
    debug($_POST); 
    die('DEBUG!!');
}

Notice: you can use the shorthand function debug for debug output instead for the long \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump

Mismatch of variable type in model:

In your model the attribute $refno is defined as string (@var string)

   /**
     * refno
     *
     * @var string
     */
    protected $refno = '';

later in your setter and getter methods its defined as integer:

   /**
     * Returns the refno
     *
     * @return int $refno
     */
    public function getRefno()
    {
        return $this->refno;
    }

    /**
     * Sets the refno
     *
     * @param int $refno
     * @return void
     */
    public function setRefno($refno)
    {
        $this->refno = $refno;
    }

Notice: you can find this kind of errors by adding a basic validation error output.

<f:form.validationResults>
  <f:if condition="{validationResults.flattenedErrors}">
    <ul class="errors">
      <f:for each="{validationResults.flattenedErrors}" as="errors" key="propertyPath">
        <li>{propertyPath}
          <ul>
          <f:for each="{errors}" as="error">
            <li>{error.code}: {error}</li>
          </f:for>
          </ul>
        </li>
      </f:for>
    </ul>
  </f:if>
</f:form.validationResults>
<f:form action="formMailBs" controller="Jobdeskform" objectName="jobdeskform" method="post">
    <label>Refno</label><br>
    <f:form.textfield property="refno" /><br>
    <label>Vorname</label><br>
    <f:form.textfield property="vorname" /><br>
    <f:form.button>Submit</f:form.button>
</f:form>

Upvotes: 2

Related Questions