CodeIgniter表单验证。多个呼叫不工作 [英] CodeIgniter Form Validation. Multiple calls not working

查看:89
本文介绍了CodeIgniter表单验证。多个呼叫不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CodeIgniter。我一直在试图调试一个非工作脚本。



我得出的结论是,当使用 $ this-> form_validation- ; run(); (表单验证类),在第一个命名调用之后,例如 $ this-> form_validation-> run(form_1); ,所有后续调用都返回true。



我正在开发一个多步骤表单,当 $ this-> form_validation-> (form_1); 正确返回true, $ this-> form_validation-> run(form_2);

任何人都有什么线索为什么?多个调用不能在控制器中的单个函数中持有,还是有特殊的方法?
Cheers

解决方案

codeigniter的设置方式不允许您使用多个规则进行验证,可以使用函数来扩展表单助手以对规则进行分组( http://ellislab.com/codeigniter/forums) / viewthread / 120221 )或者像我在 application / config / form_validation.php 中做的那样,我只是将多个组合并成自己的一组规则,

 <?php if(!defined('BASEPATH'))exit允许访问); 

$ config = array(

campaign=> array(
array(
field=>campaign [title] ,
label=>campaign title,
rules=>trim | required | max_length [255] | xss_clean



user=> array(
array(
field=>user_info [email],
label=> email,
rules=>trim | required | valid_email | is_unique [user_info.email] | max_length [255] | xss_clean




$ config [campaign_user] = array_merge($ config ['campaign'],$ config ['user']);

感兴趣的行是最后一行,两个规则组合在一起:



$ config [campaign_user] = array_merge($ config ['campaign'],$ config ['user']);



在您的控制器中,您只需调用单一规则:

  if($ this-> form_validation-> run('campaign_user'))
{
#验证成功
}


I am using CodeIgniter. I have been trying to debug a non-working script.

I have come to the conclusion that when utilizing $this->form_validation->run(); (the form validation class), after the first named call, e.g $this->form_validation->run(form_1);, all following calls return true.

I am developing a multi step form and when $this->form_validation->run(form_1); correctly returns true, $this->form_validation->run(form_2); incorrectly returns true.

Anyone have any clue as to why? Can multiple calls not be held in a single function within a controller or is there a special approach? Cheers

解决方案

the way codeigniter is setup doesn't lend itself to allow you to validate with multiple rules, you can extend the form helper with a function to group rules (http://ellislab.com/codeigniter/forums/viewthread/120221) or like I did in my application/config/form_validation.php I simply combined multiple groups into its own set of rules and referred to the single rule of combined rules.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config = array(

  "campaign" => array(
    array(
      "field" => "campaign[title]",
      "label" => "campaign title",
      "rules" => "trim|required|max_length[255]|xss_clean"
    )
  ),

  "user" => array(
    array(
      "field" => "user_info[email]",
      "label" => "email",
      "rules" => "trim|required|valid_email|is_unique[user_info.email]|max_length[255]|xss_clean"
    )
  )
);

$config["campaign_user"] = array_merge($config['campaign'], $config['user']);

The line of interest is the last one, where the two rules are combined:

$config["campaign_user"] = array_merge($config['campaign'], $config['user']);

and in your controller you would just call the single rule:

if($this->form_validation->run('campaign_user'))
{
    # validation successful
}

这篇关于CodeIgniter表单验证。多个呼叫不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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