mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Made the form enctype configurable via a method Form->setEncType().
API CHANGE: Deprecated Form->FormEncType() in favour of Form->getEncType(). MINOR: Added enctypes as constants to the Form class.
This commit is contained in:
parent
6aa90e6288
commit
3478e4f9e6
@ -44,6 +44,9 @@
|
||||
*/
|
||||
class Form extends RequestHandler {
|
||||
|
||||
const ENC_TYPE_URLENCODED = 'application/x-www-form-urlencoded';
|
||||
const ENC_TYPE_MULTIPART = 'multipart/form-data';
|
||||
|
||||
/**
|
||||
* @var boolean $includeFormTag Accessed by Form.ss; modified by {@link formHtmlContent()}.
|
||||
* A performance enhancement over the generate-the-form-tag-and-then-remove-it code that was there previously
|
||||
@ -137,6 +140,11 @@ class Form extends RequestHandler {
|
||||
*/
|
||||
protected $extraClasses = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $encType;
|
||||
|
||||
/**
|
||||
* Create a new form, with the given fields an action buttons.
|
||||
*
|
||||
@ -600,7 +608,7 @@ class Form extends RequestHandler {
|
||||
$attributes['id'] = $this->FormName();
|
||||
$attributes['action'] = $this->FormAction();
|
||||
$attributes['method'] = $this->FormMethod();
|
||||
$attributes['enctype'] = $this->FormEncType();
|
||||
$attributes['enctype'] = $this->getEncType();
|
||||
if($this->target) $attributes['target'] = $this->target;
|
||||
if($this->extraClass()) $attributes['class'] = $this->extraClass();
|
||||
if($this->validator && $this->validator->getErrors()) {
|
||||
@ -658,19 +666,42 @@ class Form extends RequestHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the encoding type of the form.
|
||||
* This will be either "multipart/form-data"" if there are any {@link FileField} instances,
|
||||
* otherwise "application/x-www-form-urlencoded"
|
||||
* Returns the encoding type for the form.
|
||||
*
|
||||
* @return string The encoding mime type
|
||||
* By default this will be URL encoded, unless there is a file field present
|
||||
* in which case multipart is used. You can also set the enc type using
|
||||
* {@link setEncType}.
|
||||
*/
|
||||
function FormEncType() {
|
||||
if(is_array($this->fields->dataFields())){
|
||||
foreach($this->fields->dataFields() as $field) {
|
||||
if(is_a($field, "FileField")) return "multipart/form-data";
|
||||
public function getEncType() {
|
||||
if ($this->encType) {
|
||||
return $this->encType;
|
||||
}
|
||||
|
||||
if ($fields = $this->fields->dataFields()) {
|
||||
foreach ($fields as $field) {
|
||||
if ($field instanceof FileField) return self::ENC_TYPE_MULTIPART;
|
||||
}
|
||||
}
|
||||
return "application/x-www-form-urlencoded";
|
||||
|
||||
return self::ENC_TYPE_URLENCODED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the form encoding type. The most common encoding types are defined
|
||||
* in {@link ENC_TYPE_URLENCODED} and {@link ENC_TYPE_MULTIPART}.
|
||||
*
|
||||
* @param string $enctype
|
||||
*/
|
||||
public function setEncType($encType) {
|
||||
$this->encType = $encType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 3.0 Please use {@link getEncType}.
|
||||
*/
|
||||
public function FormEncType() {
|
||||
Deprecation::notice('3.0', 'Please use Form->getEncType() instead.');
|
||||
return $this->getEncType();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -325,6 +325,21 @@ class FormTest extends FunctionalTest {
|
||||
SecurityToken::disable(); // restore original
|
||||
}
|
||||
|
||||
public function testEncType() {
|
||||
$form = $this->getStubForm();
|
||||
$this->assertEquals('application/x-www-form-urlencoded', $form->getEncType());
|
||||
|
||||
$form->setEncType(Form::ENC_TYPE_MULTIPART);
|
||||
$this->assertEquals('multipart/form-data', $form->getEncType());
|
||||
|
||||
$form = $this->getStubForm();
|
||||
$form->Fields()->push(new FileField(null));
|
||||
$this->assertEquals('multipart/form-data', $form->getEncType());
|
||||
|
||||
$form->setEncType(Form::ENC_TYPE_URLENCODED);
|
||||
$this->assertEquals('application/x-www-form-urlencoded', $form->getEncType());
|
||||
}
|
||||
|
||||
protected function getStubForm() {
|
||||
return new Form(
|
||||
new Controller(),
|
||||
|
Loading…
Reference in New Issue
Block a user