简体   繁体   English

如何在 CI3 中使用 form_validation?

[英]How can I use form_validation in CI3?

I'm coding a project with CI and I have a doubt about the form_validation code, my professor tough me one way to put the rules in the form_validation with arrays, like this我正在用 CI 编写一个项目,我对 form_validation 代码有疑问,我的教授用 arrays 向我提出了一种将规则放入 form_validation 的方法,就像这样

$config = array (
  'jugador' =>array(
    array(
    'field' => 'correoJug',
    'label' => 'Correo',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'nombreJug',
    'label' => 'Nombre',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'tagJug',
    'label' => 'Tag',
    'rules' => 'trim|required|htmlspecialchars', 
    ),
    array(
    'field' => 'apellidosPatJug',
    'label' => 'Apellido paterno',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'apellidosMatJug',
    'label' => 'Apellido materno',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'trim|required|htmlspecialchars',
    )
  )
);

Where 'jugador' is used for one view, my doubt is the next, I want to use the form_validation for another view, do I need to add another arrays at the end for my another view, like this:在“jugador”用于一个视图的地方,我的疑问是下一个,我想将 form_validation 用于另一个视图,是否需要在最后添加另一个 arrays 用于我的另一个视图,如下所示:

    array(
    'field' => 'apellidosMatJug',
    'label' => 'Apellido materno',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'trim|required|htmlspecialchars',
    )
)
    'OG' =>array(
    array(
    'field' => 'correoOg',
    'label' => 'Correo',
    'rules' => 'trim|required|htmlspecialchars',
    ),
    array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'trim|required|htmlspecialchars',
    )
 );

Or do I need to create another variable in the same file OR I just need to create another form_validation file.或者我是否需要在同一个文件中创建另一个变量,或者我只需要创建另一个 form_validation 文件。

I hope you understand what I'm saying and could help me我希望你明白我在说什么并能帮助我

You can create multiple validation array in single file.您可以在单个文件中创建多个验证数组。

$config = array(
  'first_validation' => array(
    'name' => 'trim|required',
    'password' => 'trim|required'
  ),
  'second_validation' => array(
    'phone' => 'trim|required',
    'email' => 'trim|required'
  )
)

and in CONTROLLER并在CONTROLLER

if ($this->form_validation->run('first_validation') == TRUE) {
   //success
} else {
  // error
}

in some another controller在另一个 controller

if ($this->form_validation->run('second_validation') == TRUE) {
   //success
} else {
  //error
}

You have to created another variable to set form validation like this您必须创建另一个变量来设置这样的表单验证

$config['form1'] = array(
               array(
                     'field'   => 'username', 
                     'label'   => 'Username', 
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'password', 
                     'label'   => 'Password', 
                     'rules'   => 'required'
                  ),
            );

$config['form2'] = array(
               array(
                     'field'   => 'email', 
                     'label'   => 'Email', 
                     'rules'   => 'required'
                  ),
            );

Than use as you are doing.比照你做的那样使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM