폼검증(form_validation) Sample

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Cart Class
 *
 * Created on 2011. 11. 15.
 * @author Kangmin
 * @package library
 * @subpackage controllers
 * @version 1.0
 */
class Form_validation_lib extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();

		//load lib
		$this->load->library('form_validation');
	}

	public function index()
	{
		$data = NULL;

		//rules
		$this->form_validation->set_rules('password', '비밀번호', 'trim|htmlspecialchars|required');
		$this->form_validation->set_rules('password_confirm', '비밀번호 확인', 'trim|htmlspecialchars|required|matches[password]');
		$this->form_validation->set_rules('min_max_length', '2~3자만 가능', 'trim|htmlspecialchars|required|min_length[2]|max_length[3]');
		$this->form_validation->set_rules('exact_length', '3자만 가능', 'trim|htmlspecialchars|required|exact_length[3]');

		//폼검증 성공이면
		if ($this->form_validation->run() == TRUE) 
		{
			//request
			$req_password = $this->form_validation->set_value('password');
			$data['result'] = '성공';
		} 

		$this->load->view('form_validation_lib_sample', $data);  
	}
}

//EOF
<?php $this->load->view('inc/header')?>

<?php
 //폼검증 에러 표출
 if(validation_errors())
 {
  echo '<div id = "error"><h1>'.validation_errors().'</h1></div>';
 }

 if(isset($result) == TRUE) echo '<div id = "result"><h1>'.$result.'</h1></div>';
?>

기본 룰 : trim|htmlspecialchars|required
<form name = "form" id = "form" method = "post" action = "<?=site_url('form_validation_lib')?>">
<input type = "text" name = "password" id = "password" value = "<?php echo set_value('password'); ?>" />비밀번호<br />
<input type = "text" name = "password_confirm" id = "password_confirm" value = "<?php echo set_value('password_confirm'); ?>" />비밀번호 확인 (matches['password'])<br />
<input type = "text" name = "min_max_length" id = "min_max_length" value = "<?php echo set_value('min_max_length'); ?>" />2~3자만 가능 (min_length[2]|max_length[3])<br />
<input type = "text" name = "exact_length" id = "exact_length" value = "<?php echo set_value('exact_length'); ?>" />3자만 가능 (exact_length[3])<br />
다른 룰도 메뉴얼을 참조하여 간단히 | (파이프)로 나열하여 검증할 수 있습니다.<br />
<input type = "submit" value = "POST" />
</form>


<?php $this->load->view('inc/footer')?>

© Copyright by hoksi(Page rendered in 0.0191 seconds)