Created
October 31, 2016 18:18
-
-
Save anonymous/21a0eb97c4bde050fbbf6b185d7a5fa4 to your computer and use it in GitHub Desktop.
Série RestServer - Método Update
This file contains 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
/* | |
* Método que irá fazer a validação dos dados e processar o update na tabela | |
* recebe como parâmetro o array com os dados vindos do formulário | |
*/ | |
function Update($field, $value, $dados) | |
{ | |
if (!isset($dados) || !isset($field) || !isset($dados)) { | |
$response['status'] = false; | |
$response['message'] = "Dados não informados."; | |
} else { | |
// setamos os dados que devem ser validados | |
$this->form_validation->set_data($dados); | |
// definimos as regras de validação | |
$this->form_validation->set_rules('nome','Nome','required|min_length[2]|trim'); | |
$this->form_validation->set_rules('email','Email','required|valid_email|trim'); | |
$this->form_validation->set_rules('senha','Senha','min_length[6]|trim'); | |
$this->form_validation->set_rules('biografia','Biografia','trim'); | |
// executamos a validação e verificamos o seu retorno | |
// caso haja algum erro de validação, define no array $response | |
// o status e as mensagens de erro | |
if ($this->form_validation->run() === false) { | |
$response['status'] = false; | |
$response['message'] = validation_errors(); | |
} else { | |
if (isset($dados['senha'])) { | |
// criptografamos a senha | |
$dados['senha'] = EncryptPassw($dados['senha']); | |
} | |
//executamos o update | |
$this->db->where($field, $value); | |
$status = $this->db->update('usuarios', $dados); | |
// verificamos o status do insert | |
if ($status) { | |
$response['status'] = true; | |
$response['message'] = "Usuário atualizado com sucesso."; | |
} else { | |
$response['status'] = false; | |
$response['message'] = $this->db->error_message(); | |
} | |
} | |
} | |
// retornamos as informações sobre o update | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment