Today I have a few forms in windows (modal) and some of these fields need to force the user to fill in today I'm doing the following:
IftheuserforgetstofillinsomefieldIusethefollowingcodewheremyformvalidatesandifthereisanerroritreturnsamessage:
Code:
functioninserir(){$this->template->set('title','InserirClientes');/*CarregaabibliotecadoCodeIgniterresponsávelpelavalidaçãodosformulários*/$this->load->library('form_validation');/*Defineastagsondeamensagemdeerroseráexibidanapágina*/$this->form_validation->set_error_delimiters('<span>','</span>');/*Defineasregrasparavalidação*/$this->form_validation->set_rules('cnome','Nome','required|max_length[40]');$this->form_validation->set_rules('emailc','E-mail','trim|required|valid_email|max_length[100]');$this->form_validation->set_rules('contato','Contato','trim|required|max_length[20]');$this->form_validation->set_rules('telefone','Telefone','trim|required|max_length[20]');$this->form_validation->set_rules('cidade','Cidade','trim|required|max_length[60]');$this->form_validation->set_rules('nameR','Representante','trim|required|max_length[60]');/*Executaavalidaçãoecasohouvererrochamaafunçãoindexdocontrolador*/if($this->form_validation->run()===FALSE){$this->session->set_flashdata('mensagem',"<div class='alert alert-danger'> preencha todos os campos antes de salvar </div>");
redirect('clientes');
/* Senão, caso sucesso: */
} else {
/* Recebe os dados do formulário (visão) */
$data['cnome'] = ucwords($this->input->post('cnome'));
$data['contato'] = ucwords($this->input->post('contato'));
$data['telefone'] = $this->input->post('telefone');
$data['emailc'] = strtolower($this->input->post('emailc'));
$data['cidade'] = ucwords($this->input->post('cidade'));
$data['representante'] = ucwords($this->input->post('nameR'));
/* Chama a função inserir do modelo */
if ($this->model->inserir($data)) {
$this->session->set_flashdata('mensagem', "<div class='alert alert-success'> Cliente salvo com sucesso</div>");
redirect('clientes');
} else {
$this->session->set_flashdata('mensagem', "<div class='alert alert-danger'> Erro ao inserir cliente</div>");
redirect('clientes');
}
}
}
Message returned in case of error:
Now explaining what I try to do, I want to leave the system more functional today if the user forgets a field he returns the list page and needs to open the register function again and enter all the fields again, my idea now is if you forget some field and click save the modal is not closed and yes the field (s) that it did not fill are highlighted in red, so I think I will need to use javascript, this is my doubt.