Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Nerdmonkey

Criando CRUD

Recommended Posts

Eu aqui de novo mais uma vez pra pedir humildemente uma xícara de café e um ajuda no código.

Seguinte, esse trecho do código está sendo o problema:

$this->checkField($field);
$this->getError();

if($this->getError() == false):

Não deveria inserir porque não estou informando nenhum campo apenas submeto o formulário.

// List main
if($post['action'] == 'list-main'):
$validate = new \api\controllers\includes\Validate;

// Crete table and data using a prefix
$table = $prefix . 'list_main';
$data = [
'main_name' => $post['main_name'],
'main_type' => $post['main_type'],
'main_status' => $post['main_status']
];

var_dump($validate->insertRow($table,$data,$post));
endif;

É aqui que eu queria chegar. Ele faz o cadastro sem validar os campos, como se o ponteiro pulasse o trecho do primeiro código acima:

array(1) {
["success"]=>
string(30) "Registro inserido com sucesso."
}

Validate.php

<?php
namespace api\controllers\includes;

use api\controllers\db\Delete;

class Validate
{
/* @var string $error */
private $error;

/** @var string $success */
private $success;

/**
* Check if a field pass by param is null
* @param $field
* @return mixed
*/
private function checkField($field)
{
foreach($field as $key => $val):
if(empty($val)):
$this->error[] = ['error' => "Campo {$val} obrigatório"];
endif;
endforeach;

return $this->error;
}

/**
* Obtain the message error
* @return string
*/
private function getError()
{
if(!empty($this->error)):
foreach($this->error as $error):
$this->error[] = $error;
endforeach;

return json_encode($this->error);
else:
return false;
endif;
}

/**
* Insert a new registry for any modules
* Use it setting the table and all the data what you pretend insert in this table
* The table need to exists in database
* and the variable $data is the columns on this table
* @param $table
* @param $data
* @param $field
* @return mixed
*/
public function insertRow($table,array $data, array $field)
{
$this->checkField($field);
$this->getError();

if($this->getError() == false):
$create = new \api\controllers\db\Create;
$create->ExeCreate($table,$data);

if($create->getResult()):
$this->success = ['success' => "Registro inserido com sucesso."];
return $this->success;
else:
$this->error = ['error' => "Falha técnica. Entre em contato com o administrador."];
return $this->error;
endif;
endif;

$this->resetValues();
}

/**
* Delete the selected rows
* @param array $rows
* @param $table
* @return array|string
*/
public function delete(array $rows, $table)
{
if(empty($rows)):
$this->error = ['error' => 'Selecione um ou mais registros para excluir'];
return $this->error;
else:
$delete = new Delete;

foreach($rows as $row):
$delete->ExeDelete($table,'WHERE id = :id',"id={$row}");
endforeach;

if($delete->getResult()):
$this->success = ['success' => 'Registro(s) excluído(s) com sucesso.'];
return $this->success;
endif;
endif;
}

/**
* Reset default values and cleanup the system memory
*/
private function resetValues()
{
$this->error = null;
$this->success = null;
}
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Aposto uma xícara de café que você criou um form assim:

<form action="??" method="POST">
	<input type="submit" name="action" value="list-main">
</form>

Ou:

<form action="??" method="POST">
	<input type="hidden" name="action" value="list-main">
</form>

Desta forma sempre existirá aquilo não acha ? sendo assim inutilizando seu if, e use isset, provavelmente você ja sabe disto :v

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.