Welcome Guest, Not a member yet? Register   Sign In
Validation with Sessions or Ajax [SOLVED]
#1

[eluser]Kosonome[/eluser]
I'm having some problems checking if a validation return is via Ajax or by Sessions (with javascript disabled), see my code:

All the Library:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
   function MY_Form_validation() {
      parent::CI_Form_validation();
      $this->set_message('valid_email', 'O campo %s contém um e-mail inválido.');
      $this->set_message('required', 'O campo %s precisa ser preenchido.');
      $this->set_message('is_numeric', 'O campo %s precisa ter somente números.');
   }

   function ajax_or_session($boolean, $nomeSessao, $mensagem, $redirect) {
   //Verificar se é por AJAX ou sessão PHP
      if ($boolean) {
         echo $mensagem;
      } else {
         $this->CI->load->library('session');
         $this->CI->session->set_flashdata($nomeSessao, $mensagem);
         redirect(site_url($redirect));
      }
   }
}
?>

One Function of the Controller for Ajax Request via Post
Code:
function prematricula() {
      if ($_POST) {
         $this->load->library('form_validation');
        /* many rules */
         if ($this->form_validation->run() == FALSE) {
            $mensagem = validation_errors();
         } else {
            $this->load->model('prematricula_model');
            $this->prematricula_model->insert($arrayInsert);
            $mensagem = 'Success!';
         }
         $this->form_validation->ajax_or_session($this->input->post('javascript_ativo'), 'validacaoPreMatricula', $mensagem, '/turmas/mostrar/'.$this->input->post('turma_id'));
      }
      redirect(site_url('/turmas'));
   }

Another part of another function of same Controller:
Code:
if ($this->session->flashdata('validacaoPreMatricula')) {
    $dataBox['validacaoPreMatricula'] = $this->session->flashdata('validacaoPreMatricula');
}
$data['matriculeseBox'] = $this->parser->parse('turmasMatriculese', $dataBox, TRUE);
$this->parser->parse('tpl.turma', $data);

Since I don't have any JS yet, $this->input->post() always return false. When the validation Fails, $dataBox['validacaoPreMatricula'] get the validation_error() string, but when validation returns true, it don't show 'Success' string.

Any help? Smile
#2

[eluser]Kosonome[/eluser]
I just solved this.

I was doing:
Code:
$msg = 'Some message<br/>
       Thank you!';
$this->session->set_flashdata('something', $msg);

Then changed to:
Code:
$msg = 'Some message<br/>Thank you!';
$this->session->set_flashdata('something', $msg);
in a single line.

Maybe it's because it needs to be linux new lines, or something like this, then I just need to set somewhere in my editor, althought I don't know where.

Well, it's solved by myself.




Theme © iAndrew 2016 - Forum software by © MyBB