Welcome Guest, Not a member yet? Register   Sign In
validation issue
#1

[eluser]Miuuzn[/eluser]
hey, im new to CI and i have a problem... could u guys help me out?

i wanna use validation, but i usually separate form actions from the actual logic
that would be "insert" for the form and "insertAction" for the submit action for example

but if i try using this with the validation(so it can display the errors) i need to render the form(whenever it fails on validation) on insertAction and that would could break all my system...
is it possible to pass the validation errors to the redirected page?

if not... i would have to use a single action both for form and for submit, and that would require something like:

Code:
if ($request->isPost()) {
// submit logic here
}

(example from cake)

so, do i need to change all my work to use the validation or can i pass it to the redirect?
#2

[eluser]Twisted1919[/eluser]
Maybe i am wrong , but loading the validation class in where you want to show the errors , wouldn't show them ?
#3

[eluser]Miuuzn[/eluser]
controller actions:

Code:
function inserir()
        {
            $dados['titulo'] = 'Novo tipo de embalagem';
            $layoutDados['titulo'] = 'Novo tipo de embalagem';
            
            $layoutDados['conteudo']= $this->load->view('embalagens/form_inserir', $dados);

            $this->load->view('layout', $layoutDados);
        }
        
        function actInserir()
        {
            $registro['descricao'] = $this->input->post('descricao');
            $registro['custo'] = $this->input->post('custo');
            
            $this->form_validation->set_rules('descricao', 'Descrição', 'required');
            $this->form_validation->set_rules('custo', 'Custo', 'required');
            
            if ($this->form_validation->run() == FALSE)
            {
                redirect('embalagens/inserir');
            }
            else
            {
                if ($this->db->insert('embalagens', $registro))
                {
                    redirect('embalagens/listar');
                }
            }
        }

my controller is called "Embalagens" (sorry its almost everything in portuguese)

the problem is i cant use "redirect('embalagens/inserir');" because i lose the validation errors when i redirect, and i i render the view on actInserir i would have to copy all the code from insert so that it work properly
#4

[eluser]Twisted1919[/eluser]
You can use the session to hold your errors and you can destroy that session right after the error is shown ... not the best ideea but still you have 2 fields so it should be no problem for that?

Otherwise instead of redirect you can reload your form view and pass the errors with $data['errors'] = validation_errors() .
#5

[eluser]Miuuzn[/eluser]
well i am avoiding having to render the form in the two actions(because, it may not be this case, but on some other this would get too fat)
so, how can i make these two actions only one?

that would be something like

Code:
if ($request->isPost())
{
     // VALIDATE AND INSERT ON DATABASE
}
// RENDER FORM

how can i accomplish this in CI? is there a function to make sure the request is submited from a form?
i know i can just set a hidden field with some value and check it, but that would be kinda ugly.
#6

[eluser]devbro[/eluser]
here is what i usually do:
Code:
if($_POST)
{
/// do validation work
.
.
.
if($this->form_validation->run())
{
//success
}
else
{
//failed
}

}
else
{
//first time visiting the page
}

$this->load->view('news_form',$view_data);

if there is no data set for the html code then show nothing only the form.
in case of error show the form and error
in case of success go ahead with insert/update/redirect ....




Theme © iAndrew 2016 - Forum software by © MyBB