Welcome Guest, Not a member yet? Register   Sign In
Version 1.7.3 Form Validation
#1

[eluser]NGiannini[/eluser]
Due to server constraints I'm using CodeIgniter 1.7.3 and am having issues with my form validation. The form submits and should return to itself displaying the validation errors however it just displays the form. I've tried replacing my code with a "pass" or "fail" message however if the form validates or not it always displays "fail" with no errors.

I'm running another website on the same server and it's form validation works perfectly fine. I've compared my code and don't seem to see anything wrong however I may just be staring at this for too long. If anyone sees any mistakes or has any suggestions it would be greatly appreciated.

Controller: application.php
Code:
<?php
class Application extends Controller {
  function Application() {
   parent::Controller();
  }
  
  function index() {
   $data['main_content'] = 'application_form';
   $this->load->view('includes/template.php', $data);
  }
  
  function apply() {
   $this->load->library('form_validation');
   $this->form_validation->set_rules('fname', 'First Name', 'trim|required');
   $this->form_validation->set_rules('mname', 'Middle Name', 'trim|required');
   $this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
   $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
   $this->form_validation->set_rules('phone', 'Phone', 'trim|required|callback_valid_phone');
   $this->form_validation->set_rules('address', 'Street Address', 'trim|required');
   $this->form_validation->set_rules('city', 'City', 'trim|required');
   $this->form_validation->set_rules('state', 'State', 'trim|required|exact_length[2]');
   $this->form_validation->set_rules('zip', 'Zip Code', 'trim|required|exact_length[5]');
  
   if($this->form_validation->run() == FALSE) {
    $data['main_content'] = 'application_form';
    $this->load->view('includes/template.php', $data);
   }
   else {
    echo "Passed";
   }
  }
  
  function valid_phone($phone) {
   $phone = trim($phone);
   if ($phone == '') {
    return false;
   }
   else {
    if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $phone)) {
     return preg_replace('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', '($1) $2-$3', $phone);
    }
    else {
     $this->form_validation->set_message('valid_phone', 'Please enter a valid phone number.');
              return false;
    }
   }
  }
}
?>

View: application_form.php
Code:
<?php
echo validation_errors('<p class="error">');
echo form_open_multipart('application/apply');
?&gt;
<fieldset>
  <legend>Personal Information</legend>
&lt;?php
echo form_label('First Name', 'fname');
echo form_input('fname', set_value('fname'));
echo form_label('Middle Name', 'mname');
echo form_input('mname', set_value('mname'));
echo form_label('Last Name', 'lname');
echo form_input('lname', set_value('lname'));
echo form_label('Email Address', 'email');
echo form_input('email', set_value('email'));
echo form_label('Phone', 'phone');
echo form_input('phone', set_value('phone'));
echo form_label('Street Address', 'address');
echo form_input('address', set_value('address'));
echo form_label('City', 'city');
echo form_input('city', set_value('city'));
echo form_label('State', 'state');
$states = array(
  '' => '',
  'AL'=>"Alabama",
     'AK'=>"Alaska",
     'AZ'=>"Arizona",
     'AR'=>"Arkansas",
     'CA'=>"California",
     'CO'=>"Colorado",
     'CT'=>"Connecticut",
     'DE'=>"Delaware",
     'DC'=>"District Of Columbia",
     'FL'=>"Florida",
     'GA'=>"Georgia",
     'HI'=>"Hawaii",
     'ID'=>"Idaho",
     'IL'=>"Illinois",
     'IN'=>"Indiana",
     'IA'=>"Iowa",
     'KS'=>"Kansas",
     'KY'=>"Kentucky",
     'LA'=>"Louisiana",
     'ME'=>"Maine",
     'MD'=>"Maryland",
     'MA'=>"Massachusetts",
     'MI'=>"Michigan",
     'MN'=>"Minnesota",
     'MS'=>"Mississippi",
     'MO'=>"Missouri",
     'MT'=>"Montana",
     'NE'=>"Nebraska",
     'NV'=>"Nevada",
     'NH'=>"New Hampshire",
     'NJ'=>"New Jersey",
     'NM'=>"New Mexico",
     'NY'=>"New York",
     'NC'=>"North Carolina",
     'ND'=>"North Dakota",
     'OH'=>"Ohio",
     'OK'=>"Oklahoma",
     'OR'=>"Oregon",
     'PA'=>"Pennsylvania",
     'RI'=>"Rhode Island",
     'SC'=>"South Carolina",
     'SD'=>"South Dakota",
     'TN'=>"Tennessee",
     'TX'=>"Texas",
     'UT'=>"Utah",
     'VT'=>"Vermont",
     'VA'=>"Virginia",
     'WA'=>"Washington",
     'WV'=>"West Virginia",
     'WI'=>"Wisconsin",
     'WY'=>"Wyoming"
    );
