<?php
class Validate_livestock extend开发者_如何学Cs Controller
{
//create a variable to be used for an array to pass back to the livestock form if there are errors.
var $validate_field;
function Validate_livestock()
{
parent::Controller();
//echo 'species11: '.$fs;
//turn the validation_field variable into an array
$this->validate_field = $this->uri->uri_to_assoc(3);
}
function validate_form()
{
//load the form validation library
$this->load->library('form_validation');
//set form validation rules
switch ($this->validate_field['field']){
case "alias":
$this->form_validation->set_rules('alias', 'Alias', 'callback_alias_check', 'trim|xss_clean');
//echo 'input: ' . $this->input->post('alias');
break;
}
//see if the form validates
if ($this->form_validation->run() == FALSE)
{
//print_r($this->validate_field['field']);
$this->load->view('false');
//echo $this->form_validation->run();
//echo "Validator Successfully ran.";
}
else
{
//print_r($this->validate_field['field']);
$this->load->view('true');
//echo $this->form_validation->run();
//echo "Validator Unsuccessfully ran.";
}
function alias_check($str)
{
if ($str == 'test')
{
return FALSE;
}
else
{
return TRUE;
}
}
}
}
}
?>
jquery code: $(document).ready(function() { function validateElement(formId, element, errorContainer) {
$.ajax({
type: 'POST',
cache: false,
url: "validate_livestock/" + "validate_form/field/" + element,
data: element+"="+$('#'+element).val(),
context: document.body,
dataType: 'html',
success: function(){
alert(document.body)
}
});
}
$('#alias').change(function(){
validateElement('#add_livestock', 'alias', '#alias_error_1')
});
});
You seem to have the function, alias check
inside the validate_form method
function validate_form()
{
//load the form validation library
$this->load->library('form_validation');
//set form validation rules
switch ($this->validate_field['field']){
case "alias":
$this->form_validation->set_rules('alias', 'Alias', 'callback_alias_check', 'trim|xss_clean');
//echo 'input: ' . $this->input->post('alias');
break;
}
//see if the form validates
if ($this->form_validation->run() == FALSE)
{
//print_r($this->validate_field['field']);
$this->load->view('false');
//echo $this->form_validation->run();
//echo "Validator Successfully ran.";
}
else
{
//print_r($this->validate_field['field']);
$this->load->view('true');
//echo $this->form_validation->run();
//echo "Validator Unsuccessfully ran.";
}
function alias_check($str)
{
if ($str == 'test')
{
return FALSE;
}
else
{
return TRUE;
}
}
}
}
You should have it outside that method:
function validate_form() {}
function alias_check(){}
精彩评论