Browse Source

Updated file() to use new widget.

ADmad 12 years ago
parent
commit
e57215676a

+ 7 - 5
src/View/Helper/FormHelper.php

@@ -171,6 +171,7 @@ class FormHelper extends Helper {
 	protected $_defaultTemplates = [
 		'button' => '<button{{attrs}}>{{text}}</button>',
 		'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
+		'file' => '<input type="file" name="{{name}}"{{attrs}}>',
 		'formstart' => '<form{{attrs}}>',
 		'formend' => '</form>',
 		'hiddenblock' => '<div style="display:none;">{{content}}</div>',
@@ -492,7 +493,7 @@ class FormHelper extends Helper {
 		}
 		$locked = array();
 		$unlockedFields = $this->_unlockedFields;
-		
+
 		foreach ($fields as $key => $value) {
 			if (!is_int($key)) {
 				$locked[$key] = $value;
@@ -1522,14 +1523,15 @@ class FormHelper extends Helper {
 		$options['secure'] = static::SECURE_SKIP;
 
 		$options = $this->_initInputField($fieldName, $options);
-		$field = $this->entity();
 
 		foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
-			$this->_secure($secure, array_merge($field, array($suffix)));
+			$this->_secure(
+				$secure,
+				$this->_secureFieldName(['name' => $options['name'] . '[' . $suffix . ']'])
+			);
 		}
 
-		$exclude = array('name' => null, 'value' => null);
-		return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude));
+		return $this->widget('file', $options);
 	}
 
 /**

+ 2 - 1
src/View/Widget/File.php

@@ -54,7 +54,8 @@ class File implements WidgetInterface {
 			'escape' => true,
 		];
 		unset($data['val']);
-		return $this->_templates->format('fileinput', [
+
+		return $this->_templates->format('file', [
 			'name' => $data['name'],
 			'attrs' => $this->_templates->formatAttributes(
 				$data, ['name', 'val']

+ 14 - 20
tests/TestCase/View/Helper/FormHelperTest.php

@@ -1886,14 +1886,13 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testFormSecuredFileInput() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
-		$this->Form->request->params['_csrfToken'] = 'testKey';
 		$this->assertEquals(array(), $this->Form->fields);
 
 		$this->Form->file('Attachment.file');
 		$expected = array(
-			'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
-			'Attachment.file.error', 'Attachment.file.size'
+			'Attachment.file.name', 'Attachment.file.type',
+			'Attachment.file.tmp_name', 'Attachment.file.error',
+			'Attachment.file.size'
 		);
 		$this->assertEquals($expected, $this->Form->fields);
 	}
@@ -6818,25 +6817,21 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testFileUploadField() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
+		$expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
+
 		$result = $this->Form->file('Model.upload');
-		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
+		$this->assertTags($result, $expected);
 
-		$this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
-		$result = $this->Form->input('Model.upload', array('type' => 'file'));
-		$expected = array(
-			'div' => array('class' => 'input file'),
-			'label' => array('for' => 'ModelUpload'),
-			'Upload',
-			'/label',
-			'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
-			'/div'
-		);
+		$this->Form->request->data['Model']['upload'] = [
+			'name' => '', 'type' => '', 'tmp_name' => '',
+			'error' => 4, 'size' => 0
+		];
+		$result = $this->Form->file('Model.upload');
 		$this->assertTags($result, $expected);
 
 		$this->Form->request->data['Model']['upload'] = 'no data should be set in value';
 		$result = $this->Form->file('Model.upload');
-		$this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
+		$this->assertTags($result, $expected);
 	}
 
 /**
@@ -6845,11 +6840,10 @@ class FormHelperTest extends TestCase {
  * @return void
  */
 	public function testFileUploadOnOtherModel() {
-		$this->markTestIncomplete('Need to revisit once models work again.');
-		$this->Form->create('ValidateUser', array('type' => 'file'));
+		$this->Form->create($this->article, array('type' => 'file'));
 		$result = $this->Form->file('ValidateProfile.city');
 		$expected = array(
-			'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
+			'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
 		);
 		$this->assertTags($result, $expected);
 	}

+ 1 - 1
tests/TestCase/View/Widget/FileTest.php

@@ -31,7 +31,7 @@ class FileTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		$templates = [
-			'fileinput' => '<input type="file" name="{{name}}"{{attrs}}>',
+			'file' => '<input type="file" name="{{name}}"{{attrs}}>',
 		];
 		$this->templates = new StringTemplate($templates);
 	}