echo form_dropdown('state', $states, set_value('state'));
echo form_label('Zip Code', 'zip');
echo form_input('zip', set_value('zip'));
?&gt;
</fieldset>
&lt;?php
echo form_submit('submit', 'Submit Application');
echo form_close();
?&gt;
#2

[eluser]Marc Arbour[/eluser]
Hello.

The user_guide says
Code:
&lt;?php echo validation_errors(); ?&gt;
and you are using
Code:
echo validation_errors('<p class="error">');

That might have something to do with it?
#3

[eluser]NGiannini[/eluser]
The parameter just wraps the errors in that particular tag so it shouldn't be a problem. I tried it without the parameter and it still doesn't work.

Thanks for trying to help though!
#4

[eluser]NGiannini[/eluser]
/bump still can't figure out whats wrong.
#5

[eluser]Marc Arbour[/eluser]
I got it.

You have several errors.

here is your application_form
Code:
&lt;?php
echo validation_errors('');
echo form_open_multipart($this->uri->uri_string);
?&gt;
<fieldset>
  <legend>Personal Information</legend>
&lt;?php
echo form_label('First Name', 'fname');
echo form_input('fname', set_value('fname'));
echo form_label('Middle Name', 'mname');
echo form_input('mname', set_value('mname'));
echo form_label('Last Name', 'lname');
echo form_input('lname', set_value('lname'));
echo form_label('Email Address', 'email');
echo form_input('email', set_value('email'));
echo form_label('Phone', 'phone');
echo form_input('phone', set_value('phone'));
echo form_label('Street Address', 'address');
echo form_input('address', set_value('address'));
echo form_label('City', 'city');
echo form_input('city', set_value('city'));
echo form_label('State', 'state');
$states = array(
  '' => '',
  'AL'=>"Alabama",
     'AK'=>"Alaska",
     'AZ'=>"Arizona",
     'AR'=>"Arkansas",
     'CA'=>"California",
     'CO'=>"Colorado",
     'CT'=>"Connecticut",
     'DE'=>"Delaware",
     'DC'=>"District Of Columbia",
     'FL'=>"Florida",
     'GA'=>"Georgia",
     'HI'=>"Hawaii",
     'ID'=>"Idaho",
     'IL'=>"Illinois",
     'IN'=>"Indiana",
     'IA'=>"Iowa",
     'KS'=>"Kansas",
     'KY'=>"Kentucky",
     'LA'=>"Louisiana",
     'ME'=>"Maine",
     'MD'=>"Maryland",
     'MA'=>"Massachusetts",
     'MI'=>"Michigan",
     'MN'=>"Minnesota",
     'MS'=>"Mississippi",
     'MO'=>"Missouri",
     'MT'=>"Montana",
     'NE'=>"Nebraska",
     'NV'=>"Nevada",
     'NH'=>"New Hampshire",
     'NJ'=>"New Jersey",
     'NM'=>"New Mexico",
     'NY'=>"New York",
     'NC'=>"North Carolina",
     'ND'=>"North Dakota",
     'OH'=>"Ohio",
     'OK'=>"Oklahoma",
     'OR'=>"Oregon",
     'PA'=>"Pennsylvania",
     'RI'=>"Rhode Island",
     'SC'=>"South Carolina",
     'SD'=>"South Dakota",
     'TN'=>"Tennessee",
     'TX'=>"Texas",
     'UT'=>"Utah",
     'VT'=>"Vermont",
     'VA'=>"Virginia",
     'WA'=>"Washington",
     'WV'=>"West Virginia",
     'WI'=>"Wisconsin",
     'WY'=>"Wyoming"
    );
