티스토리 뷰

웹개발/Php

Kohana Validation 체크

yaku 2011. 8. 26. 07:03

* 생성
 
$validation = Validation::factory(array(
'name' => 'Lysender',
'email' => 'lysender@troy.org',
'address' => 'City of Troy',
'zip' => '1234'
));


* 룰 추가 

// Adding a rule looks the same syntax but let's dig deeper later on
$validation->rule('name', 'not_empty');
$validation->rule('name', 'min_length', array(':value', 4));

 

 // Some custom callback function
function is_visitor_email_unique($visitor_model, $email)
{
return (boolean) $visitor_model->get_by_email($email);
}

function is_visitor_name_unique($visitor_model, $name)
{
return (boolean) $visitor_model->get_by_name($name);
}

// Then your validation rule will look like this
$visitor_model = new Model_Visitor;

$validation->bind(':visitor', $visitor_model);

$validation->rule('email', 'is_visitor_email_unique', array(':visitor', ':value'));
$validation->rule('name', 'is_visitor_name_unique', array(':visitor', ':value'));


class Model_Visitor
{
public function is_visitor_name_unique($name)
{
return (boolean) $this->get_by_name($name);
}

public function is_visitor_email_unique($email)
{
return (boolean) $this->get_by_email($email);
}

public function get_by_name($name)
{
// Some magic that will retrieve a record by looking up a name
// ...

return $record;
}

public function get_by_email($email)
{
// Some magic that will retrieve a record by looking up an email address
// ...
return $record;
}
}

$visitor = new Model_Visitor;
$validation->rule('name', array($visitor, 'is_visitor_name_unique'));



많은 룰을 지정

 $validation->rule('name', 'not_empty')
->rule('name', 'min_length', array(':value', 4))
->rule('name', 'max_length', array(':value', 16))
->rule('email', 'not_empty')
->rule('email', 'email')
->rule('address', 'min_length', array(':value', 5))
->rule('address', 'max_length', array(':value', 100))
->rule('zip', 'min_length', array(':value', 3))
->rule('zip', 'max_length', array(':value', 4));

$result = $validation->check();



패스워드 확인 지정

$validation->rule('password_confirm', 'matches', array(':validation', 'password_confirm', 'password'));


application/messages/signup.php 지정
 


<?php

return array(
'name' => array(
'not_empty' => 'Name is required',
'min_length' => 'Name is too short, minimum is :param2',
'max_length' => 'Name is too long, maximum is :param2'
),
'email' => array(
'not_empty' => 'Email is required',
'email' => 'Email must be a valid email address'
),
'address' => array(
'min_length' => 'Address is too short, minimum is :param2',
'max_length' => 'Address is too long, maximum is :param2'
),
'zip' => array(
'min_length' => 'Zip is too short, minimum is :param2',
'max_length' => 'Zip is too long, maximum is :param2'
),
);
 

 

'웹개발 > Php' 카테고리의 다른 글

PHP 파일 모드 간단 설명  (0) 2012.01.11
PHP 정규 표현식 10가지 사용 예제  (2) 2011.09.06
Kohana Query Builder  (0) 2011.08.26
Kohana Request  (0) 2011.08.26
Kohana 3.2 Config 얻기..  (0) 2011.08.24
댓글
D-DAY
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함