Formulário com Validação (Campos em Branco)
Estou enviando um modelo de formatação de formulário que simplifica o processo de verficação de campos em branco.
Olá pessoal, desenvolvi um script muito simples para verificação de campos em branco em formulário. Ele verifica a quantidade de campos que são obrigatórios, através do array[campos], armazenando seus cabeçalhos. Em seguida, faz a contagem destes cabeçalhos, apresentando na forma de uma lista, todos os campos que não foram preenchidos. Uma vez preenchidos estes campos, você pode, por exemplo, cadastrar os dados em banco. Utilizei este modelo na confecção de um Sistema de Registros Informatizados que fiz para a colônia de pescadores aqui da minha cidade e tem funcionado perfeitamente.
ALGUNS SCREENSHOTS DO SISTEMA:
www.infortec.info/projetos/siri/siri.php
Espero que ajude! Abraço à todos.
/*
***************************
VALIDAÇÃO DE FORMULÁRIO
AUTOR: Bruno Bandeira Rocha
SITE: www.infortec.info
***************************
*/
<?
class ExemploFormularioValidacao
{
private $janela;
private $entrada_1;
private $entrada_2;
private $entrada_3;
private $entrada_4;
private $entrada_5;
private $entrada_6;
private $botao;
public function __construct(){
// Cria a janela
$this->janela= new GtkWindow();
// Define título da janela
$this->janela->set_title('Validação de Formulário');
// Define tamanho da janela
$this->janela->set_size_request(300,250);
//Define espaçamento de borda
$this->janela->set_border_width(10);
// Cria uma caixa vertical
$vbox1 = new GtkVbox();
// Cria rótulos do formulário
$label_1 = new GtkLabel("Nome * ");
$label_1->set_size_request(80,-1);
$label_1->set_alignment(1, 0.5);
$label_2 = new GtkLabel("Endereço * ");
$label_2->set_alignment(1, 0.5);
$label_2->set_size_request(80,-1);
$label_3 = new GtkLabel("Bairro * ");
$label_3->set_size_request(80,-1);
$label_3->set_alignment(1, 0.5);
$label_4 = new GtkLabel("Cidade * ");
$label_4->set_size_request(80,-1);
$label_4->set_alignment(1, 0.5);
$label_5 = new GtkLabel("Tel ");
$label_5->set_size_request(80,-1);
$label_5->set_alignment(1, 0.5);
$label_6 = new GtkLabel("Email ");
$label_6->set_size_request(80,-1);
$label_6->set_alignment(1, 0.5);
// Cria caixas de entrada
$this->entrada_1 = new GtkEntry();
$this->entrada_2 = new GtkEntry();
$this->entrada_3 = new GtkEntry();
$this->entrada_4 = new GtkEntry();
$this->entrada_5 = new GtkEntry();
$this->entrada_6 = new GtkEntry();
//Empacota formulário - Nome
$hbox_1 = new GtkHbox();
$hbox_1->pack_start($label_1, false, false);
$hbox_1->pack_start($this->entrada_1, false, false);
$hbox_1->pack_start(new GtkLabel(""), true, true);
//Empacota formulário - Endereço
$hbox_2 = new GtkHbox();
$hbox_2->pack_start($label_2, false, false);
$hbox_2->pack_start($this->entrada_2, false, false);
$hbox_2->pack_start(new GtkLabel(""), true, true);
//Empacota formulário - Bairro
$hbox_3 = new GtkHbox();
$hbox_3->pack_start($label_3, false, false);
$hbox_3->pack_start($this->entrada_3, false, false);
$hbox_3->pack_start(new GtkLabel(""), true, true);
//Empacota formulário - Cidade
$hbox_4 = new GtkHbox();
$hbox_4->pack_start($label_4, false, false);
$hbox_4->pack_start($this->entrada_4, false, false);
$hbox_4->pack_start(new GtkLabel(""), true, true);
//Empacota formulário - Tel
$hbox_5 = new GtkHbox();
$hbox_5->pack_start($label_5, false, false);
$hbox_5->pack_start($this->entrada_5, false, false);
$hbox_5->pack_start(new GtkLabel(""), true, true);
//Empacota formulário - Email
$hbox_6 = new GtkHbox();
$hbox_6->pack_start($label_6, false, false);
$hbox_6->pack_start($this->entrada_6, false, false);
$hbox_6->pack_start(new GtkLabel(""), true, true);
//Define botão
$this->botao = new GtkButton("Finalizar Registro");
// Conecta botão à função finaliza registro
$this->botao->connect_simple("clicked",array($this,'onFinalizaRegistro'));
//Define caixa horizontal
$hbox_7 = new GtkHbox();
$hbox_7->pack_start(new GtkLabel(""), true, true);
$hbox_7->pack_start($this->botao, false, false);
$hbox_7->pack_start(new GtkLabel(""), true, true);
//Empacota layout
$vbox1->pack_start($hbox_1, false, false);
$vbox1->pack_start($hbox_2, false, false);
$vbox1->pack_start($hbox_3, false, false);
$vbox1->pack_start($hbox_4, false, false);
$vbox1->pack_start($hbox_5, false, false);
$vbox1->pack_start($hbox_6, false, false);
$vbox1->pack_start($hbox_7, false, false);
//Adiciona layout a janela
$this->janela->add($vbox1);
// Mostra janela e widgets
$this->janela->show_all();
// Conecta o X da janela a função de sair
$this->janela->connect_simple("destroy",array('Gtk','main_quit'));
}
// Função que verifica campos em branco
function onFinalizaRegistro() {
//Pega dados do formulario
$campos['NOME'] = $NOME = $this->entrada_1->get_text();
$campos['ENDERECO'] = $ENDER = $this->entrada_2->get_text();
$campos['BAIRRO'] = $BAIRRO = $this->entrada_3->get_text();
$campos['CIDADE'] = $MUNICIPIO = $this->entrada_4->get_text();
$TEL = $this->entrada_5->get_text();
$EMAIL = $this->entrada_6->get_text();
// Verifica campos em branco
if(sizeof($campos) > 0){
$em_branco = array();
foreach($campos as $valida => $valor){
if(!$valor){
$em_branco[] = $valida;
}}
// Verifica tamanho do array
$tam = sizeof($em_branco);
// Converte array em string
$ad = implode("
",$em_branco);
}
// Cria array de substitui os valores em branco por nomes em extenso
$anterior = array("NOME","ENDERECO","BAIRRO","CIDADE");
$nova = array("Nome ","Endereço","Bairro","Cidade");
// Substitui nomes da string
$aviso = str_replace($anterior, $nova , $ad);
if($tam >1){
//Exibe popup informando sobre campos nulos
$dialog = new GtkMessageDialog(null, Gtk::DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
$dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$dialog->set_border_width(10);
$dialog->set_modal(true);
$dialog->set_transient_for($wnd);
$dialog->set_markup('<span foreground="black">Os seguintes campos<b><i> são obrigatórios</i></b></span><span foreground="black"> e não devem ser deixados em branco: </span>'. "
" . '<b>' . $aviso . '</b>' . "
" . '<span foreground="black">Preencha estes campos antes de salvar o registro atual.</span>');
$dialog->modify_font(new PangoFontDescription('Arial 10'));
$dialog->run();
$dialog->destroy();
}else{
if($tam == 1){
//Exibe popup informando sobre campos nulos
$dialog = new GtkMessageDialog(null, Gtk::DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
$dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$dialog->set_border_width(10);
$dialog->set_modal(true);
$dialog->set_transient_for($wnd);
$dialog->set_markup('<span foreground="black">O seguinte campo<b><i> é obrigatório</i></b></span><span foreground="black"> e não deve ser deixado em branco: </span>'. "
" . '<b>' . $aviso . '</b>' . "
" . '<span foreground="black">Preencha estes campos antes de salvar o registro atual.</span>');
$dialog->modify_font(new PangoFontDescription('Arial 10'));
$dialog->run();
$dialog->destroy();
}else{
//Exibe popup informando sobre campos nulos
$dialog = new GtkMessageDialog(null, Gtk::DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK);
$dialog->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$dialog->set_border_width(10);
$dialog->set_modal(true);
$dialog->set_transient_for($wnd);
$dialog->set_markup('<span foreground="black">Todos os dados foram preenchidos corretamente.</span>');
$dialog->modify_font(new PangoFontDescription('Arial 10'));
$dialog->run();
$dialog->destroy();
}}}
// fecha a função onFinalizaRegistro
}





