Created
September 1, 2016 00:13
-
-
Save pedroppinheiro/b65c798bb9a5ccb98fb3d3ef77ca3756 to your computer and use it in GitHub Desktop.
Classe que ajuda na validação de campos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Exemplo de utilização: | |
| * $regras = array( | |
| * Validar::Required($situacao_id, 'Preencha o campo de Situação!'), | |
| * Validar::Required($empregado_nome, 'Preencha o campo Nome!'), | |
| * Validar::Required($empregado_cpf, 'Preencha o campo CPF!'), | |
| * Validar::Required($empregado_base_id, 'Selecione uma base!'), | |
| * Validar::Required($empregado_data_admissao, 'Insira a data de admissão do empregado!'), | |
| * Validar::EmpregadoCodigoNaoExistente($empregado_codigo, 'O Código já pertence a um empregado!'), | |
| * Validar::CpfValido($empregado_cpf, 'Insira um CPF válido!'), | |
| * Validar::EmpregadoCpfNaoExistente($empregado_cpf, 'O CPF informado já pertence a um empregado!'), | |
| * Validar::File('txtArquivoDoExame', $exame_arquivo_do_exame) | |
| * ); | |
| * $error_message = Validar::Campos($regras); | |
| * if(empty($error_message)) { | |
| * list($exame_arquivo_do_exame_upload,$exame_arquivo_do_exame) = Util::uploadFile('txtArquivoDoExame'); | |
| * ... | |
| * } | |
| **/ | |
| class Validar { | |
| static function Required($sCampo, $sMensagem) { | |
| if (empty($sCampo) && $sCampo !== '0') { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| #VALIDAR CAMPOS VAZIOS | |
| static function Equals($sCampo1, $sCampo2, $sMensagem) { | |
| if (!empty($sCampo1) && !empty($sCampo2) && $sCampo1 !== $sCampo2) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| #VALIDAR CAMPOS IGUAIS | |
| static function EqualsTo($sCampo1, $sCampo2, $sMensagem) { | |
| if ($sCampo1 === $sCampo2) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| static function PercentualValido($sCampo1, $sMensagem) { | |
| $percentual = intval($sCampo1); | |
| if ($percentual < 1 || $percentual > 100) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| #VALIDAR CAMPOS IGUAIS | |
| static function LoginExistente($sCampo, $sMensagem) { | |
| if (empty($sCampo)) { | |
| return null; | |
| } | |
| $oFachada = new Fachada(); | |
| $voObjeto = $oFachada->usuariogerencia()->getUsuariogerencia($sCampo); | |
| if (isset($voObjeto) && is_object($voObjeto) && !empty($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| static function CpfValido($sCampo, $sMensagem) { | |
| if (!empty($sCampo) && !Util::validaCPF($sCampo)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| static function CnpjValido($sCampo, $sMensagem) { | |
| if (!empty($sCampo) && !Util::validaCNPJ($sCampo)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| // static function TelefoneExistente($sCampo, $sFiltro, $sMensagem) { | |
| // if(!$sCampo) { | |
| // return null; | |
| // } | |
| // $oFachada = new Fachada(); | |
| // $filtro ='telefone1 = "' . $sCampo . '"'; | |
| // if(!empty($sFiltro)) { | |
| // $filtro .= " AND " . $sFiltro; | |
| // } | |
| // $vFiltro = array($filtro); | |
| // $voObjeto = $oFachada->getAllSeg_usuario(null, $vFiltro); | |
| // if (is_array($voObjeto)) { | |
| // return $sMensagem; | |
| // } else { | |
| // return null; | |
| // } | |
| // } | |
| static function File($sCampo, $file_name, $file_extension = null) { | |
| $allowed_extensions = $file_extension === null || !is_array($file_extension) ? Constante::uploadExtensoesPermitidas() : $file_extension; | |
| $default_msg = 'Falha no arquivo ' . htmlspecialchars($file_name) . ': '; | |
| // checks for default errors | |
| $error_number = intval($_FILES[$sCampo]['error']); | |
| if($error_number === 4) { | |
| return null; | |
| } | |
| if ($error_number !== 0) { | |
| return $default_msg . Constante::dataToString(Constante::uploadErrors(), $error_number); | |
| } | |
| // Faz a verificação da extensão do arquivo | |
| $extensao = strtolower(end(explode('.', $_FILES[$sCampo]['name']))); | |
| if (array_search($extensao, $allowed_extensions) === false) { | |
| return $default_msg . Constante::dataToString(Constante::uploadErrors($allowed_extensions), 5); | |
| } | |
| // Faz a verificação do tamanho do arquivo | |
| if (UPLOAD_TAMANHO < intval($_FILES[$sCampo]['size'])) { | |
| return $default_msg . Constante::dataToString(Constante::uploadErrors(), 6); | |
| } | |
| return null; | |
| } | |
| /** | |
| * | |
| * @param type $sCampo | |
| * @param type $sMensagem | |
| * @param type $id para desconsiderar da busca | |
| * @return type | |
| */ | |
| static function LaboratorioCnpjNaoExistente($sCampo, $sMensagem, $id = null) { | |
| if (empty($sCampo)) { | |
| return null; | |
| } | |
| $oFachada = new Fachada(); | |
| $voObjeto = $oFachada->laboratorio()->getAll(null, array("cnpj = ?", "id <> ?"), null, null, null, null, null, array($sCampo, $id)); | |
| if (isset($voObjeto) && is_array($voObjeto) && !empty($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| /** | |
| * | |
| * @param type $sCampo | |
| * @param type $sMensagem | |
| * @param type $id para desconsiderar da busca | |
| * @return type | |
| */ | |
| static function EmpregadoCpfNaoExistente($sCampo, $sMensagem, $id = null) { | |
| if (empty($sCampo)) { | |
| return null; | |
| } | |
| $oFachada = new Fachada(); | |
| $voObjeto = $oFachada->empregado()->getAll(null, array("cpf = ?", "id <> ?", "deletado = 0"), null, null, null, null, null, array($sCampo, $id)); | |
| if (isset($voObjeto) && is_array($voObjeto) && !empty($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| /** | |
| * | |
| * @param type $sCampo | |
| * @param type $sMensagem | |
| * @param type $id para desconsiderar da busca | |
| * @return type | |
| */ | |
| static function EmpregadoCodigoNaoExistente($sCampo, $sMensagem, $id = null) { | |
| if (empty($sCampo)) { | |
| return null; | |
| } | |
| $oFachada = new Fachada(); | |
| $voObjeto = $oFachada->empregado()->getAll(null, array("codigo = ?", "id <> ?", "deletado = 0"), null, null, null, null, null, array($sCampo, $id)); | |
| if (isset($voObjeto) && is_array($voObjeto) && !empty($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| /** | |
| * | |
| * @param type $sCampo | |
| * @param type $sMensagem | |
| * @param type $id para desconsiderar da busca | |
| * @return type | |
| */ | |
| static function SorteioCodigoNaoExistente($sCampo, $sMensagem, $id = null) { | |
| if (empty($sCampo)) { | |
| return null; | |
| } | |
| $oFachada = new Fachada(); | |
| $voObjeto = $oFachada->sorteio()->getAll(null, array("codigo = ?", "id <> ?"), null, null, null, null, null, array($sCampo, $id)); | |
| if (isset($voObjeto) && is_array($voObjeto) && !empty($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| function CepValido($sCampo, $sMensagem) { | |
| // retira espacos em branco | |
| $sCampo = trim($sCampo); | |
| // expressao regular para avaliar o cep | |
| $re = "/^[0-9]{5}-[0-9]{3}$/"; | |
| if(!empty($sCampo) && preg_match($re, $sCampo) === 0) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| static function EmailExistente($sCampo, $sFiltro, $sMensagem) { | |
| $oFachada = new Fachada(); | |
| $filtro ='email = "' . $sCampo . '"'; | |
| if(!empty($sFiltro)) { | |
| $filtro .= " AND " . $sFiltro; | |
| } | |
| $vFiltro = array($filtro); | |
| $voObjeto = $oFachada->getAllSeg_usuario(null, $vFiltro); | |
| if (is_array($voObjeto)) { | |
| return $sMensagem; | |
| } else { | |
| return null; | |
| } | |
| } | |
| #VALIDAR LOGIN EXISTENTE | |
| static function Campos($vValidarCampos) { | |
| if (is_array($vValidarCampos) && count($vValidarCampos) > 0) { | |
| // $nMsgExistente = 0; | |
| // foreach ($vValidarCampos as $sMsg) { | |
| // if (!empty($sMsg) && $sMsg != NULL) { | |
| // $nMsgExistente++; | |
| // } | |
| // } | |
| // if ($nMsgExistente > 0) { | |
| // if (is_array($nMsgExistente)) { | |
| // $sMsgCompleta = | |
| // } else { | |
| // $sMsgCompleta = $vMsg; | |
| // } | |
| return implode('</br>', array_filter($vValidarCampos)); | |
| // $_SESSION["msg"] = $sMsgCompleta; | |
| // header("Location: $sDirecionar"); | |
| // if (!empty($sDirecionar)) { | |
| // Mensagem::Disparar('error', array_filter($vValidarCampos), $sDirecionar); | |
| // } else { | |
| // return array_filter($vValidarCampos); | |
| // } | |
| } else { | |
| return null; | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment