formignator를 formignator에서 양식에 적용하려고합니다. 새 페이지를 추가 할 때 상위 드롭 다운 섹션에 오류가 표시되지 않지만 양식 유효성 검사 후 예를 들어 사용자가 제목 레이블을 입력하는 것을 잊어 버린 경우, 빈 페이지이거나 유효성 검사 후 드롭 다운 섹션.유효성 검사 후 Codeigniter 양식 드롭 다운 오류
두 번째 변수는 form_dropdown에 대해 배열이어야합니다. 하지만 내 배열이며 유효성 검사 전에 페이지에 오류를 포기하지 않습니다. 그래서 나는 문제를 파악할 수 없다.
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
MY_Controller :
class MY_Controller extends CI_Controller{
public $data = array();
function __construct(){
parent::__construct();
}
}
관리 컨트롤러 :
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
$this->data['meta_title'] = 'My Website Control Panel';
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters(' <div class=" alert alert-danger alert-block" ><span class="glyphicon glyphicon glyphicon-minus-sign"></span> ', '<span class="close" data-dismiss="alert">×</span></div>');
$this->load->library('session');
$this->load->model('user_m');
$exceptions = array('admin/user/login', 'admin/user/logout');
if(in_array(uri_string(),$exceptions) == FALSE){
if($this->user_m->loggedin() == FALSE){
redirect('admin/user/login');
}
}
}
}
컨트롤러 :
class Page extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index(){
$this->data['pages'] = $this->page_m->get_with_parent();
$this->data['subview'] = "admin/page/index";
$this->load->view('admin/_layout_main',$this->data);
}
public function edit ($id = NULL)
{
// Fetch a page or set a new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'Not found.';
}
else {
$this->data['page'] = $this->page_m->get_new();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array('title', 'slug', 'body','keywords','description','parent_id'));
$this->page_m->save($data, $id);
redirect('admin/page');
}
// Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
}
모델 :
public function get_no_parents()
{
// Fetch pages without parents
$this->db->select('id, title');
$this->db->where('parent_id', 0);
$pages = parent::get();
// Return key => value pair array
$array = array(
0 => ''
);
if (count($pages)) {
foreach ($pages as $page) {
$array[$page->id] = $page->title;
}
}
return $array;
}
보기 :에서
<?=form_dropdown('parent_id', $pages_no_parents, set_value('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id);?>
내 생각에 유효성 검사 코드가 pages_no_parents 배열을 다시 전달하지 않아 유효성 검사가 페이지를 완전히 새로 고침하므로 페이지를 실행하는 데 필요한 변수가 if 유효성 검사 == false 코드로 다시 생성되어야합니다. 유효성 검사 코드를 보는 것도 도움이 될 것입니다. –
컨트롤러에 완전한 기능을 표시하지 않았으므로 덮어 쓰는 것이 분명하지 않습니다. 뷰 부분에 관해서는, set_value는 자동적으로 포스트 물건을 돌 봅니다. & 기억하고 있듯이, 3 번째 인자를 취하지 않습니다. – ahmad
당신은 옳았습니다. 고맙습니다. –