CodeIgniter变量/输入的验证不是来自表单吗? [英] CodeIgniter validation of variables / input not coming from form?

查看:44
本文介绍了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';
}

这篇关于CodeIgniter变量/输入的验证不是来自表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