Welcome Guest, Not a member yet? Register   Sign In
Form_validation questions
#1

[eluser]joeizang[/eluser]
Hi y'all,

I have a quick question, and sorry I am a NOOB as I have been using codeigniter for about 2 months. I need to know is it ok to echo out validation_errors() from inside a controller? This is what I see in a lot of tutorials on line but from learning OOP in php it's srongly discouraged to echo directly from a class. I am trying to show validation errors on a form but doing anything else apart from echoing does not give me access to the errors. Here is my code:

<?php
//assuming the class and extending it etc is done right.


function create()
{
$this->form_validation->set_rules('articletitle','Article Title','required|xss_clean|trim');
$this->form_validation->set_rules('author','Article Author', 'required|xss_clean|trim');
$this->form_validation->set_rules('articlebody','Article\'s Body','required|xss_clean|trim');

if($this->form_validation->run() == FALSE)
{
//$this->addarticle();
return validation_errors();
}
else{
$data = array(
'articletitle' => $this->input->post('articletitle'),
'articleauthor' => $this->input->post('author'),
'articlebody' => $this->input->post('articlebody')
);

$this->load->model('articles');
$this->articles->createarticle($data);
// $this->load->view('editorfiles/successful');

}
}

?>

when I try to insert it works fine but when I have errors it does not show at all. I am ajaxing the entire process so there is not page refresh so I want the page to show up in the same div as where the form was. here is my ajax code:

//JQuery code
function submithandler(formname)
{
$(formname).submit(function(eve){
eve.preventDefault();
$.ajax({
url: 'create',
type: 'POST',
data: $('#manualform').serialize(),
beforeSend: function(){
$('#busy').show('slow');
},
success: function(data){
$('#busy').hide('slow');
processform(data);//handles the form processing
}
})
});
}
//end JQUERY code

any thots?!
#2

[eluser]BrianDHall[/eluser]
Technically you should echo form errors in your view...but you are unlikely to be shiv'ed for echo'ing in your controller. It's not best practice though, so I would generally recommend echo in your view.
#3

[eluser]bretticus[/eluser]
At a quick glance it appears you are returning the errors but not sending them to buffer (ie echo.)


try changing:

return validation_errors();

to:

echo validation_errors();
#4

[eluser]joeizang[/eluser]
Thanks guyz,

will try this and see how it goes

Best regards
#5

[eluser]bretticus[/eluser]
On another note, sometimes i want to return json to my Ajax call. Often it's really easy to just echo out json_encode($some_array);

A common approach to this is detecting that the request is via Ajax and handling it differently.

Here's some code that is floating around. It goes in your application/config/constants.php file:

Code:
/*
|--------------------------------------------------------------------------
|Detect an ajax request
|--------------------------------------------------------------------------
|
|
|
*/

define('IS_AJAX', (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) ? TRUE : FALSE);

Now you can use a conditional throughout your code to determine how you will respond.

Code:
function respond()
{
if ( IS_AJAX ) {
   echo json_encode(...)
} else {
   ///show your view.
}




Theme © iAndrew 2016 - Forum software by © MyBB