问题
I have two actions, newAction
and createAction
. Both get the same parameters, which are plain values, not objects.
Their implementations looks similar to this:
/**
* Renders a form with the createAction as action="".
*
* @param string $value1
* @param boolean $value2
*
* @ignorevalidation $value1
* @ignorevalidation $value2
*/
public function newAction($value1 = NULL, $value2 = NULL) {
$this->view->assignMultiple([
'value1' => $value1,
'value2' => $value2,
]);
}
/**
* @param string $value1
* @param boolean $value2
*
* @validate $value1 NotEmpty
* @validate $value1 EmailAddress
* @validate $value1 \Vendor\Extkey\Validator\MyValidator
*
* @validate $value2 Boolean(is=TRUE)
*/
public function createAction($value1, $value2) {
// Do some stuff
}
If the newAction
is executed without parameters, everything works fine, the form is displayed without prefilling the fields.
If the form is submitted with valid values, the createAction
is called with the correct values, and everything works as expected.
If the form is submitted with invalid values, validation fails, as expected. Then the request is forwarded to the newAction again, as it should - but the parameters for the newAction are all NULL
, instead of the invalid values that were entered into the form before.
I've made sure that the parameters are actually submitted (var_dump($_POST)
), and TYPO3 has received them (by printing them in the errorAction
).
Why does this happen, am I doing something wrong? Or is extbase broken in that place?
TYPO3-Version: 6.2.15
回答1:
Ok, I have just found the reason for this: It is intentionally done this way, to avoid passing around values that failed validation. Here is the forge ticket.
In order to get the invalid parameters, one can access original request using $this->request->getOriginalRequest()
. Doing this in my above example could look like this:
/**
* Renders a form with the createAction as action="".
* No more parameters here!
*/
public function newAction() {
$originalRequest = $this->request->getOriginalRequest();
if (NULL !== $originalRequest) {
$this->view->assignMultiple($originalRequest->getArguments());
}
}
/**
* @param string $value1
* @param boolean $value2
*
* @validate $value1 NotEmpty
* @validate $value1 EmailAddress
* @validate $value1 \Vendor\Extkey\Validator\MyValidator
*
* @validate $value2 Boolean(is=TRUE)
*/
public function createAction($value1, $value2) {
// Do some stuff
}
来源:https://stackoverflow.com/questions/32936733/parameters-to-action-lost-when-validation-error-occurs