slam4:php:codeigniter:validation

Ceci est une ancienne révision du document !


Validation des formulaires

La bibliothèque form_validation doit être chargée :

  • soit automatiquement avec autoload.php :
$autoload['libraries'] = array('form_validation');
  • Soit dans le code d'un contrôleur par exemple :
$this->load->library('session');

Créer le formulaire myForm.php dans application/views/

|h application/views/myForm.php
<html>
<head>
<title>My Form</title>
</head>
<body>
 
<?php echo validation_errors(); ?>
 
<?php echo form_open('form'); ?>
 
<h5>Nom d'utilisateur</h5>
<input type="text" name="username" value="" size="50" />
 
<h5>Mot de passe</h5>
<input type="text" name="password" value="" size="50" />
 
<h5>Confirmation de mot de passe</h5>
<input type="text" name="passconf" value="" size="50" />
 
<h5>Adresse email</h5>
<input type="text" name="email" value="" size="50" />
 
<div><input type="submit" value="Submit" /></div>
 
</form>
 
</body>
</html>

Créer un contrôleur pour accéder et contrôler la vue :

|h application/controllers/form.php
<?php
 
class Form extends CI_Controller {
 
	function index()
	{
		$this->load->helper(array('form', 'url'));
 
		$this->load->library('form_validation');
 
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('myform');
		}
		else
		{
			$this->load->view('formsuccess');
		}
	}
}
?>
  • slam4/php/codeigniter/validation.1355007098.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)