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

[eluser]Unknown[/eluser]
Hello!

I would like to know how can I implement form validation without making another view for the form. My working code is the one shown in the video tutorial from CI's website (the blog). I would like to validate the comment adding form. Here is the code for comments_view.php and here is the one for blog.php

Any suggestions?

Thanks in advance.
#2

[eluser]gtech[/eluser]
hey have you checked out the validation documentation?

[url="http://ellislab.com/codeigniter/user-guide/libraries/validation.html"](click here)[/url]

its quite well documented, I think what your asking is that you want to redisplay the same form view if it is not validated correctly and display the error messages, if thats the case then there are a few examples in the validation documentation.
#3

[eluser]Michael Wales[/eluser]
Make your form action the same method that displays the form. Place all of your validation in that form and then use the run() method of the Validation class to determine whether you need to perform a database insert or not.

Code:
if ($this->validation->run()) {
  // Form was submitted and validated - insert content
  // I like to do a redirect after database queries (so a refresh doesn't submit the data again)
} else {
  // Form wasn't submitted - or it doesn't validate
  // Load a view file
}
#4

[eluser]Unknown[/eluser]
Can you guys give me an example using the code I have provided at pastebin? I don't seem to get it working Sad
#5

[eluser]Michael Wales[/eluser]
I'll just write up some code here - just because I'm to drunk to go clicking on links (is it Drunken tut time?).


Controller
Code:
function login() {
  $this->load->library('validation');
  $rules['username'] = 'trim|required|max_length[40]';
  $rules['password'] = 'trim|required|max_length[20]';
  $this->validation->set_rules($rules);
  $fields['username'] = 'username';
  $fields['password'] = 'password';
  $this->validation->set_fields($fields);

  if ($this->validation->run()) {
    // Login the user by setting his session information
    redirect('admin/cpanel');
  } else {
    $this->load->view('admin/login');
  }
}
#6

[eluser]Chris Newton[/eluser]
I posted some validation stuff into your code at your collaboration site. I'll echo what gtech mentioned though... the docs are pretty detailed in this particular area.




Theme © iAndrew 2016 - Forum software by © MyBB