PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] How can I use form_validation in CI3?

 March 09, 2022     codeigniter, forms, php, validation     No comments   

Issue

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

$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:

    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.

I hope you understand what I'm saying and could help me


Solution

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

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

in some another controller

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


Answered By - Saud Khan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing