Welcome Guest, Not a member yet? Register   Sign In
Form validation problem
#1

[eluser]Boris Strahija[/eluser]
Okay, it's pretty late here so this might be a stupid question Smile

I'm using the Form Generation library (http://ellislab.com/forums/viewthread/107861/) for my forms, but I think this is a general question about libraries in CI.
So here's my complicated situation:
- I have a CMS library that handles request from the CMS. This is setup so that I have 1 controller named 'Krustr', and this controller is loading a library called 'Front'
- The index() method in the 'Krustr' controller is just calling the route_request() method from the 'Front' library
- Now the problem is that the validation isn't working when I setup a form in the library class, unless I manually define the rules like this:
Code:
$this->ci->form_validation->set_rules('email', 'email', 'required|valid_email');

So this is my code that isn't working:
Code:
$subscription_form = new Form();
$subscription_form->open(current_url())
    ->fieldset()
    ->text('name',     'ime',         'required')
    ->text('email', 'email',     'required|valid_email')
    ->html('<p class="btn">')->submit('Prijavi se!')->html('</p>');

Does anyone know what could be the problem?
#2

[eluser]skunkbad[/eluser]
The form validation rules don't belong in the form, which is where it looks like you are putting them. I've got many working examples of form validation in my Community Cart application, and here is one of them:

There are at least a couple examples in this controller:

http://bitbucket.org/skunkbad/community-...eckout.php
#3

[eluser]Boris Strahija[/eluser]
I know how validation works, the code I've sent isn't in a view, it's a controller.
This library is just a quick way of generating forms in a controller. The code it generates is then sent to the controller.

I think the problem is that the form library is used inside another library and this part isn't working correctly inside the form library:
Code:
$CI =& get_instance();

This is the code for my Newsletter library:
Code:
&lt;?php  if (!defined('BASEPATH')) exit('No direct script access allowed');    
    
    /**
     * Newsletter Library
     *
     */
    
    class Newsletter {
        
        protected $ci;
        
        /* ------------------------------------------------------------------------------------------ */
        
        /**
         *
         */
        function __construct()
        {
            $this->ci =& get_instance();
            
            $this->ci->load->model('user');
            $this->ci->load->model('newsletter_model');
            $this->ci->load->library('form');
            
        } //end __contruct()
        
        
        /* ------------------------------------------------------------------------------------------ */
        
        /**
         *
         */
        function subscription_form()
        {
            $subscription_form = new Form();
            $subscription_form->open(current_url())
                    ->fieldset()
                    ->text('name',     'ime',         'required')
                    ->text('email', 'email',     'required|valid_email')
                    ->html('<p class="btn">')->submit('Prijavi se!')->html('</p>');
    
            if ($this->ci->form_validation->run() == FALSE) :
                $data = array(
                     'subscription_form'     => $subscription_form->get()
                    ,'subscription_errors'     => $subscription_form->errors
                );
                
                return $data;
                
            else :
                // Subscribe
                $this->subscribe(
                     $this->input->post('email')
                    ,$this->input->post('category')
                    ,$this->input->post('name')
                );
                
            endif;
            
        } //end subscription_form()
        
        
        /* ------------------------------------------------------------------------------------------ */
        
        /**
         * Subscribe to one or multiple categories
         *
         */
        function subscribe($email = NULL, $category_id = NULL, $fullname = NULL)
        {
            
        } //end subscribe()
        
        
        /* ------------------------------------------------------------------------------------------ */
        
    } //end Newsletter


/* End of file newsletter.php */
/* Location: ./app/libraries/newsletter.php */
#4

[eluser]Boris Strahija[/eluser]
Just a quick note, the validation kinda works because $subscription_form->errors returns no errors. The only part that does not work ist this:
Code:
$this->ci->form_validation->run()
It always returns false.




Theme © iAndrew 2016 - Forum software by © MyBB