Welcome Guest, Not a member yet? Register   Sign In
CI not refilling form fields
#1

[eluser]PeterB[/eluser]
Hi,

I've just started with CI (have to write PHP4 code for a project... boo) and have been trying to get form validation to work, on a componentised basis. My controller looks like:

Code:
class Signup extends Controller {

    function __construct()
    {
        parent::Controller();
        #if (!$this->site_sentry->is_logged_in()) {redirect('login/', 'location');exit;}
        $this->load->helper('date');
        $this->load->helper('form');
        $this->load->library('validation');
        $this->load->model('subscribers_model');
        $this->load->model('plans_model');
    }

    function index()
    {


        $data['plans'] = $this->plans_model->getAll(); // activate the option


        $data['form_details'] = $this->load->view('subscribers/_form_details', '', true);
        $this->load->view('subscribers/_form_details_validation.php', '', FALSE);
        
        $data['form_paymentplan'] = $this->load->view('signup/_form_paymentplan', $data, true);
        $this->load->view('signup/_form_paymentplan_validation.php', '', FALSE);
        
        
        if ($this->validation->run() == FALSE) {
            $this->load->view('signup/signup', $data);
        } else {
            // Breaks MVC as this should be encapsulated int he Subscirbers model?
            $newSubscriber = array(
                'firstname' => htmlspecialchars($this->input->post('firstname'), ENT_QUOTES),
                'lastname' => htmlspecialchars($this->input->post('lastname'), ENT_QUOTES),
                'email' => htmlspecialchars($this->input->post('email'), ENT_QUOTES),
                'password' => htmlspecialchars($this->input->post('password'), ENT_QUOTES),
                'status' => 'pending'
            );
            // make insertion, grab insert_id
            if ($this->db->insert('subscribers', $newSubscriber)) {
                $subscriber_id = array('subscriber_id'  =>  $this->db->insert_id());
                $this->session->set_userdata($subscriber_id);
            } else {
                die ($this->lang->line('error_problem_inserting'));
            }
            redirect('signup/payment/', 'location');
        }
        #$this->load->view('signup/signup', $data);
    }
}?>

_form_details looks like
Code:
<p>
    <label for="firstname">&lt;?php echo $this->lang->line('subscriber_firstname'); ?&gt;</label>
    &lt;input type="text" name="firstname" value="&lt;?php echo $this-&gt;validation->firstname; ?&gt;">
</p>
<p>
    <label for="lastname">&lt;?php echo $this->lang->line('subscriber_lastname'); ?&gt;</label>
    &lt;input type="text" name="lastname" value="&lt;?php echo $this-&gt;validation->lastname; ?&gt;">
</p>
...

And the validation code in _form_details_validation is
Code:
&lt;?php
$rules['firstname'] = 'trim|required|max_length[50]';
$rules['lastname'] = 'trim|required|max_length[50]';
$rules['email'] = 'trim|required|max_length[100]|valid_email';
$rules['password'] = 'trim|required|matches[password_confirm]|md5';
$rules['password_confirm'] = 'trim|required';
$this->validation->set_rules($rules);

$fields['firstname'] =  $this->lang->line('subscriber_firstname');
$fields['lastname'] =  $this->lang->line('subscriber_lastname');
$fields['email'] =  $this->lang->line('subscriber_email');
$fields['password'] =  $this->lang->line('subscriber_password');
$fields['password'] =  $this->lang->line('subscriber_password_confirm');
$this->validation->set_fields($fields);

$this->validation->set_error_delimiters('<p class="error">', '</p>');

Now... when I run it the form displays properly, and I can submit the form no problem (yes it goes back to the same method in the controller, I've checked). It even validates the data and displays error messages.

However... the specified $fields values aren't used in place of the field names, and it won't repopulate the form with data. This is using CodeIgniter 2.5.4, following the documentation that came with it. Is there a known problem, or have I done something silly?

Thanks,
Peter
#2

[eluser]Michael Wales[/eluser]
Well I'd say loading your rules/fields as a view is a bit silly, but nonetheless it shouldn't matter.

Just for grins, have you tried pulling the code out of _form_details_validation and placing it within your controller - just to see if that makes a difference? I honestly don't think it should (since your rules are obviously working), but who knows?
#3

[eluser]PeterB[/eluser]
I agree it's a silly place to do it, I can't however figure out a more sensible place! This form and the associated rules will be used in 3 controllers, so they need to be kept separated.

I have tried with them in the controller - and the fields are not autofilled.
#4

[eluser]Michael Wales[/eluser]
Maybe it's because you are defining the password field twice? You would think the Validation class would just overwrite the first definition with the second, but maybe it freaks out and refuses to work.

Change that line to:
Code:
$fields['password_confirm'] =  $this->lang->line('subscriber_password_confirm');
#5

[eluser]Colin Williams[/eluser]
Quote:I agree it’s a silly place to do it, I can’t however figure out a more sensible place!
I like to use a validation config file.
Code:
$rules = $this->config->item('user_register_validation');
#6

[eluser]Jim OHalloran[/eluser]
I recently had the same problem with the Validation library under PHP4, everything worked, but the field values weren't available via the validation class. I was autoloading the validation class, as soon as I loaded the validation library in the controller method itself it worked fine.

Jim.
#7

[eluser]Jim OHalloran[/eluser]
[quote author="walesmd" date="1189633227"]Maybe it's because you are defining the password field twice? You would think the Validation class would just overwrite the first definition with the second, but maybe it freaks out and refuses to work.[/quote]
It's not possible to have two array elements with the same key, so the second assignment will simply replace the first. The validation class won't see the original value.
[quote author="walesmd" date="1189633227"]Change that line to:
Code:
$fields['password_confirm'] =  $this->lang->line('subscriber_password_confirm');
[/quote]
Definitely.

Jim.
#8

[eluser]bijon[/eluser]
well can you tell which php4 version you are running. Because i saw in this thread
http://ellislab.com/forums/viewthread/47377/ validation does not work on php4.4.4.
So can you help them to run validation in php4.

thanks




Theme © iAndrew 2016 - Forum software by © MyBB