我遇到令牌和文件字段表单的问题.
表单的验证如下:
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'fields' => array(
'file' => new File(
array(
'maxSize' => '2M',
'mimeTypes' => array(
'application/pdf', 'application/x-pdf',
'image/png', 'image/jpg', 'image/jpeg', 'image/gif',
),
)
),
)
));
return array(
'validation_constraint' => $collectionConstraint
}
Run Code Online (Sandbox Code Playgroud)
当我上传一个无效的大小文件(~5MB)时,我得到了这个错误,这是我希望的:
The file is too large. Allowed maximum size is 2M bytes
Run Code Online (Sandbox Code Playgroud)
但是当我上传一个太大的文件(~30MB)时,错误会发生变化:
The CSRF token is invalid. Please try to resubmit the form
The uploaded file was too large. Please try to upload a smaller file
Run Code Online (Sandbox Code Playgroud)
问题是错误令牌.我的格式是{{form_rest(form)}}代码.我认为错误更改是因为: …
我有这个例子的Symfony2文档的基本表单,模板和控制器操作.
每当我尝试在控制器操作中获取表单的参数时,我必须使用此:
$parameters = $request->request->all();
$name = $parameters["form"]["name"];
Run Code Online (Sandbox Code Playgroud)
但是,在文档中使用此:
$name = $request->request->get('name');
Run Code Online (Sandbox Code Playgroud)
但这对我来说是错误的,在这种情况下$ name为null并且Object请求(ParameterBag)包含:
object(Symfony\Component\HttpFoundation\ParameterBag)#8 (1) {
["parameters":protected]=>
array(1) {
["form"]=>
array(1) {
["name"]=>
string(4) "test"
}
}
}
Run Code Online (Sandbox Code Playgroud)