Error validation Codeigniter

0

I'm trying to fix validation problems of my code, at first I did the test only with the label "Name", where it is required and can not contain numbers or characters, but is registering EVERYTHING.

How can I fix it?

Index Controller

public function submit(){
        $result = $this->m->submit();

        // TESTE DE VALIDAÇÃO
        $this->form_validation->set_rules('txt_nome', 'Nome', 'trim|required');

        if( $this->form_validation->run() ){

            // echo 'Success';
            $this->session->set_flashdata('msg_sucesso', 'Registro adicionado com sucesso!');

        }else{

            //echo validation_errors();
            $this->session->set_flashdata('erro_msg', 'Erro ao adicionar registro.');

        }

        redirect(base_url('estado'));

    }

Form

<form action="<?php echo base_url('estado/submit'); ?>" method="post" class="form-horizontal">

        <div class="form-group">
            <label for="nome" class="col-md-1 text-right">Nome</label>
            <div class="col-md-10">

                <input type="text" name="txt_nome" value="<?php echo set_value('txt_nome'); ?>" class="form-control" required>
                <td><?php echo form_error('txt_nome'); ?></td>

            </div><!-- col-md-10 -->
        </div><!-- form-group -->


        <div class="form-group">
            <label class="col-md-1 text-right"></label>
            <div class="col-md-10">
                <input type="submit" name="btnCadastro" class="btn btn-primary" value="Cadastrar">
            </div><!-- col-md-10 -->
        </div><!-- form-group -->

        </form><!-- form -->
    
asked by anonymous 24.10.2018 / 02:10

1 answer

0

I'm returning to let you know that I've been able to solve the problem, I've simply changed the position, as follows:

// TESTE DE VALIDAÇÃO
        $this->form_validation->set_rules('txt_nome', 'Nome', 'trim|required|min_length[3]|max_length[11]|alpha');

        if( $this->form_validation->run() ){

            $result = $this->m->submit();


            $this->session->set_flashdata('msg_sucesso', 'Registro adicionado com sucesso!');

        }else{

            $this->session->set_flashdata('erro_msg', 'Erro ao adicionar registro.');

        }       
        redirect(base_url('estado'));
    
25.10.2018 / 06:10