CodeIgniter变量/输入的验证不是来自表单吗?
问题描述:
让我们假设我有用户生成的数据,这些数据不是通过表单发布的.有没有一种方法可以使用/扩展CodeIgnitors form_validation类来验证该数据.
Lets assume I have user generated data which is not coming through a form post. Is there a way I can use/ extend CodeIgnitors form_validation class to validate that data.
例如
<?php
$userData = array('name' => 'tt' , 'city' => 'London' , 'age' => '1200' );
// How can I validate this data using form_validation checks
// like min_length,max_length,and apply some processing
// like trim,xss_clean provided by the same class
?>
答
是的,您可以通过 set_data()
方法进行操作,
Yes you can via set_data()
method, Here you go.
$this->form_validation->set_data(array(
'name' => 'Full Name',
'city' => 'City',
));
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('city', 'City', 'trim|required');
$this->form_validation->set_rules('age', 'Age', 'trim|required');
if ($this->form_validation->run() == FALSE) {
echo 'Invalid: ' . validation_errors();
} else {
echo 'Valid';
}