Welcome Guest, Not a member yet? Register   Sign In
Form validation on update
#1

[eluser]Tomer Simis[/eluser]
Hello, Im facing some problems with codeigniter and I need some help Sad

I have a function in my controller (categories/update) and this function checks if the id is empty, in case of false it loads a view, in case of true it loads the default view (where I select the category I want to update)

In this view there is a form, and here where the problem begins.

When I submit the form, it goes back to (categories/update) and because the id is not set, it loads the default view. The point is: the form validation doesn't work and neither the model's function "save_changes"

Here are the codes:

Controller:
Code:
function update($id = ''){
    $this->load->library('form_validation');
    $this->form_validation->set_message('required','O campo <strong> %s </strong> é obrigatório');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
            
        if(!empty($id) && is_numeric($id)){
            $data['titulo'] = 'BSS Direct - Alterando categoria';
            $data['categoria'] = $this->CategoriesModel->selectData($id);
            $this->load->view('header',$data);
            
            if($this->form_validation->run() == FALSE){
                $this->load->view('categories/form');
            }else{
                $this->CategoriasModel->save_changes($id,$_POST['nome']);
                redirect('index');
            }
        }else{
            $data['titulo'] = 'BSS Direct - Alterar categoria';
            $data['categorias'] = $this->CategoriesModel->selectAll();
            $this->load->view('header',$data);
            $this->load->view('categories/default');    
        }
    }

Model:
Code:
function selectData($id){
        $qr = mysql_query("SELECT id,nome FROM `bss`.`bss_categoria` WHERE id='$id'");
        return mysql_fetch_array($qr);
    }

function save_changes($id,$nome){
        $qr = mysql_query("UPDATE `bss`.`bss_categoria` SET nome='$nome' WHERE id='$id'");
    }

View:

form
Code:
&lt;/head&gt;
&lt;body id="edit"&gt;
<div id="topbar"> &lt;!-- Topbar --&gt;
<p>Seja bem-vindo <strong>&lt;?php echo $this->session->userdata('nome'); ?&gt; </strong> | <a href="http://localhost/BSS DIRECT/backend.php/index/deslogar">Sair</a></p>
</div> &lt;!-- Topbar --&gt;

<div id="header"> &lt;!-- Header --&gt;
    <img src="images/logo.jpg" alt="Home" />
</div> &lt;!-- Header --&gt;

<div id="left">

&lt;?php

$err = validation_errors();

if(!empty($err)){
    echo '<div id="aviso">
    <h3>Atenção !</h3><ul>
    '.$err.'</ul>
</div>';
}
    

echo form_open('categories/update');

?&gt;
    <fieldset name="geral" id="geral">
        <div class="box-left">
            <div class="box-tb">
                <img src="images/upd.jpg" alt="Alterar" />
                <h2>Alterando categoria</h2>
            </div>
                
            <ul>
            
                <li class="darker">
                    <label for="nome">Nome da categoria</label>
                    &lt;input type="text" name="nome" value="&lt;?php echo $categoria['nome']; ?&gt;" /&gt;
                </li>
            
            </ul>
            
        </div>
    </fieldset>
    
&lt;input type="submit" value="Alterar categoria"  /&gt;

<br style="clear:both;" />

&lt;/form&gt;
</div>

<div id="rodape">

<p>Todos os direitos reservados <br />
    <strong>BSS Direct</strong></p>

</div>


&lt;/body&gt;
&lt;/html&gt;

default
Code:
&lt;/head&gt;
&lt;body id="edit"&gt;
<div id="topbar"> &lt;!-- Topbar --&gt;
<p>Seja bem-vindo <strong>&lt;?php echo $this->session->userdata('nome'); ?&gt; </strong> | <a href="http://localhost/BSS DIRECT/backend.php/index/deslogar">Sair</a></p>
</div> &lt;!-- Topbar --&gt;

<div id="header"> &lt;!-- Header --&gt;
    <img src="images/logo.jpg" alt="Home" />
</div> &lt;!-- Header --&gt;

<div id="left">

        <div class="box-left">
            <div class="box-tb">
                <img src="images/upd.jpg" alt="Adicionar" />
                <h2>Remover categoria</h2>
            </div>          
            
            <table width="100%" cellspacing="2" border="1">
            
            <thead>
                <tr class="darkerTR">
                    <td>Id</td>
                    <td>Nome da categoria</td>
                    <td>Produtos</td>
                    <td></td>
                </tr>
            </thead>
            <tbody>
            
            &lt;?php
            
            $i = 0;
            
            while($cat = mysql_fetch_array($categorias)){
            $i++;
            
            $num = $this->CategoriasModel->countProdutos($cat['id']);
            
            if($i % 2 == 0){
                echo '<tr class="darkerTR">
                          <td>'.$cat['id'].'</td>
                          <td>'.$cat['nome'].'</td>
                          <td>'.$num.'</td>
                          <td class="last">'.anchor('categories/update/'.$cat['id'],'<img src="images/updateDark.jpg"/>').'</td>
                      </tr>';
            }else{
                echo '<tr>
                          <td>'.$cat['id'].'</td>
                          <td>'.$cat['nome'].'</td>
                          <td>'.$num.'</td>
                          <td class="last">'.anchor('categories/update/'.$cat['id'],'<img src="images/update.jpg" />').'</td>
                      </tr>';
            }
            
            }
            
            ?&gt;
            </tbody>
            </table>
        
            
            
        </div>
    
<br style="clear:both;" />
</div>
<div id="rodape">
<p>Todos os direitos reservados <br />
    <strong>BSS Direct</strong></p>
</div>
&lt;/body&gt;
&lt;/html&gt;

Please guys, help me. I'm getting crazy :ahhh:
Cheers!
#2

[eluser]WebsiteDuck[/eluser]
The $id variable never gets set, unless you call update($id) from your alterar function, but since the form posts directly to update, $id never gets set

Code:
echo form_open('categories/update'); //no ID specified
#3

[eluser]Tomer Simis[/eluser]
Sorry, the views were wrong at some parts. I have edited and now its ok.

Actual form open:
Code:
echo form_open('categories/update/'.$this->uri->segment(3)); //categories/update/4

Its not working, this time it only comes back to the form page with the informations, but when a submit I didn't get any validation error.
please, help me guys Sad
#4

[eluser]WebsiteDuck[/eluser]
You need validation rules like this:

Code:
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
#5

[eluser]Tomer Simis[/eluser]
It's set on form_validation.php in the config folder. I have used like this on another controller, so its not the problem.
#6

[eluser]Tomer Simis[/eluser]
Ok guys, Im back. I've solved the problem Smile

Code:
function alterar(){
        
            if(!$this->uri->segment(3)){
                $data['titulo'] = 'BSS Direct - Alterar categoria';
                $data['categorias'] = $this->CategoriasModel->selectAll();
                $this->load->view('header',$data);
                $this->load->view('categorias/alterar');    
            }else{    
                $this->load->library('form_validation');
                $this->form_validation->set_message('required','O campo <strong> %s </strong> é obrigatório');
                $this->form_validation->set_error_delimiters('<li>', '</li>');
                
                $data['titulo'] = 'BSS Direct - Alterar categoria';
                $data['categoria'] = $this->CategoriasModel->selectDados($this->uri->segment(3));
                $this->load->view('header',$data);            
                
                    if($this->form_validation->run('categorias/alterar') == TRUE){
                        $this->load->view('categorias/alterar');
                    }else{
                        echo $this->input->post('nome');
                        $this->load->view('categorias/alterar_form');
                    }
            }
    }

I've put a parameter on the form validation and it worked, the "auto run on a config file" didn't work.

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB