Codeigniter validation error

Asked

Viewed 91 times

0

I am trying to correct 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 this registering ALL.

How can I correct ?

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 -->

1 answer

0


I am returning to inform you that I have already solved the problem, simply I changed the positioning, 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'));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.