CodeIgniter Forums
was receiving form validation errors, now i'm not? hows my code look - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: was receiving form validation errors, now i'm not? hows my code look (/showthread.php?tid=60265)

Pages: 1 2


was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-18-2014

[eluser]crispyhihats[/eluser]
i'm updating records in a database with a form that at one point returned errors.. after fixing a few bugs, it seems i've broke the form errors. feeling a little like this guy right now

[Image: iQ60Dr5.gif]


... basically, this is supposed to check for an existing record that is assigned to the user and has the specified song_id. If it has the ID, it will update the record with the new info. All #formErrors errors display except for when data.validation == 'error'...for some reason it's not working..if data.validaiton == 'valid' && data.response == 'error' ... #formErrors will display the error.

The form seems to be validating the inputs as i'm unable to submit until I meet the set_rules rules, but the errors aren't displaying.

my div to display errors
Code:
<div id="formErrors"></div>


javascript inside my view
Code:
$(function(){
       $('#submit-btn').click(function(e){

         e.preventDefault();
         var url = '&lt;?php echo base_url("manage/finish/json") ?&gt;';
         $.post(url,
            $('#myForm').serialize(),
            function(data, status, xhr){

             if (data.validation == 'error') {
              $('#formErrors').html(data.message);
             } else if (data.validation == 'valid' && data.response == 'error') {
              $('#formErrors').html(data.message);
             } else if (data.validation == 'valid' && data.response == 'success') {
                $('#song_form').modal('hide');

[redacted]

inside my controller
Code:
if($this->form_validation->run() === FALSE)
                        {
                                //validation errors
                            echo json_encode(array('validation' => 'error', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
                        } else if (!$this->Song_model->valid_song_exists($file_uid, $user_id, $file_name)) {
                            //couldn't find song that matches the file_uid/user_id/file_name
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'It appears the song doesnt exist.'));
                        } else {
                            //upload
                             $song_row_updated = $this->Song_model->add_complete_song($update_where, $song_data);
                        }

                        if(!$song_row_updated) {
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'Unable to upload song.'));
                        } else {
                            //pay dirt
                            echo json_encode(array('validation' => 'valid', 'response'=>'success','message'=>'song updated', 'song'=>array('song_id'=>$file_uid,'song_url'=>$urlslug)));
                        }


any help would be appreciated


was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-18-2014

[eluser]InsiteFX[/eluser]
You need at least one form set_rules for it to work.



was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-22-2014

[eluser]crispyhihats[/eluser]
[quote author="InsiteFX" date="1392789106"]You need at least one form set_rules for it to work.
[/quote]

my current rules

Code:
$this->form_validation->set_rules('artist', 'Artist', 'trim|required|min_length[2]|max_length[100]');
                      $this->form_validation->set_rules('title', 'Title', 'trim|required|min_length[2]|max_length[100]');
                      $this->form_validation->set_rules('featuring', 'Featuring', 'trim||min_length[2]|max_length[255]');
                      $this->form_validation->set_rules('producer', 'Producer', 'trim|min_length[4]|max_length[25]');
                      $this->form_validation->set_rules('album', 'Album', 'trim|min_length[2]|max_length[255]');
                      $this->form_validation->set_rules('video', 'Video', 'trim|min_length[2]');
                      $this->form_validation->set_rules('image', 'Image', 'trim|min_length[2]');
                      $this->form_validation->set_rules('description', 'Description', 'trim|min_length[2]|max_length[500]');

i am using an ajax post to submit form data to the controller.. the rules seem to work as i'm unable to submit the data until i meet the set rules listed above, however no errors are being displayed.


was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-22-2014

[eluser]InsiteFX[/eluser]
Code:
&lt;?php echo validation_errors(); ?&gt;

Or for each field

&lt;?php echo form_error('username'); ?&gt;

You need to add that to your forms for it to display errors.



was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-26-2014

[eluser]crispyhihats[/eluser]
[quote author="InsiteFX" date="1393108146"]
Code:
&lt;?php echo validation_errors(); ?&gt;

Or for each field

&lt;?php echo form_error('username'); ?&gt;

You need to add that to your forms for it to display errors.
[/quote]

Tried both, neither work.. I am using AJAX to return the validation errors

view
Code:
$.post(url,
            $('#myForm').serialize(),
            function(data, status, xhr){
              alert('successful post!');
             if (data.validation == 'error') {
              alert('Validation Error');
              $('#formErrors').html(data.message);
             } else if (data.validation == 'valid' && data.response == 'error') {
              $('#formErrors').html(data.message);
             } else if (data.validation == 'valid' && data.response == 'success') {
              $('#formErrors').html(data.message);
               $('#song_form').modal('hide');
controller
Code:
if($this->form_validation->run() === FALSE)
                        {
                                //validation errors
                            echo json_encode(array('validation' => 'error', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
                        } else if (!$this->Song_model->valid_song_exists($file_uid, $user_id, $file_name)) {
                            //couldn't find song that matches the file_uid/user_id/file_name
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'It appears the song doesnt exist.'));
                        } else {
                            //upload
                             $song_row_updated = $this->Song_model->add_complete_song($update_where, $song_data);
                        }

                        if(!$song_row_updated) {
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'Unable to upload song.'));
                        } else {
                            //pay dirt
                            echo json_encode(array('validation' => 'valid', 'response'=>'success','message'=>'Song Uploaded.. Redirecting', 'song'=>array('song_id'=>$file_uid,'song_url'=>$urlslug)));
                        }

validation works as i'm unable to submit any data via the modal unless I meet the form rules, however no error text is displaying. I am able to push other text to the div such as "'It appears the song doesnt exist" and "Song Uploaded" which is set in the controller, along with the validation_errors.





was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-26-2014

[eluser]InsiteFX[/eluser]
If you are passing your own error messages back from Ajax then you need to manually set them like below.
Code:
$this->form_validation->set_message('rule', 'Error Message');



was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-26-2014

[eluser]crispyhihats[/eluser]
[quote author="InsiteFX" date="1393421931"]If you are passing your own error messages back from Ajax then you need to manually set them like below.
Code:
$this->form_validation->set_message('rule', 'Error Message');
[/quote]

thanks for the responses, i really appreciate it.

I figured it out.

you don't need to set rules if you're sending errors via AJAX. My code was correct, the problem was my if/else statements in the controller that passed back the json_encoded responses. basically, I had to move the bottom if/else statement inside of the final else statement related to this->form_validation->run


ORIGINAL
Code:
if ($this->form_validation->run() == FALSE)
                        {
                                //validation errors
                            echo json_encode(array('validation' => 'error', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
                            //echo json_encode(array('validation' => 'error', 'response'=>'error', 'message' => 'YOO THIS IS MY FUCKING ALERT'));
                            
                        } else if (!$this->Song_model->valid_song_exists($file_uid, $user_id, $file_name)) {
                            //couldn't find song that matches the file_uid/user_id/file_name
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'It appears the song doesnt exist.'));
                        } else {
                             $song_row_updated = $this->Song_model->add_complete_song($update_where, $song_data);
                            
                        }

if(!$song_row_updated) {
                                echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'Unable to upload song.'));
                            } else {
                                //pay dirt
                                echo json_encode(array('validation' => 'valid', 'response'=>'success','message'=>'Song Uploaded..', 'song'=>array('song_id'=>$file_uid,'song_url'=>$urlslug)));
                            }


CORRECT, WORKING CODE.

Code:
if ($this->form_validation->run() == FALSE)
                        {
                                //validation errors
                            echo json_encode(array('validation' => 'error', 'message' => validation_errors('<div class="alert alert-error"><strong>Error!</strong> ', '</div>')));
                            //echo json_encode(array('validation' => 'error', 'response'=>'error', 'message' => 'YOO THIS IS MY FUCKING ALERT'));
                            
                        } else if (!$this->Song_model->valid_song_exists($file_uid, $user_id, $file_name)) {
                            //couldn't find song that matches the file_uid/user_id/file_name
                            echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'It appears the song doesnt exist.'));
                        } else {
                             $song_row_updated = $this->Song_model->add_complete_song($update_where, $song_data);
                            
                                 if(!$song_row_updated) {
                                echo json_encode(array('validation' => 'valid', 'response'=>'error', 'message' => 'Unable to upload song.'));
                            } else {
                                //pay dirt
                                echo json_encode(array('validation' => 'valid', 'response'=>'success','message'=>'Song Uploaded..', 'song'=>array('song_id'=>$file_uid,'song_url'=>$urlslug)));
                            }
                        }



was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-26-2014

[eluser]ivantcholakov[/eluser]
About the AJAX handler: I would not use direct echo statements. When you move to CodeIgniter 3 I am not sure that this would work as you expect. You may try this way:

Code:
public function index()
    {
        $this->output->set_header('Content-Type: application/json; charset=utf-8');

        // Get POST parameters, make validation, do the action.

        $output_array = array(...);  // The result array - success/error flag, success/error message, other feedback data.

        $this->output->set_output(json_encode($output_array));
    }



was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-27-2014

[eluser]crispyhihats[/eluser]
[quote author="ivantcholakov" date="1393470923"]About the AJAX handler: I would not use direct echo statements. When you move to CodeIgniter 3 I am not sure that this would work as you expect. You may try this way:

Code:
public function index()
    {
        $this->output->set_header('Content-Type: application/json; charset=utf-8');

        // Get POST parameters, make validation, do the action.

        $output_array = array(...);  // The result array - success/error flag, success/error message, other feedback data.

        $this->output->set_output(json_encode($output_array));
    }
[/quote]

nice, thank you! i'm always looking for ways to improve. That section of code always looked a little sloppy to me.


was receiving form validation errors, now i'm not? hows my code look - El Forum - 02-27-2014

[eluser]CroNiX[/eluser]
I just create a helper to do that so I only need to pass an array to it and it outputs the proper headers and json, and then I don't have to repeat that code everywhere I want json output.