简体   繁体   中英

Error in Form Objects in FLUID TYPO3

I created a custom model called Play and it has properties Title and Body with the getters and setters.

In one of my controller Master , I have the new and create actions

public function newAction(\TYPO3\Playground\Domain\Model\Play $newPlay = NULL) {
    $this->view->assign('newPlay', $newPlay);
}

My View looks like this:

<table>
        <f:form action="create" name="newPlay" object="{newPlay}">
            <th>Title:</th>
            <td>
                <f:form.textfield property="title"/>
            </td>
            <th>Body:</th>
            <td>
                <f:form.textarea property="body"/>
            </td>
            </th>
            <tr>
                <td>
                    <f:form.submit value="Create"/>
                </td>
            </tr>
        </f:form>
    </table>

But in my create function in controller I get this error Required argument "newPlay" is not set.

public function createAction(\TYPO3\Playground\Domain\Model\Play $newPlay) {
    echo $newPlay->getBody();
    echo $newPlay->getTitle();
}

Am I missing something here?

Update

After matching the name to the object and removing the brackets of property for fields, this is the error:

Exception while property mapping at property path "":No converter found which can be used to convert from "array" to "TYPO3\\Playground\\Domain\\Model\\Play".

My Solution to it

I'm very sure there is a neat way than this

public function createAction() {

    $newPlayArray=$this->request->getArgument('newPlay');
    $newPlay = json_decode(json_encode($newPlayArray), FALSE);
    echo $newPlay->title;

}

The name of the form must match the object

<f:form action="create" name="newPlay" object="{newPlay}">

It is used to set the name of the fields.

Update

Have you set PHPDoc comments? These are required and very importan for Extbase development. Example:

/**
 * @param \TYPO3\Playground\Domain\Model\Play $newPlay
 * @dontvalidate $newPlay
 */
public function newAction(\TYPO3\Playground\Domain\Model\Play $newPlay = NULL) {
    $this->view->assign('newPlay', $newPlay);
}

Try to always pass an object to the view. In your case you most likely pass NULL:

/**
 * @param \TYPO3\Playground\Domain\Model\Play $newPlay
 * @dontvalidate $newPlay
 * @return void
 */
public function newAction(\TYPO3\Playground\Domain\Model\Play $newPlay = NULL) {
    if (is_null($newPlay)) {
        $newPlay = $this->objectManager->create('TYPO3\Playground\Domain\Model\Play');
    }
    $this->view->assign('newPlay', $newPlay);
}

Leave your form as it is and just take the object in your createAction:

/**
 * @param \TYPO3\Playground\Domain\Model\Play $newPlay
 * @return void
 */
public function createAction(\TYPO3\Playground\Domain\Model\Play $newPlay) {
    $this->playRepository->add($newPlay);
    $this->redirect('anyAction');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM