From 6aa90e6288d19982943e8c83b353a48df59573ef Mon Sep 17 00:00:00 2001 From: ajshort Date: Mon, 26 Dec 2011 18:31:32 +1100 Subject: [PATCH 1/3] MINOR: Replaced is_a with instanceof to fix bugs with the changed is_a behaviour in PHP 5.3.7. --- forms/CheckboxSetField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forms/CheckboxSetField.php b/forms/CheckboxSetField.php index 86eeaceb6..db389695d 100644 --- a/forms/CheckboxSetField.php +++ b/forms/CheckboxSetField.php @@ -110,7 +110,7 @@ class CheckboxSetField extends OptionsetField { } if($source) foreach($source as $index => $item) { - if(is_a($item, 'DataObject')) { + if($item instanceof DataObject) { $key = $item->ID; $value = $item->Title; } else { From 3478e4f9e65323e2a151137aac52495cd86098d9 Mon Sep 17 00:00:00 2001 From: ajshort Date: Mon, 26 Dec 2011 18:36:24 +1100 Subject: [PATCH 2/3] 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. --- forms/Form.php | 59 ++++++++++++++++++++++++++++++---------- tests/forms/FormTest.php | 17 +++++++++++- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/forms/Form.php b/forms/Form.php index 791e53e19..32913ce8c 100644 --- a/forms/Form.php +++ b/forms/Form.php @@ -43,7 +43,10 @@ * @subpackage core */ 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()) { @@ -656,23 +664,46 @@ class Form extends RequestHandler { if($this->template) return $this->template; else return $this->class; } - + /** - * 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" - * - * @return string The encoding mime type + * Returns the encoding type for the form. + * + * 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(); + } + /** * Returns the real HTTP method for the form: * GET, POST, PUT, DELETE or HEAD. diff --git a/tests/forms/FormTest.php b/tests/forms/FormTest.php index 06463cc62..8d00b9e82 100644 --- a/tests/forms/FormTest.php +++ b/tests/forms/FormTest.php @@ -324,7 +324,22 @@ 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(), From d692c22ee459a2d1ebd559f252fe31f0741267de Mon Sep 17 00:00:00 2001 From: ajshort Date: Mon, 26 Dec 2011 19:08:08 +1100 Subject: [PATCH 3/3] MINOR: Fixed FileField->Value() erroring when files value not set. --- forms/FileField.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/forms/FileField.php b/forms/FileField.php index 414ed7bb7..7b45a90d1 100644 --- a/forms/FileField.php +++ b/forms/FileField.php @@ -156,11 +156,11 @@ class FileField extends FormField { $record->{$this->name . 'ID'} = $file->ID; } } - + public function Value() { - return $_FILES[$this->getName()]; + return isset($_FILES[$this->getName()]) ? $_FILES[$this->getName()] : null; } - + /** * Get custom validator for this field *