Php 如何验证Codeigniter中组合的多个字段?
是否有一种创造性且简单的方法可以同时检查多个表单字段 我有一个表单,其中包含动态生成的字段,每个字段都有一个唯一的id 问题是提交所有字段都不是必需的,但在提交之前必须至少填写一个字段 在Codeigniter中是否有这样做的方法,或者我将如何有效地验证这一点 我知道可以单独检查每个字段,但我正在寻找一种更干净的方法Php 如何验证Codeigniter中组合的多个字段?,php,codeigniter,Php,Codeigniter,是否有一种创造性且简单的方法可以同时检查多个表单字段 我有一个表单,其中包含动态生成的字段,每个字段都有一个唯一的id 问题是提交所有字段都不是必需的,但在提交之前必须至少填写一个字段 在Codeigniter中是否有这样做的方法,或者我将如何有效地验证这一点 我知道可以单独检查每个字段,但我正在寻找一种更干净的方法 我希望你们清楚。谢谢。不完全确定您的意思,因为无论您采用何种方式,都必须单独检查字段。除非你连接了所有的输入并检查它?也许我错过了一些东西 只需转到客户端: 服务器端: 希望这有帮
我希望你们清楚。谢谢。不完全确定您的意思,因为无论您采用何种方式,都必须单独检查字段。除非你连接了所有的输入并检查它?也许我错过了一些东西 只需转到客户端: 服务器端:
希望这有帮助…我不完全确定您的意思,因为无论您采用何种方式,字段都必须单独检查。除非你连接了所有的输入并检查它?也许我错过了一些东西 只需转到客户端: 服务器端:
希望这有助于…现在,我有一个自定义功能,可以批量检查表单上的每个提交内容,以确保它符合一组规则。我认为可以将我的表单设置为html数组(
data[]
),在CodeIgniter中检索它,然后使用CI回调
验证函数来确保一切正常。它看起来很复杂,所以我还没有完全了解它,但也许这可以让你的车轮朝着正确的方向转动
编辑:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
然后,根据需要验证的内容编写自定义验证函数
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}
现在,我有一个自定义函数,它将批量检查表单上的每个提交,以确保它符合一组规则。我认为可以将我的表单设置为html数组(
data[]
),在CodeIgniter中检索它,然后使用CI回调
验证函数来确保一切正常。它看起来很复杂,所以我还没有完全了解它,但也许这可以让你的车轮朝着正确的方向转动
编辑:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
然后,根据需要验证的内容编写自定义验证函数
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}
试试这个:
$_POST['data_you_want_to_validate_together'] = $_POST['first_field'] . $_POST['second_field'];
$this->form_validation->set_rules('data_you_want_to_validate_together','Some Data', 'required|callback_some_function');
现在,您可以通过以下方式获取数据:
echo $this->input->post('data_you_want_to_validate_together');
试试这个:
$_POST['data_you_want_to_validate_together'] = $_POST['first_field'] . $_POST['second_field'];
$this->form_validation->set_rules('data_you_want_to_validate_together','Some Data', 'required|callback_some_function');
现在,您可以通过以下方式获取数据:
echo $this->input->post('data_you_want_to_validate_together');
您的输入字段是如何命名的?一个
前缀
后接一个时间戳
表示日期范围内的日期。您的输入字段是如何命名的?一个前缀
后接一个时间戳
表示日期范围内的日期。我离开了一段时间,我不明白您的意思。目前,我正在遍历所有字段,并返回一个错误,即使其中一个失败。不太方便。但我会努力理解你的意思,并尝试实施。我离开了一段时间,我不明白你的意思。目前,我正在遍历所有字段,并返回一个错误,即使其中一个失败。不太方便。但我会尽力理解你的意思,并尝试实施。