How to keep the form data after submitting using Codelgniter

0

I'm new to using the Codelgniter Framework, I have a question about how to keep my form data after submitting the page, I'm creating a form with enough fields to fill in, if the user leaves a field blank or writes in a wrong way it will generate a msg error, I wanted the user to have the option to go back in the form and retrieve what was typed.

$this->form_validation->set_rules('nome', 'NOME', 'required|min_length[3]|max_length[40]');
    $this->form_validation->set_rules('data_nasc', 'DATA DE NASCIMENTO', 'required');
    $this->form_validation->set_rules('rg', 'RG', 'required|exact_length[9]|numeric');
    $this->form_validation->set_rules('cpf', 'CPF', 'required|exact_length[11]|numeric');
    $this->form_validation->set_rules('sexo_pessoa', 'SEXO DO CLIENTE', 'required|numeric');
    $this->form_validation->set_rules('rua', 'RUA', 'required|min_length[4]|max_length[20]|trim');
    $this->form_validation->set_rules('numero', 'NUMERO', 'required|max_length[5]|numeric');
    $this->form_validation->set_rules('cep', 'CEP', 'required|exact_length[8]|numeric');
    $this->form_validation->set_rules('estado', 'ESTADO', 'required|numeric');
    $this->form_validation->set_rules('cidade', 'CIDADE', 'required|numeric');
    $this->form_validation->set_rules('bairro', 'BAIRRO', 'required|numeric');
    $this->form_validation->set_rules('tel1', 'TELEFONE 1', 'required|min_length[10]|max_length[11]|numeric');      
    $this->form_validation->set_rules('cat_tel1', 'CATEGORIA DO TELEFONE 1', 'required|numeric');       
    $this->form_validation->set_rules('nome_animal', 'NOME DO ANIMAL', 'alpha|required|min_length[3]|max_length[20]');
    $this->form_validation->set_rules('categoria_animal', 'CATEGORIA DO ANIMAL', 'required|numeric');
    $this->form_validation->set_rules('raca', 'RAÇA', 'required|numeric');
    $this->form_validation->set_rules('sexo_animal', 'SEXO DO ANIMAL', 'required|numeric');
    $this->form_validation->set_rules('cor', 'COR', 'required|numeric');
    $this->form_validation->set_rules('porte', 'PORTE', 'required|numeric');
    $this->form_validation->set_rules('data_nasc_animal', 'DATA DE NASCIMENTO DO ANIMAL', 'required');

    if($this->form_validation->run() == FALSE){
        if(validation_errors()){
            die('<p> <a href="'.base_url('cadastro_cliente').'"> Clique aqui </a> para voltar</p>'.validation_errors());
        }
    }       
    
asked by anonymous 11.12.2018 / 22:31

1 answer

0

You need two things to make the values come back filled in a form that went wrong:

1 °) Re-call view by passing $data to the value of these fields that you have been validated. 2) You have programmed the fields of the form with value= receiving the function set_data() , which tests whether or not the value exists, when the form is zeroed.

If all fields came from POST, you quickly mount a $data by taking PHP $_POST values. If any variable is not in POST, you have to add it in hand. Logo:

if($this->form_validation->run() == FALSE){
    if(validation_errors()){
        $data = $this->input->post();

        // Adiciona uma variável manualmente no $data
        $data['erros_do_form'] = validation_errors();

        // Usuário volta para o mesmo lugar onde estava
        $this->load->view('nome_da_view', $data);
    }
}  

In% w_of_%, display the error to the user by means of the variable (array) view .

Even in erros_do_form , you need to prepare the inputs to receive or not value, that is, the same form will be called zeroed or with values filled. Example for field animal_name:

// null é o valor default aqui, mas poderia ser 'Lulu'
<input name="nome_animal" type="text" value="<?=set_value('nome_animal', null)?>">
    
26.12.2018 / 17:53