Hi guys i have simple problem that i can't seem to solve and has been frustrating me for some time i am a newbie when it comes to Codeigniter so be kind hahah, okay so on to my problem i have form validation on a subscription form that seems to not be working

Here is my controller function:

    public function add_email()
    {
       $this->load->library('form_validation');
       $this->form_validation->set_rules('name', 'Name', 'trim|required');
       $this->form_validation->set_rules('email', 'Email Address', 'trim|required|xss_clean');
       if ($_POST AND $this->form_validation->run() == false)
       {
           redirect(base_url());
       } 
       else
       {
           $data = array(
               'name' => $this->input->post('name'),
               'email' => $this->input->post('email')
           );
           $this->Newsletter->subscribe($data);
        }
    }

And The view:

<?= validation_errors() ?>
<?php form_open('Newsletters/add_email'); ?>
<input type="text" class="form-control" autofocus="true" id="name" name="name" placeholder="Your Name">
<input type="email" class="form-control" id="email" name="email" placeholder="Email address">
<input class="btn btn-primary btn-block" type="submit" value="SIGN ME UP" id="subscribe">
<?php form_close(); ?>

So when i want to test if validation is working and i press sign up nothing happens and no validations errors show! So what am i doing wrong?

try,

public function add_email()
{
    /* set errors var as array */
   $errors = array();

   $this->load->library('form_validation');

   $this->form_validation->set_rules('name', 'Name', 'trim|required');
   $this->form_validation->set_rules('email', 'Email Address', 'trim|required|xss_clean');

   /* check if any of the validations fails */
   if ($this->form_validation->run() == false) {
       /* take all of the validation errors as errors */
        $errors['errors'] = $this->form_validation->set_err();

        /* assign the data to your view file */
        $this->load->view('NAME_OF_YOUR_VIEW_FILE', $errors);

    }

on you view file, you can show the errors by adding these next to the form elements.

 <!-- error for name -->
 <?php echo (isset($errors['name']) ? $errors['name'] : ''); ?>

 <!-- error for email -->
 <?php echo(isset($errors['email']) ? $errors['email'] : ''); ?>

Try avoiding the use of PHP echo shorthand. Not all php.ini file have this enabled by default. Besides, in enterprise working environment, they would not allow it. It is nice to develope a good habit earlier than correcting a bad habit that has been going on for years.

Although CI would never twist your arms to have a model, it is always nice to completely follow the M-V-C patterns when writing an application. By doing this, you will not have any difficulties in transitioning between different frameworks.

A good way to remember the M-V-C pattern is to put all data processor in the model and grab the processed data in the controller and then passed it to the view..

good luck..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.