I've got some strange behavior with validation. I'm loading up a form to allow editing of an existing database entry. If I make my changes to the fields, enter in the required "revision comments" and submit, the page does NOT post, it seems to only refresh and empty out all the entries in each of the fields.
Now, on the other hand if I open the form to make changes to a
database entry and immediately press "Submit" without filling in the
revision comments field, it fails validation and tells me to enter
comments. Now if I fill in comments to satisfy all the requirements
and press submit, it posts and I get my "Successful Entry"
confirmation prompt.
I think I've narrowed it down to the remote: part of my jQuery. I
eliminated by trial-and-error the other pieces of the jQuery code and
it seems that when I remove the remote: it does not experience this
strange behviour.
Any ideas? Does the code look correct?
jQuery Code:
[code]
// Form Validation customization
$("#Form_business").validate({
rules: {
bus_zip: {
required: true,
//digits: true,
minlength: 5
},
bus_extension: {
digits: true
},
bus_number: {
required: true,
remote: {
url:
"/rtui/codeigniter/index.php/business_form/dupeCheck",
type: "post",
data: {
action: function() {
return
$("#action").val();
},
bus_id: function() {
return
$("#bus_id").val();
}
}
}
}
},
messages: {
bus_zip: {
required: "This field is required.",
//digits: "Numbers only",
minlength: jQuery.format("{0} digits
required")
},
bus_extension: {
digits: "Numbers only"
},
bus_number: {
required: "Advertiser number required",
remote: jQuery.format("Duplicate
customer number")
}
}
});
[/code]
HTML Code:
[code]
<?=form_open('business_form/newbusiness', $form_attributes); ?>
<fieldset>
<legend><?=$action; ?> Customer</legend>
<ol>
<input type="hidden" name="action"
id="action" value="<?=
$action; ?>" />
<input type="hidden" name="employee_id"
value="<?= $employee_id; ?
>" />
<?php
if ($action == 'Edit') : ?>
<input type="hidden"
name="bus_id" id="bus_id" value="<?=
$bus_id; ?>" />
<?php endif; ?>
* denotes required fields
<li>
<label for="bus_name">Customer Name</label>
<em>*</em><input type="text" name="bus_name"
size="36" value="<?= $bus_name; ?>" class="required" />
</li>
<li>
<label for="bus_number">Customer Number</label>
<em>*</em><input type="text" id="bus_number"
name="bus_number" size="24" value="<?= $bus_number; ?>"
class="required" />
</li>
<li>
<label for="bus_address">Address 1</label>
<em>*</em><input type="text" name="bus_address"
size="24" value="<?= $bus_address; ?>" class="required" />
</li>
<li>
<label for="bus_city">City</label>
<em>*</em><input type="text"
name="bus_city" size="24" value="<?=
$bus_city; ?>" class="required" />
</li>
<li>
<label for="bus_state">State</label>
<em>*</em><select name="bus_state"
style="width:150px;"
class="required">
<?php
if ($action == 'New')
{
echo '<option value=""
selected />';
}
elseif ($action == 'Edit')
{
echo '<option
value="'.$bus_state.'" selected>'.$state_name.'</
option>';
}
//standard listing of states in
database
foreach($states as $row)
{
echo '<option
value="'.$row['state_abbreviation'].'">'.$row
['state_name'].'</option>';
}
?>
</select>
</li>
<li>
<label for="bus_zip">Zip</label>
<em>*</em><input type="text"
name="bus_zip" size="12" value="<?=
$bus_zip; ?>" />
</li>
<li>
<label for="bus_contact">Contact
Name</label>
<em>*</em><input type="text"
name="bus_contact" size="36"
value="<?= $bus_contact; ?>" class="required" />
</li>
<li>
<label for="bus_phone">Phone
Number</label>
<em>*</em><input type="text"
name="bus_phone" size="16" value="<?
= $bus_phone; ?>" id="phone1" class="required" />
</li>
<li>
<label for="bus_extension">Phone
Extension</label>
<em> </em><input type="text"
name="bus_extension" size="16"
value="<?= $bus_extension; ?>" id="phone4" />
</li>
<li>
<label for="bus_mobile">Mobile
Number</label>
<em> </em><input type="text"
name="bus_mobile" size="16"
value="<?= $bus_mobile; ?>" id="phone2" />
</li>
<li>
<label for="bus_fax">Fax Number</label>
<em> </em><input type="text"
name="bus_fax" size="16"
value="<?= $bus_fax; ?>" id="phone3" />
</li>
<?php
if ($action == 'Edit') : ?>
<li>
<label for="bus_rev_changes">Revision Memo</label>
<em>*</em><textarea
name="bus_rev_changes" cols="36" rows="4"
class="required"></textarea>
</li>
<?php endif; ?>
<li>
<label for="submit"></label>
<input type="submit" name="submitForm"
value="Submit" />
<input type="reset" value="Clear" />
</li>
</ol>
</fieldset>
</form>
[/code]
PHP Code
[code]
function newBusiness()
{
$data['form_attributes'] = array('class' => 'rtuiform', 'id' =>
'Form_business');
$data['states'] = $this->states->getAllStates();
$data['category'] = $this->top20->getAllTop20();
$data['reps'] = $this->user->getSalesReps();
$data['employees'] = $this->user->getUserExecs();
// If form is submitted - insert data to database
if ($this->input->post('submitForm'))
{
$action = $this->input->post('action',TRUE);
$_POST['bus_number'] = strtoupper($this->input->post
('bus_number',TRUE));
$_POST['bus_name'] =
ucwords(strtolower($this->input->post
('bus_name',TRUE)));
$_POST['bus_corporate'] =
ucwords(strtolower($this->input->post
('bus_corporate',TRUE)));
$_POST['bus_address'] =
ucwords(strtolower($this->input->post
('bus_address',TRUE)));
$_POST['bus_address2'] =
ucwords(strtolower($this->input->post
('bus_address2',TRUE)));
$_POST['bus_city'] =
ucwords(strtolower($this->input->post
('bus_city',TRUE)));
$_POST['bus_contact'] =
ucwords(strtolower($this->input->post
('bus_contact',TRUE)));
$_POST['bus_email'] =
ucwords(strtolower($this->input->post
('bus_email',TRUE)));
// If form input for NEW business successfully validates
if ($action == 'New')
{
$bus_number =
$this->input->post('bus_number',TRUE);
// Check for duplicate Business numbers
if ($this->rtui_app->dupe_check('bus',
$bus_number))
{
$inputSchema = array(
'bus_name' => '',
'bus_number' => '',
'bus_address' => '',
'bus_city' => '',
'bus_state' => '',
'bus_zip' => '',
'bus_contact' => '',
'bus_phone' => '',
'bus_extension' => '',
'bus_mobile' => '',
'bus_fax' => '',
'bus_email' => '',
'cat_id' => '',
'employee_rep' => '',
'bus_restricted' => ''
);
$inputData =
array_intersect_key($_POST, $inputSchema);
$this->business->insertNewBusiness($inputData);
$this->rtui_app->revision_entry('business', $action,
$bus_number);
redirect('app/formSubmit');
}
else
{
echo "New Entry Failed - Duplicate
Business number check failed.
(business_form/newBusiness)";
die;
}
}
// Form input for EDIT business successfully validates
elseif ($action == 'Edit')
{
$bus_number =
$this->input->post('bus_number',TRUE);
$bus_id = $this->input->post('bus_id',TRUE);
if (!$this->input->post('bus_restricted',TRUE))
{
$_POST['bus_restricted'] = 0;
}
else
{
$_POST['bus_restricted'] = 1;
}
// Check for duplicate Business numbers w/
exception
if ($this->rtui_app->dupe_check('bus',
$bus_number, $bus_id))
{
$inputSchema = array(
'bus_name' => '',
'bus_number' => '',
'bus_address' => '',
'bus_city' => '',
'bus_state' => '',
'bus_zip' => '',
'bus_contact' => '',
'bus_phone' => '',
'bus_extension' => '',
'bus_mobile' => '',
'bus_fax' => '',
'bus_email' => '',
'cat_id' => '',
'employee_rep' => '',
'bus_restricted' => ''
);
$inputData =
array_intersect_key($_POST, $inputSchema);
$this->business->updateBusiness($inputData, $bus_id);
$this->rtui_app->revision_entry('business', $action, $bus_number,
$this->input->post('bus_rev_changes',TRUE));
redirect('app/formSubmit');
}
else
{
echo "Edit Entry Failed - Duplicate
Business number check failed.
(business_form/Business)";
die;
}
}
}
elseif (!isset($submitForm)) // Form is not submitted - execute
new
or edit form
{
// Form is setup for New entry
if (!$this->uri->segment(3))
{
$data['content_header_id'] = "generic_form_add";
$data['content_header_title'] = "Customer Form
Entry";
$data['title'] = "RTUI Customer - New";
$data['action'] = "New";
$data['session_employee_name'] =
$this->rtui_app->get_loggedIn_user
($this->session->userdata('employee_id'));
// Initialize input form variables
$data['bus_name'] = '';
$data['bus_number'] = '';
$data['bus_address'] = '';
$data['bus_city'] = '';
$data['bus_state'] = '';
$data['bus_zip'] = '';
$data['bus_contact'] = '';
$data['bus_phone'] = '';
$data['bus_extension'] = '';
$data['bus_mobile'] = '';
$data['bus_fax'] = '';
$data['bus_email'] = '';
$data['cat_id'] = '';
$data['employee_rep'] = '';
$data['employee_id'] =
$this->session->userdata('employee_id');
$data['bus_restricted'] = '';
$this->load->view('include/header', $data);
$this->load->view('business_new');
$this->load->view('include/footer');
}
// Form is setup for Edit entry
else
{
$data['content_header_id'] =
"generic_form_edit";
$data['content_header_title'] = "Customer Form
Edit";
$data['title'] = "RTUI Customer - Edit";
$data['action'] = "Edit";
$data['session_employee_name'] =
$this->rtui_app->get_loggedIn_user
($this->session->userdata('employee_id'));
$bus_number = $this->uri->segment(3);
$data['results'] =
$this->business->getBusinessbyNumber
($bus_number);
// Load database query results into array for
the input form
foreach ($data['results'] as $row)
{
$data['bus_id'] = $row['bus_id'];
$data['bus_name'] = $row['bus_name'];
$data['bus_number'] =
$row['bus_number'];
$data['bus_address'] =
$row['bus_address'];
$data['bus_city'] = $row['bus_city'];
$data['bus_state'] = $row['bus_state'];
$data['state_name'] =
$row['state_name'];
$data['bus_zip'] = $row['bus_zip'];
$data['bus_contact'] =
$row['bus_contact'];
$data['bus_phone'] = $row['bus_phone'];
$data['bus_extension'] =
$row['bus_extension'];
$data['bus_mobile'] =
$row['bus_mobile'];
$data['bus_fax'] = $row['bus_fax'];
$data['bus_email'] = $row['bus_email'];
$data['cat_id'] = $row['cat_id'];
$data['cat_title'] = $row['cat_title'];
$data['employee_rep'] =
$row['employee_rep'];
$data['rep_firstname'] =
$row['employee_firstname'];
$data['rep_lastname'] =
$row['employee_lastname'];
$data['employee_id'] =
$this->session->userdata('employee_id');
$data['bus_restricted'] =
$row['bus_restricted'];
}
$this->load->view('include/header', $data);
$this->load->view('business_new');
$this->load->view('include/footer');
}
}
}
[/code]

