使用线路时:
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
表单甚至不会提交。就像页面重新加载一样。此代码用于工作。另外,如果我使用 ad_photos。而不是 ad_photos[] 我收到此错误消息:
Cannot get an empty property
如果我只使用不带点或括号的 ad_photos,则此请求-> 数据中仅显示一个文件。我试图调试和 var_dump 数据,如下所示,但它甚至没有使用括号进入代码的那部分,并且只用点抛出错误。
控制器方法:
public function add()
{
$listing = $this->HavesAndWants->newEntity();
$error = '';
if ($this->request->is('post')) {
debug($this->request->data()); exit;
echo "<pre>"; var_dump($this->request->data()); echo "</pre>"; exit;
$listing = $this->HavesAndWants->patchEntity($listing, $this->request->data);
$listing->user_id = $this->Auth->user('id');
$listing->ad_photos = '';
if ( $error ) {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
} else {
if ($this->HavesAndWants->save($listing)) {
$this->Flash->success(__('The listing has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
}
}
}
$this->set(compact('error', 'listing'));
$this->set('_serialize', ['listing']);
}
查看表格:
<?= $this->Form->create($listing, ['type' => 'file']) ?>
<h1><?= __('Add Listing') ?></h1>
<fieldset>
<legend><?= __('Contact Info') ?></legend>
<?= $this->Form->input('contact_name'); ?>
<?= $this->Form->input('contact_email'); ?>
<?= $this->Form->input('contact_phone'); ?>
<?= $this->Form->input('contact_street_address'); ?>
<?= $this->Form->input('contact_city'); ?>
<?= $this->Form->input('contact_state'); ?>
<?= $this->Form->input('contact_zip'); ?>
</fieldset>
<fieldset>
<legend><?= __('Listing Info') ?></legend>
<?= $this->Form->input('ad_title'); ?>
<?= $this->Form->input('ad_street_address'); ?>
<?= $this->Form->input('ad_city'); ?>
<?= $this->Form->input('ad_state'); ?>
<?= $this->Form->input('ad_zip'); ?>
<?= $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); ?>
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>