CodeIgniter Forums
Loading the form and validating in the same function? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Loading the form and validating in the same function? (/thread-30015.html)

Pages: 1 2


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]parham90[/eluser]
Hi,

It's the first time I am doing a function like this (loading of the form and validating in the same function), so I have to paste the whole function; sorry for the long post. But the form doesn't validate; the form just reloads, with no fields repopulated, even though I have used

Code:
<?=form_input('name', set_value('name'))?>

The function follows:

Code:
function contact()
{
$this->load->library('Recaptcha');
$this->load->library('form_validation');
$this->load->helper('form');
$this->lang->load('recaptcha');
if ($this->form_validation->run())
$this->load->view('success.php');
else
{
$this->form_validation->set_rules('email', 'email', 'required|valid_email');
$this->form_validation->set_rules('name', 'name', 'required|xss_clean');
$this->form_validation->set_rules('message', 'message', 'required|xss_clean');
$this->form_validation->set_rules('phone', 'phone', 'required|numeric');
$this->form_validation->set_rules('phone', 'phone', 'required|numeric');
$this->form_validation->set_rules('recaptcha_response_field', 'lang:recaptcha_field_name', 'required|callback_check_captcha');
$data['captcha'] = $this->recaptcha->get_html();
$this->load->view('contact', $data);
}
}

Thanks!


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]mattpointblank[/eluser]
Does your form definitely submit to contact(), is it set to method="post", and does your view output the validation errors?


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]parham90[/eluser]
Hi,

Yes, I've checked the HTML source, and it is being sent to "/welcome/contact", and the method is post. There is a line in my view that reads:

Code:
<?=validation_errors()?>

after which is a <br/> tag. The new line tag is there, but not any messages before it, and the value attributes of inputs, although present, are set to "".


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]St0neyx[/eluser]
[quote author="parham90" date="1272571069"]
Code:
&lt;?=validation_errors()?&gt;
[/quote]

Maybe to simple, but did you enable php shorttags?

default:
Code:
$config['rewrite_short_tags'] = FALSE;
Should be:
Code:
$config['rewrite_short_tags'] = TRUE;



Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]parham90[/eluser]
Yep, I've enabled that before. Thanks for the reminder though. Smile


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]mattpointblank[/eluser]
Try swapping your controller code to this:

Code:
if ($this->form_validation->run() == FALSE)
    $this->form_validation->set_rules('email', 'email', 'required|valid_email');
    $this->form_validation->set_rules('name', 'name', 'required|xss_clean');
    $this->form_validation->set_rules('message', 'message', 'required|xss_clean');
    $this->form_validation->set_rules('phone', 'phone', 'required|numeric');
    $this->form_validation->set_rules('phone', 'phone', 'required|numeric');
    $this->form_validation->set_rules('recaptcha_response_field', 'lang:recaptcha_field_name', 'required|callback_check_captcha');
    $data['captcha'] = $this->recaptcha->get_html();
    $this->load->view('contact', $data);
} else {
    $this->load->view('success.php');
}



Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]parham90[/eluser]
I tried that. I even thought maybe setting the $data['captcha'] array is doing it, but even removing that didn't work. I guess something is resetting the post data or the validation... no idea.


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]mattpointblank[/eluser]
Could you post your view?


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]parham90[/eluser]
Of course. Please note that in an effort to fix the problem (which didn't work anyway), I moved the code that got the recaptcha HTML to the view, rather than the controller. So, don't worry that the $this->recaptcha->get_html() line is being repeated twice; it is not. The view code follows:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;Title&gt;Contact Us&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Contact Us</h1>
&lt;?=validation_errors()?&gt;<br/>
You can use this page to send us any comments, questions, suggestions or requests. Entering an email is required, as it will allow us to get back to you on your query.<br/>
&lt;?=validation_errors()?&gt;<br/>
&lt;?=form_open('welcome/contact')?&gt;
&lt;?=form_label('Email Address (Required)', 'email')?&gt;
&lt;?=form_input('Email', set_value('email'))?&gt;<br/>
&lt;?=form_label('Name (required)', 'name')?&gt;
&lt;?=form_input('name', set_value('name'))?&gt;<br/>
&lt;?=form_label('Message (required)', 'message')?&gt;
&lt;?=form_input('message', set_value('message'))?&gt;<br/>
&lt;?=form_label('Phone number (optional)', 'phone')?&gt;
&lt;?=form_input('phone', set_value('phone'))?&gt;<br/>
&lt;?=$this->recaptcha->get_html()?&gt;<br/>
&lt;?=form_submit('submit', 'Send')?&gt;
&lt;?=form_close()?&gt;
&lt;/body&gt;
&lt;/html&gt;

Thanks again for all your help.


Loading the form and validating in the same function? - El Forum - 04-29-2010

[eluser]mattpointblank[/eluser]
Doh, just realised what was wrong. In your controller, you're defining your validation rules AFTER checking to see if it passed them! It'll never pass!

Move all your $this->form_validation->set_rules() calls to before checking if ($this->form_validation->run())