我目前正在处理包含许多标签的表单。当出现错误时,我想重新填充表单。它适用于我的表单中的第一个选项卡,但对于其他选项卡,它不会重新填充。关于什么是错的任何想法?
tabNum变量是为了在用户提交表单时将页面重定向回正确的选项卡。
function addResearchMicroFundView()
{
if($this->session->userdata('logged_in'))
{
//Get information from session
$session_data = $this->session->userdata('logged_in');
$user = $this->user_model->getUsernameNameLevel($session_data['user_id']);
foreach ($user as $row)
{
}
$data['user_id'] = $session_data['user_id'];
$data['username'] = $row->username;
$data['name'] = $row->name;
$data['level'] = $row->level;
$data['page'] = "addResearchMicroFund";
if (!$this->session->flashdata('tabNum'))
{
$this->session->set_flashdata('tabNum', 1);
}
$this->load->view('addResearchMicroFund_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function addSubmitApplicantDetails()
{
$this->form_validation->set_rules('applicant_name', 'Name of Applicant', 'required');
$this->form_validation->set_rules('academic_position', 'Academic Position', 'required|in_list[A,B1,B2,C,D,E]');
$this->form_validation->set_rules('applicant_faculty', 'Faculty / Centre / Unit', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('tabNum', $this->input->post('tabNum'));
$this->addResearchMicroFundView();
}
else
{
}
}
function addSubmitResearchDetails()
{
$this->form_validation->set_rules('research_title', 'Research Title', 'required|is_natural');
$this->form_validation->set_rules('project_summary', 'Project Summary', 'required');
$this->form_validation->set_rules('item_necessity', 'Necessity of the item for the project', 'required');
$this->form_validation->set_rules('expected_outcome', 'Expected Outcome', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('tabNum', $this->input->post('tabNum'));
$this->addResearchMicroFundView();
}
else
{
}
}
<section class="content-header">
<?php if(validation_errors()):?>
<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><Strong><?php echo validation_errors(); ?></Strong></div>
<?php endif;?>
</section>
<!-- Main content -->
<section class="content">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li <?php if ($this->session->flashdata('tabNum') == 1){echo 'class="active"';}?>><a href="#tab_1" data-toggle="tab">Applicant Details</a></li>
<li <?php if ($this->session->flashdata('tabNum') == 2){echo 'class="active"';}?>><a href="#tab_2" data-toggle="tab">Research Details</a></li>
<!-- <li><a href="#tab_3" data-toggle="tab">Recommendation</a></li> -->
</ul>
<div class="tab-content">
<div <?php if ($this->session->flashdata('tabNum') == 1) echo "class='tab-pane active'"; else echo "class='tab-pane'"; ?> id="tab_1">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Applicant Details</h3>
</div><!-- /.box-header -->
<?php
$attributes = array('role' => 'form', 'data-toggle' => 'validator','id' =>'validateForm');
echo form_open('researchMicroFund/addSubmitApplicantDetails',$attributes)?>
<!-- form start -->
<form role="form">
<input type="hidden" name="tabNum" value="1"/>
<input type="hidden" name="user_id" value="<?php echo $user_id?>"/>
<div class="box-body">
<div class="form-group has-feedback">
<label>Name of Applicant</label>
<input type="text" class="form-control" id="applicant_name" name="applicant_name" value="<?=!form_error('applicant_name')?set_value('applicant_name'):''?>" placeholder="Name of Applicant" required>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Academic Position</label>
<p class="help-block">(A, B1, B2, C, D or E)</p>
<input type="text" class="form-control" id="academic_position" name="academic_position" value="<?=!form_error('academic_position')?set_value('academic_position'):''?>" placeholder="Academic Position" required>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Faculty / Centre / Unit</label>
<input type="text" class="form-control" id="applicant_faculty" name="applicant_faculty" value="<?=!form_error('applicant_faculty')?set_value('applicant_faculty'):''?>" placeholder="Faculty / Centre / Unit" required>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Type of Service</label>
<select class="form-control select2" style="width: 100%;" id="service_type" name="service_type" required onchange="disableContractExpiryDate()">
<option <?php echo set_select('service_type', 'Tenure', TRUE); ?> value="Tenure">Tenure</option>
<option <?php echo set_select('service_type', 'Contract'); ?> value="Contract">Contract</option>
</select>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Contract Expiry Date</label>
<input type="date" class="form-control" id="expiry_date" name="expiry_date" value="<?=!form_error('expiry_date')?set_value('expiry_date'):''?>" placeholder="Contract Expiry Datet" required>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div><!-- /.box -->
</div>
<div <?php if ($this->session->flashdata('tabNum') == 2) echo "class='tab-pane active'"; else echo "class='tab-pane'"; ?> id="tab_2">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Research Details</h3>
</div><!-- /.box-header -->
<?php
$attributes = array('role' => 'form', 'data-toggle' => 'validator','id' =>'validateForm');
echo form_open('researchMicroFund/addSubmitResearchDetails',$attributes)?>
<!-- form start -->
<form role="form">
<input type="hidden" name="tabNum" value="2"/>
<input type="hidden" name="user_id" value="<?php echo $user_id?>"/>
<div class="box-body">
<div class="form-group has-feedback">
<label>Title of Proposed Research</label>
<textarea class="form-control" rows="3" id="research_title" name="research_title" value="<?=!form_error('research_title')?set_value('research_title'):''?>" placeholder="Title of Proposed Research" required></textarea>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Summary of Project</label>
<p class="help-block">(not more than 200 words)</p>
<textarea class="form-control" rows="3" id="project_summary" name="project_summary" value="<?=!form_error('project_summary')?set_value('project_summary'):''?>" placeholder="Summary of Project" required></textarea>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>The necessity of the item for the project</label>
<p class="help-block">(not more than 200 words)</p>
<textarea class="form-control" rows="3" id="item_necessity" name="item_necessity" value="<?=!form_error('item_necessity')?set_value('item_necessity'):''?>" placeholder="The necessity of the item for the project" required></textarea>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
<div class="form-group has-feedback">
<label>Expected Outcome</label>
<p class="help-block">(not more than 200 words)</p>
<textarea class="form-control" rows="3" id="expected_outcome" name="expected_outcome" value="<?=!form_error('expected_outcome')?set_value('expected_outcome'):''?>" placeholder="Expected Outcome" required></textarea>
<div class="glyphicon form-control-feedback" aria-hidden="true"></div>
<div class="help-block with-errors"></div>
</div>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div><!-- /.box -->
</div>
答案 0 :(得分:0)
为了更清楚,您需要在新行传递错误。 E.g:
<input type="text" class="form-control" id="academic_position" name="academic_position" value="<?php echo $this->input->post('academic_position') ?>" placeholder="Academic Position" required>
<?= form_error('academic_position') ?>
希望它会对你有帮助。
答案 1 :(得分:0)
我发现了问题。问题不在于标签。
<textarea class="form-control" rows="3" id="research_title" name="research_title" value="<?=!form_error('research_title')?set_value('research_title'):''?>" placeholder="Title of Proposed Research" required></textarea>
文本区域没有值。愚蠢的错误。所以解决方案应该是:
<textarea class="form-control" rows="3" id="research_title" name="research_title" placeholder="Title of Proposed Research" required><?=!form_error('research_title')?set_value('research_title'):''?></textarea>