这是我第一次在StackOverflow中询问。所以请原谅我,如果我错了,我是PHP和CodeIgniter的新手。 好的,我有一个用于创建新数据的页面。 我正在CI上使用表单验证,但是如果我填写表单没有任何事情,那就不行了。这是我的代码。
我的控制器
function __construct()
{
parent::__construct();
/* LOAD CORE LIBRARY */
$this->load->library('ion_auth');
$this->load->library('form_validation');
/* CHECK IF USER IS AUTHENTICATED */
if (!$this->ion_auth->logged_in())
redirect('bunker/login', 'refresh');
$user = $this->ion_auth->user()->row();
if(empty($user->last_pwd_change)){
redirect('bunker/account/renew', 'refresh');
}
/* THEN LOAD EVERYTHING ELSE */
/* LOAD ALL NECESSARY HELPER */
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load('auth');
// Load MongoDB library instead of native db driver if required
$this->config->item('use_mongodb', 'ion_auth') ?
$this->load->library('mongo_db') : $this->load->database();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
/* LOAD ALL NECESSARY MODELS */
$this->load->model('system/company_model');
$this->load->model('system/common_model');
$this->load->model('website/article_model');
$this->load->model('website/page_model');
$this->menu['company'] = $this->company_model->getCompany();
}
function create(){
$post = $this->input->post();
if(!empty($post)){
$this->form_validation->set_rules('page_id', "Page Name", 'required|numeric|xss_clean');
$this->form_validation->set_rules('article_title', "Article Title", 'required|xss_clean');
$this->form_validation->set_rules('article_url', "Custom URL", 'xss_clean');
$this->form_validation->set_rules('article_status', "Publication Status", 'required|xss_clean');
$this->form_validation->set_rules('article_excerpt', "Excerpt", 'required|min_length[250]|xss_clean');
$this->form_validation->set_rules('article_content', "Content", 'required|min_lenght[500]|xss_clean');
$this->form_validation->set_error_delimiters('<li>','</li>');
if ($this->form_validation->run() == TRUE){
if(isset($_FILES['article_file'])){
$cover = $this->common_model->uploadPicture('uploads/images/','article_file');
$authorId = $this->ion_auth->get_user_id();
$result = $this->article_model->addArticle($this->input->post(), $authorId, $cover);
if($result){
$data = array(
'response' => 'success',
'message' => 'Article successfully created.'
);
}else{
$data = array(
'response' => 'error',
'message' => 'Failed to create article, refresh your browser and try again.'
);
}
}else{
$data = array(
'response' => 'error',
'message' => 'Please provide an image for your article.'
);
}
}else{
$data['response'] = 'error';
$data['message'] = '<ul>';
$data['message'] .= form_error('page_id');
$data['message'] .= form_error('article_title');
$data['message'] .= form_error('article_url');
$data['message'] .= form_error('article_status');
$data['message'] .= form_error('article_excerpt');
$data['message'] .= form_error('article_content');
$data['message'] .= "</ul>";
}
header('Content-Type: application/json');
echo json_encode($data);
exit;
}else{
// DISPLAY THE PAGE
$pages = $this->page_model->getAllPages();
if(!$pages){
$this->data['pages'] = NULL;
}else{
$this->data['pages'] = $pages;
}
$this->footer['status'] = "new";
$this->load->view('common/header');
$this->load->view('common/menu', $this->menu);
$this->load->view('website/article/article_form', $this->data);
$this->load->view('common/script', $this->footer);
$this->load->view('common/common');
$this->load->view('website/article/article_script');
$this->load->view('common/footer');
}
}
查看/表单
<div class="block-content">
<h2><strong>Publish</strong> New Article</h2>
<p>Use the form below to write a new article.</p>
<form name="article-form" id="article-form" action="<?= base_url(); ?>article/create" method="POST" enctype="multipart/form-data">
<div class="col-md-6">
<div class="form-group">
<label>Page:</label>
<select name="page_id" class="form-control">
<option value='0' selected="selected">- Choose -</option>
<?php if($pages != NULL): ?>
<?php foreach($pages as $page): ?>
<option value='<?= $page->id; ?>'><?= $page->name;?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
<span class="help-block">Where do you want to publish the article.</span>
</div>
<div class="form-group">
<label>Article Title:</label>
<input name="article_title" id="article_title" type="text" placeholder="Title of your article" class="form-control">
<span class="help-block">Provide a title for your article (by default, title will used for the custom url).</span>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Custom URL:</label>
<input name="article_url" id="article_url" type="text" placeholder="(Optional Field)" class="form-control">
<span class="help-block">A custom url for this article (i.e <?= base_url(); ?>my-custom-url/).</span>
</div>
<div class="form-group">
<label>Status:</label>
<select name="article_status" class="form-control">
<option value='0'>- Choose -</option>
<option value='draft'>Draft</option>
<option value='publish'>Published</option>
</select>
<span class="help-block">Assign publication status.</span>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<div class="input-group file">
<input name="article_filename" type="text" placeholder="Select image for your article" class="form-control">
<input name="article_file" type="file">
<span class="input-group-btn">
<button type="button" class="btn btn-primary">Browse</button>
</span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label>Excerpt:</label>
<textarea name="article_excerpt" id="article_excerpt" class="form-control"></textarea>
<span class="help-block">Provide a short excerpt for your article, an excerpt may only be 1 or 2 paragraphs long.</span>
</div>
<div class="form-group">
<label>Content:</label>
<textarea name="article_content" id="article_content" class="form-control"></textarea>
<span class="help-block">Provide the actual content of your article</span>
</div>
<div class="form-group">
<label>Tags:</label>
<input name="article_tags" id="article_tags" type="text" value="" class="form-control tagsinput">
<span class="help-block">Provide some tag for your article</span>
</div>
</div>
</form>
</div>
<button type="button" onClick="submitForm();" class="btn btn-success">
的javascript
<script type="text/javascript">
/* SCRIPT FOR NEW ARTICLE */
$(document).ready(function(){
$('#article-form').ajaxForm({
dataType : 'json',
beforeSubmit : ShowRequest,
success : SubmitSuccesful,
error : AjaxError
});
});
function submitForm(){
tinyMCE.triggerSave();
$('#article-form').submit();
}
function ShowRequest(formData, jqForm, options) {
return true;
}
function AjaxError(){
// alertify.alert("An unknown error has occured, please refresh your browser.");
noty({
layout: 'topRight',
text: 'Unknown error occured, please refresh your browser and try again.',
type: 'warning'
});
}
function SubmitSuccesful(responseText, statusText) {
if(responseText.response == "success"){
$('#article-form')[0].reset();
noty({
text: responseText.message,
layout: 'topRight',
type: 'success'
});
window.setTimeout(function(){location.href="<?=base_url()?>bunker/article"}, 2000);
}else{
noty({
text: responseText.message,
layout: 'topRight',
type: 'error'
});
}
}
function articleRemove(articleId){
noty({
text: 'Are you sure you want to delete this page?',
buttons: [
{
addClass: 'btn btn-danger btn-clean',
text: 'Delete',
onClick: function($noty) {
console.log("Confirmed");
console.log("Article ID: " + articleId);
$.ajax({
url: '<?= base_url(); ?>bunker/article/delete/' + articleId,
type: 'POST',
dataType: 'json',
success: function (data) {
console.log(data.response);
noty({
layout: 'topRight',
text: data.message,
type: 'success'
});
window.setTimeout(function(){location.href="<?=base_url()?>bunker/article"},2000);
},
failed: function(data){
console.log(data.response);
noty({
layout: 'topRight',
text: data.message,
type: 'error'
});
},
data: null
});
$noty.close();
}
},
{
addClass: 'btn btn-success btn-clean',
text: 'Cancel',
onClick: function($noty) {
$noty.close();
}
}
]
});
}
</script>
抱歉,如果我的英语太糟糕了。
答案 0 :(得分:0)
重构控制器#create()方法,以便不使用操作后检查。而是使用表单验证运行方法。
示例:
$this->form_validation->set_rules('page_id', "Page Name", 'required|numeric|xss_clean');
$this->form_validation->set_rules('article_title', "Article Title", 'required|xss_clean');
$this->form_validation->set_rules('article_url', "Custom URL", 'xss_clean');
$this->form_validation->set_rules('article_status', "Publication Status", 'required|xss_clean');
$this->form_validation->set_rules('article_excerpt', "Excerpt", 'required|min_length[250]|xss_clean');
$this->form_validation->set_rules('article_content', "Content", 'required|min_lenght[500]|xss_clean');
$this->form_validation->set_error_delimiters('<li>','</li>');
if ($this->form_validation->run() == TRUE){
// Display Succeeded Site or something else
} else {
// Display Failed Site or Errors or something else
}