what I am trying to do is pretty simple (I hope):
I am trying to validate POST data (or any data provided via variable) with Form_Validation.
The problem is that to run actual validation I/we need to do
if ($this->form_validation->run() == FALSE){
//validation did not pass
}else{
//validation did pass
}
my set of rules are like this:
$this->form_validation->set_rules('key', '', 'trim|required'); //simplified
so, I am sending data via AJAX to this script obviously
echo $_POST['key']; //prints valid value that is sent from AJAX
How can I validate $_POST['key'] using form_validation?
AJAX code
$("#key-inputs .short, #key-inputs .long").change(function () {
var key = $(this).val();
var id = $(this).attr('name');
var table = $(this).attr('class');
var file = $('span .file').val();
var data = 'NULL';
$.post(file + "/edit",{
id:id,
key:key,
file:file,
table:table},
function(code, textStatus) {
data = code;
});
$(this).ajaxStop(function(){
if (data == 3) $(this).parent().fadeOut('slow', function() {}); //delete
if (data == 2) $(this).fadeOut(200).fadeIn(200); //update
if (data == 5) alert('not valid'); //update parameter is not valid input (depends on set_value rules)
//writeConsole(previous.val().outerHTML);
});
});
PHP code //simplified
if (!$this->input->is_ajax_request()) redirect('lang/translate/keys/'.$param);
//do work here
if ($key == "") {
//delete key
$this->lang_translate_model->DeleteKey($id, $table);
echo 3;
return TRUE;
}
$this->form_validation->set_rules('key', 'key', 'trim|required|max_length[56]|xss_clean|unique_file[lang_key:'.$param.']');
if ($this->form_validation->run() == FALSE){
//validation did not pass
echo 5;
}
so I have some (generated) inputs, on change I want to make update in DB only if ..set_rules passes.
if input is empty it deletes itself (this works fine)
SOLUTION
TRUE/FALSE table is here
if ($this->form_validation->required($key)){
//validation did not pass
echo 5;
}
if there is parameter to pass for example min_length[2];
$this->form_validation->min_length($key,'2');