Welcome Guest, Not a member yet? Register   Sign In
set error delimiters (field name as class)
#1

[eluser]Nouman[/eluser]
Hey everyone,

so my question is:
I want to set a custom error delimiter across all validations for this form I'm checking.

Code:
$this->form_validation->set_error_delimiters('<p class=""/>', '</div>');

is there anyway to set the paragraph class as the field name?

I'm using ajax to check the validation with the controller, and I want to place each error beside its own input field. If I simply echo validation_errors, I just get them wrapped in <p> tags without a way for my javascript to tell them apart.

thanks!
#2

[eluser]bretticus[/eluser]
Quote:is there anyway to set the paragraph class as the field name?

I strongly doubt it, but I'm not sure why you need to do this anyway (I'll explain further below.)

Also, you are setting the wrapper for error messages with:

Code:
$this->form_validation->set_error_delimiters('<p class=""/>', '</div>');

But you have a complete paragraph tag and then a closing div??? Did you leave the opening div out by chance?

Either way, I think this is unnecessary. I assume you mean to get the errors via AJAX from the output of validation_errors(). There is a better way I think. For example, I just return the entire error array after calling run() from the form_validation object. It's undocumented but there for the picking. Here's some code from one of my projects:

Code:
if ($this->form_validation->run() === FALSE) {
  if ( IS_AJAX ) {
    json_headers(); //optional json headers via custom helper (don't let this distract you.)
    $response = $this->form_validation->_error_array;
    echo json_encode($response);
  }
}

$this->form_validation->_error_array is an associative array with the field names as keys and, as the values, error messages. Perfect! I return that as JSON so I can use the variables as native javascript variables. For example, using jquery's post method:

Code:
$.post('controller/method', {param1: some_value}, function(data) {
    $.each(data, function (field,error) {
        $('#'+field).css('background-color', 'red');
        $('<p>'+error+'</p>').insertAfter('#'+field);
    });
}, 'json');
#3

[eluser]Nouman[/eluser]
Hey, thanks for the reply and suggestions. (and for the <P> </div> issue, that was me overlooking my tags ;] )

So I've changed my setup to function the way you recommended, and when I submit the form I get 500 internal server error.

This was after the response showed Use of undefined constant IS_AJAX – assumed ‘IS_AJAX’, so I added

Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');

to my constants file (found via google).

Am I missing something?

thanks!
#4

[eluser]bretticus[/eluser]
Yes, I should have mentioned that the IS_AJAX constant is something you need to define (it's so common now that I take it for granted that many developers aren't familiar with it. They ought to just put it in CodeIgniter 2.0.) However, defining this as you have in your constants file, should make the undefined warning go away (or error I guess.) If you copied my code, did you remove the json_headers() call? If not, as the comments indicate, it's my own helper function. Very simple way to just send headers with application/json. If you want to use it, it's here:

Code:
if ( !function_exists('json_headers') ) {
    function json_headers() {
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: application/json');
    }
}
#5

[eluser]Nouman[/eluser]
Thanks so much dude, everything works perfectly. And the code is much easier to maintain!
#6

[eluser]Unknown[/eluser]
Thanks




Theme © iAndrew 2016 - Forum software by © MyBB