0
Today I have some cupboards in windows (modals) and some of these fields need to force the user to fill today I am doing as follows:
In case the user forgets to fill some field I use the following code where valid my form and if there is an error it returns a message:
Code:
function inserir() {
$this->template->set('title', 'Inserir Clientes');
/* Carrega a biblioteca do CodeIgniter responsável pela validação dos formulários */
$this->load->library('form_validation');
/* Define as tags onde a mensagem de erro será exibida na página */
$this->form_validation->set_error_delimiters('<span>', '</span>');
/* Define as regras para validaçã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]');
/* Executa a validação e caso houver erro chama a função index do controlador */
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 seek to do, I want to make the system more functional today if the user forgets some field it is returns the listing page and needs to open the function to register again and inform all fields again, my idea now is that if he forgets some field and click save the modal is not closed and yes that the field(s) he did not fill are highlighted in red, for this I believe I will need to use javascript,this is my doubt.
Perfect friend worked very well
– Felipe Miranda De Lima