0

I´m following a tutorial, but I am getting this error: Unable to access an error message corresponding to your field name

I´ve searched, but I can´t find a solution. The program is very simple: cformulario.php

<?php
class cFormulario extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
    }

    function mostrarDatos(){
        $this->form_validation->set_rules('txtnom', 'Nombre', 'trim|required|min_lenght[3]|max_lenght[50]');
        $this->form_validation->set_rules('txtmail', 'Correo', 'trim|required|valid_email');

        if($this->form_validation->run()==FALSE){
            $this->load->view('vformulario');
        }
        else{

            $datos=array(
                'nombre' => $this->input->post('txtnom'),
                'edad' => $this->input->post('txted'),
                'correo' => $this->input->post('txtmail')
            );
            $this->load->view("vformulario", $datos);
        }
    }


}

?>

vformulario.php

<body>
    <h1>Información Personal</h1>
    <br>

    <div id="error">
        <?php 
            echo validation_errors();
        ?>
    </div>
    <br>

    <?php
        echo form_open('cFormulario/mostrarDatos');
    ?>
    Nombre
    <input type="text" name="txtnom" id="txtnom" value="<?php echo set_value('texnom', '');?>"/>
    <br>
    Edad
    <input type="text" name="txted" id="txted" />
    <br>
    Correo
    <input type="text" name="txtmail" id="txtmail" value="<?php echo set_value('texmail', '');?>"/>
    <br>
    <input type="submit" name="Mostrar datos" />
    <input type="reset" name="Restablecer" />   

    <?php
        echo form_close();
    ?>

    <br>
    <div id="resultado">
        <?php
            if(!empty($nombre) && !empty($edad) && !empty($correo)){

                echo "Nombre: $nombre<br>";
                echo "Edad: $edad<br>";
                echo "Correo: $correo<br>";
            }
        ?>
    </div>

</body>

Weird thing is, it was working fine while I was typing it. But when I tried to show some results, the error came.

1
  • For starters you have a typo in min_lenght. It should be spelt as min_length Commented Sep 7, 2018 at 9:20

1 Answer 1

2

Yeah, you can add form_error("field_name") next to your field to get an error message.

for ex-

     <?php
        echo form_open('cFormulario/mostrarDatos');
    ?>
    Nombre
    <input type="text" name="txtnom" id="txtnom" value="<?php echo set_value('texnom', '');?>"/>
    <br>
    Edad
    <input type="text" name="txted" id="txted" />
<?php echo form_error('txted'); ?>
    <br>
    Correo
    <input type="text" name="txtmail" id="txtmail" value="<?php echo set_value('texmail', '');?>"/>
<?php echo form_error('txtmail'); ?>
    <br>
    <input type="submit" name="Mostrar datos" />
    <input type="reset" name="Restablecer" />   

    <?php
        echo form_close();
    ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.