我有一个包含多个输入和一个提交按钮的表单。用户不必填写每个字段。表单中输入的值将提交给数据库,我可以成功输出所有值。问题是我需要根据已提交的输入更改视图。 例如,如果用户仅提交YouTube链接,则仅输出YouTube I帧。
我在视图中的表格:
<?= $this->Form->create($work, ['class'=> 'worksform', 'enctype'=>'multipart/form-data']) ?>
<?= $this->Html->image('upload.png', ['class' => 'uploadform center']); ?>
<div class="worksformname"><?= $this->request->session()->read('Auth.User.first_name')?> <?= $this->request->session()->read('Auth.User.last_name');?></div>
<fieldset>
<div class= "worksformtitle"><?= $this->Form->control('Title');?></div>
<div class= "worksformcourse"><?= $this->Form->control('course_id', ['options' => $courses]);?></div>
<div class= "worksformunit"><?= $this->Form->control('unit_id', ['options' => $units]); ?></div>
<div class= "worksformdesc"><?= $this->Form->control('description');?></div>
<p class="worksformtext">I want to upload a... </p>
<li class="buttonworks">
<button type="button" class="videoworks"id="videob"> Video Project </button>
<button type="button" class="audioworks" id="audiob"> Audio Project</button>
<button type="button" class="pdfworks" id="pdfb"> PDF File </button>
<button type="button" class="imageworks" id="imageb"> Image File </button> </li>
<div class="worksformvideo d-none" id="videof"><?= $this->Form->control('video_url')?> </div>
<div class="worksformvideo d-none" id="audiof"><?= $this->Form->control('sound_url');?> </div>
<div class="worksformvideo d-none"id="pdff"><?= $this->Form->control('pdf', ['type'=> 'file'])?> </div>
<div class="worksformvideo d-none"id="imagef"><?= $this->Form->control('image', ['type'=> 'file'])?> </div>
</fieldset>
<div class="buttonbox">
<?= $this->Form->button(__('Submit')) ?>
</div>
<?= $this->Form->end()?>
我的控制器:
public function add()
{
$work = $this->Works->newEntity();
if ($this->request->is('post')) {
$work = $this->Works->patchEntity($work, $this->request->getData());
$work->user_id = $this->Auth->user('id');
$file =$this->request->data['image'];
$name =$this->request->data['image']['name'];
$work->image_url = $name;
$file =$this->request->data['pdf'];
$name =$this->request->data['pdf']['name'];
$work->pdf_url = $name;
if ($this->Works->save($work)) {
move_uploaded_file($file['tmp_name'], WWW_ROOT . 'files/' . $name);
$this->Flash->success(__('The work has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The work could not be saved. Please, try again.'));
}
$users = $this->Works->Users->find('list', ['limit' => 200]);
$units = $this->Works->Units->find('list', ['limit' => 200]);
$courses = $this->Works->Courses->find('list', ['limit' => 200]);
$this->set(compact('work', 'users', 'units', 'courses'));
}
输出视图:
<div class="worksviewcontainer">
<p class="worksunitcourseview"> <?= $work->course->name?> / <?=
$work->unit->name?></p>
<iframe width="100%" height="166" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/<?= h($work->sound_url)?>&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true"></iframe>
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/<?= h($work->video_url)?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>
<object data="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" type="application/pdf" width="600" height="500">
<embed src="<?= '/~i7437085/project/files/'.$work->pdf_url ?>" width="600px" height="500px" />
<p>This browser does not support PDFs. Please download the PDF to view it:
<a href="<?= '/~i7437085/project/files/'.$work->pdf_url ?>">Download PDF</a>.</p>
</embed>
</object>
<img class="pc" src="<?= '/~i7437085/project/files/'.$work->image_url ?>">
<p class="workstitleview"> <?= h($work->Title)?> </p>
<p class="worksnameview"> Published by <?= $work->user->first_name?> <?=$work->user->last_name?> (<?= $work->course->name?>) on <?= h($work->created) ?></p>
<p class="descheading">Description</p>
<p class="worksdescview"><?= h($work->description); ?></p>
</div>
答案 0 :(得分:2)
如果您的验证允许添加空输入,则表示db中的相关字段应为“空”。这意味着,您可以根据字段值显示您的数据:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
您可以对与可以为空的字段相关的所有标记重复此操作。