Welcome Guest, Not a member yet? Register   Sign In
form_error not displayed
#1

(This post was last modified: 12-29-2014, 02:48 AM by Aurélienwww. Edit Reason: As includebeer noticed, i've added an echo in php demo line with form_error() )

Hello everybody !

I'm developping a web application using CodeIgniter with form helper and form_validation library.

I'm trying to do a form with 15 fields to fill. The action of the form calls a method named "add()" which is :


PHP Code:
public function add(){

 
       /*Vérification qu'un utilisateur est connecté afin d'éviter qu'un utilisateur déconnecté puisse soumettre le formulaire*/
 
       if($this->session->userdata('id')){

 
           /*ETAPE 1 : chargement de la librairie contenant les regex*/
 
           $this->load->library('regex_lib');


 
           /*ETAPE 2 : définition des règles de vérification des champs du formulaire*/
 
           $this->form_validation->set_rules("client","Client","required|regex_match[".$this->regex_lib->RegList('alphaNumSpaceChar')."]");
 
           $this->form_validation->set_rules("departement","Departement","required|regex_match[".$this->regex_lib->RegList('departement')."]");
 
           $this->form_validation->set_rules("source","Source","required|regex_match[".$this->regex_lib->RegList('alphaNumSpaceChar')."]");
 
           $this->form_validation->set_rules("ca","Chiffre d'affaire","required|numeric");
 
           $this->form_validation->set_rules("nbr_version","Nombre de versions","required|integer");
 
           $this->form_validation->set_rules("type","Type","required|regex_match[".$this->regex_lib->RegList('type')."]");
 
           $this->form_validation->set_rules("date_devis","Date du devis","required|regex_match[".$this->regex_lib->RegList('date')."]");
 
           $this->form_validation->set_rules("date_relance","Date de relance","required|regex_match[".$this->regex_lib->RegList('date')."]");
 
           $this->form_validation->set_rules("taux_reussite","Taux de réussite","required|regex_match[".$this->regex_lib->RegList('pourcentage')."]");
 
           $this->form_validation->set_rules("prospection","Prospection","required|integer");
 
           $this->form_validation->set_rules("travail","Travail","required|integer");
 
           $this->form_validation->set_rules("signature","Signature","required|integer");
 
           $this->form_validation->set_rules("suivi","Suivi","required|integer");
 
           $this->form_validation->set_rules("tps_deplacement","Temps de déplacement","required|numeric");
 
           $this->form_validation->set_rules("validation","Validation","required|regex_match[".$this->regex_lib->RegList('true_false')."]");

 
           /*ETAPE 3 : mise en forme des messages d'erreur*/
 
           $this->form_validation->set_error_delimiters('<div class="alert alert-danger">''</div>');
 
           $this->form_validation->set_message("required","Vous avez oublié de spécifier le champ '%s'");
 
           $this->form_validation->set_message("numeric","Le format du champ '%s' n'est pas valide");
 
           $this->form_validation->set_message("integer","Le format du champ '%s' n'est pas valide");
 
           $this->form_validation->set_message("regex_match","Le champ '%s' n'est pas valide");


 
           /*ETAPE 4 : vérification de la présence d'erreur dans le formulaire*/
 
           if($this->form_validation->run() == false){
 
               $this->create();
 
           }else{
 
               exit('test success');
 
               $this->devis_model->addDevis();
 
               $this->session->set_flashdata('devis_created',"<div class='alert alert-success'>Votre devis a correctement été ajouté.</div>");
 
               $this->lister();
 
           }

 
       /*Dans le cas où l'utilisateur n'est pas connecté, il est redirigé vers la page de connexion*/
 
       }else{
 
           $this->getLayout('accueil');
 
       }

 
   


Comments are in french, but the only thing i do different from CI documentation is loading a library containing all my regex.
It's not the first time i do this kind of form and it has always worked so far.

For info "create()" is a method used to load the view.

The problem is : errors are not displayed in view when i'm using for exemple with the first field:

PHP Code:
<?php echo form_error('client'); ?>

I really don't understand what's going wrong, I've tried to use
PHP Code:
<?php echo validation_errors(); ?>
but nothing is displayed.

Here is my view :

PHP Code:
<form class="form-horizontal" action="<?php echo site_url('devis/add');?>" method="post">

 
           <div class="form-group">
 
               <label class="col-xs-12 col-sm-4 control-label" for="client">Client</label>
 
               <div class="col-xs-12 col-sm-8">
 
                   <input type="text" class="form-control" id="client" name="client" placeholder="Nom, prénom, ville">
 
               </div>
 
               <?php echo form_error('client'); ?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="departement">Département</label>
                <div class="col-xs-12 col-sm-8">
                    <select class="form-control" id="departement" name="departement">
                        <?php
                        foreach
($dep as $key=>$value){
 
                           ?>
                            <option value="<?php echo $value->departement_id;?>"><?php echo $value->departement_code " - " $value->departement_nom?></option>
                        <?php
                        
}
 
                       ?>
                    </select>
                </div>
                <?php echo form_error('departement');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="source">Source</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="source" name="source" placeholder="Nom du contact">
                </div>
                <?php echo form_error('source');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="ca">Chiffre d'affaire</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="ca" name="ca" placeholder="Chiffre d'affaire hors taxes, sans éco-part">
                </div>
                <?php echo form_error('ca');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="nbr_version">Nombre de versions</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="nbr_version" name="nbr_version">
                </div>
                <?php echo form_error('nbr_version');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label">Type</label>
                <div class="col-xs-12 col-sm-8">
                    <label class="radio-inline">
                        <input type="radio" name="type" id="type1" value="int">Intérieur
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="type" id="type2" value="ext">Extérieur
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="type" id="type3" value="int_ext">Intérieur & extérieur
                    </label>
                </div>
                <?php echo form_error('type');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="date_devis">Date du devis</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="date_devis" name="date_devis" placeholder="Date au format jj/mm/aaaa, ex: 12/05/2014">
                </div>
                <?php echo form_error('date_devis');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="date_relance">Date de la relance</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="date_relance" name="date_relance" placeholder="Date au format jj/mm/aaaa, ex: 12/05/2014">
                </div>
                <?php echo form_error('date_relance');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="taux_reussite">Taux de réussite</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="taux_reussite" name="taux_reussite" placeholder="A exprimer en pourcentage, ex : 90 (pour 90%)">
                </div>
                <?php echo form_error('taux_reussite');?>
            </div>

            <br />
            <h4>Nombre de rendez-vous :</h4>
            <hr />

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="prospection">Prospection</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="prospection" name="prospection">
                </div>
                <?php echo form_error('prospection');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="travail">Travail</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="travail" name="travail">
                </div>
                <?php echo form_error('travail');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="signature">Signature</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="signature" name="signature">
                </div>
                <?php echo form_error('signature');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="suivi">Suivi</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="suivi" name="suivi">
                </div>
                <?php echo form_error('suivi');?>
            </div>

            <div class="form-group">
                <label class="col-xs-12 col-sm-4 control-label" for="tps_deplacement">Temps de déplacement</label>
                <div class="col-xs-12 col-sm-8">
                    <input type="text" class="form-control" id="tps_deplacement" name="tps_deplacement" placeholder="A exprimer en heure, ex : 3,5 (pour 3 heures et demie)">
                </div>
                <?php echo form_error('tps_deplacement');?>
            </div>

            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" id="validation" name="validation" value="validation">Validation
                        </label>
                    </div>
                    <?php echo form_error('validation');?>
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-8 col-sm-offset-4">
                    <button type="submit" class="btn btn-primary">Valider</button>
                </div>
            </div>

        </form> 

If you need more precision, don't hesitate to ask.

Thank you, have nice christmas holidays !!
Reply
#2

Have you also loaded the validation lib by autoload or as followed?

PHP Code:
$this->load->library('form_validation'); 

And if this dont help, can you post the content of your create() method ...

Reply
#3

I've loaded it with autoload.php

here is create():

PHP Code:
public function create(){

        
/*Vérification qu'un utilisateur est bien connecté*/
        
if($this->session->userdata('id')){

            
/*Récupération de la liste de tous les département français qui servira à alimenter les options du bouton select du formulaire*/
            
$data['dep'] = $this->departement_model->getAllDepartments();

            
$this->getLayout('devis/creer',$data);

        
/*Si la variable de session contenant l'id de l'utilisateur n'existe pas, c'est qu'il n'est pas connecté, on le redirige donc vers la page de connexion*/
        
}else{
            
$this->getLayout('accueil');
        }

    } 

In this method, i use a model to get all french departments that are used as <option> in my <select> field in the form.
Reply
#4

(12-28-2014, 10:54 AM)Aurélienwww Wrote: The problem is : errors are not displayed in view when i'm using for exemple with the first field:



PHP Code:
<?php form_error('client'); ?>

You need to echo the error message. Did  you just forgot the "echo" in your post or it's really not in your code?


PHP Code:
<?php echo form_error('client'); ?>
Reply
#5

Sorry my bad, I've forgotten it in this demo line, but "echo" is used everywhere in my view, so i don't think that is the problem.

I will edit this line.
Reply
#6

Your function getLayout, does it load the view directly or does it makes a redirect?

PHP Code:
$this->getLayout('devis/creer',$data); 
Reply
#7

(This post was last modified: 12-29-2014, 05:48 AM by Aurélienwww.)

Here is my getLayout which extends CI_controllers and all others controllers extends Layout. This function doesn't not redirect but load a header, a navigation bar (if it's needed), the content, and a footer.

PHP Code:
class Layout extends CI_Controller{

    
/**
     *Le controlleur "layout" permet de générer une structure de page identique pour toutes les pages de l'application
     *Cette structure est composée d'un header, du corps de page et d'un footer
     *Le corps de page géré par d'autres controlleur appel l'action getLayout en faisant passer le nom de la vue et les données en paramètres
     */

    
public function getHeader()
    {
        
$this->load->view('partial/header.php');
    }

    public function 
getFooter()
    {
        
$this->load->view('partial/footer.php');
    }

    public function 
getNav(){
        
$this->load->view('partial/nav.php');
    }

    public function 
getLayout($page,$data=array())
    {

        
$this->getHeader();

        
/*On charge la barre de navigation si la page appelée est différente de la page accueil, autrement dit de la page connexion où la navigation n'apparait pas.*/
        
if($page != 'accueil'){
            
$this->getNav();
        }
        
$this->load->view($page,$data);
        
$this->getFooter();
    }

Reply
#8

Why would you use another method to load a view with the form? You've just called another method without passing it any data.
Reply
#9

(This post was last modified: 12-29-2014, 07:24 AM by Aurélienwww.)

Actually I pass some default datas with method "create()". This method use a model to pass some database datas to the view with the form.

I could put that part of the code in the same method called "add()" but I wanted to separate those two things.

I can resume my code as follow considering a user is filling the form :

  1. A user fills the form and push "valider" button, the controller and action called are "devis/add".
  2. In "devis/add", according to rules of validation we look if there are errors or not.
  3. If there are some, We are taken to a method called "create()" which is gonna load the view with the form passing some datas needed to display form fields (<option> of <select> tag are generated this way). This is the same method called initialy to get the page with the form.
  4. If there a no errors, the user is taken to a success page.
Reply
#10

Ok I've found something which can may help.

After following tutorial on Codeigniter official documentation, I have seen that whether there is success page or not, the view is directly loaded using:
PHP Code:
$this->load->view('form'); 

I have three forms in my application : 1 for login, an other to let the user changing his password, and a last one used to deal with client orders. The first form (login one) works perfectly using my function "getLayout('name_of_page')" to display it. The two others don't work using the same function but when I load the view directly (so without using "getLayout" and thus, without displaying header, nav and footer), there work, form_error() function displays errors as wanted.

So, I really don't understand why it happens this way. I don't know what to do with my getLayout function. One form works perfectly with it and the two others don't. It's not the first time I'm developping applications using CodeIgniter and I've never had this problem before.

If anyone has any idea I will be so happy :p

Regards !
Reply




Theme © iAndrew 2016 - Forum software by © MyBB