echo form_dropdown('state', $states, set_value('state'));
echo form_label('Zip Code', 'zip');
echo form_input('zip', set_value('zip'));
?&gt;
</fieldset>
&lt;?php
echo form_submit('submit', 'Submit Application');
echo form_close();
?&gt;
I removed
Code:
<p class="error">
in your
Quote: echo validation_errors('');
function and added
Quote:$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
in the controller instead
and your controller Test for which I have not tested the phone callback.
Quote:&lt;?php
class Test extends Controller {
function index() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('fname', 'First Name', 'trim|required');
$this->form_validation->set_rules('mname', 'Middle Name', 'trim|required');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('phone', 'Phone', 'trim|required|callback_valid_phone');
$this->form_validation->set_rules('address', 'Street Address', 'trim|required');
$this->form_validation->set_rules('city', 'City', 'trim|required');
$this->form_validation->set_rules('state', 'State', 'trim|required|exact_length[2]');
$this->form_validation->set_rules('zip', 'Zip Code', 'trim|required|exact_length[5]');

if($this->form_validation->run() == FALSE) {
$this->load->view('application_form', $data);
}
else {
echo "Passed";
}

}


function valid_phone($phone) {
$phone = trim($phone);
if ($phone == '') {
return false;
}
else {
if (preg_match('/^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/', $phone)) {
return preg_replace('/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/', '($1) $2-$3', $phone);
}
else {
$this->form_validation->set_message('valid_phone', 'Please enter a valid phone number.');
return false;
}
}
}
}
?&gt;

Notice your controller is renamed because you used a reserved name. Would never have worked.

I also changes the submit url of the form to itself because your validation data should be in the index function not in the apply function.

I have also removed
Code:
$data['main_content'] = 'application_form';
because it was useless

And finally, I put
Code:
$this->load->view('application_form', $data);
instead of
Code:
$this->load->view('includes/template.php', $data);
When you load a view, remove the ".php" for CI takes care of business.

see your working code at http://www.vitevite.org/test for the next couple of minutes.
Try that.

Hope it helps.
#6

[eluser]CroNiX[/eluser]
Try removing the callback_valid_phone from your rules and see if it works.

Also, if you are going to use validation_errors() and enter an opening tag, you need to add the closing tag as well. Not sure if that affects this problem.

Code:
validation_errors('<p class="error">', '</p>');
#7

[eluser]InsiteFX[/eluser]
Application is not a reserved word you can use it for a controller class name.
#8

[eluser]NGiannini[/eluser]
Unfortunately it is still not working properly even with the changes.

Code:
validation_errors('<p class="error">', '</p>');


Adding the '&lt;/p&gt;' parameter actually doesn't matter because '&lt;p&gt;' and '&lt;/p&gt;' are the default delimiters. The only change is adding the class 'error'.

Code:
$data['main_content'] = 'application_form';
$this->load->view('includes/template', $data);

I removed the .php extension here but this code actually loads the form into a template file to abstract the main content from the rest of the page. Also, I use the apply() function because I do not want the form to try validating without being submitted first.

What confuses me the most is the fact that I have another application on the same server running CodeIgniter 1.7.3 and its form validation works perfectly fine despite the two having similar code. I've noticed no differences that would cause this form validation not to work.

I appreciate the help so far.
#9

[eluser]InsiteFX[/eluser]
Try re-uploading maybe you have corrupted files on that server...
#10

[eluser]NGiannini[/eluser]
Ok so I tried re-uploading all the files to no avail, however I think I may have isolated the problem.


I added echo statements inside of the Form_validation.php library and it seems to break right when the run() function begins and checks to see if anything has been posted.

Code:
function run($group = '')
{
  // Do we even have any data to process?  Mm?
  if (count($_POST) == 0)
  {
   echo "Break 1: no POST DATA";
   return FALSE;
  }

So apparently its not seeing the POST data that is being sent. I used Liveheaders addon for Firefox and can clearly see that the POST data is going through. Any thoughts on why CodeIgniter wouldn't be seeing the POST data?




Theme © iAndrew 2016 - Forum software by © MyBB