Doubt with set value (codeigniter)

Asked

Viewed 232 times

0

I have a form that after the validation using form_validation, I would like that if errors occurred, that when showing the view, return to the value I previously selected in form_dropdown, as well as with set_value when it is a form_input.

I didn’t find anything about it and I need to say, follow my codes below:

view (which has form_dropdown):

echo '<div class="form-group">';
      echo form_label('Tipo de Produto: *', 'tipoproduto');
      echo form_dropdown('tipoproduto', $options_tipovinhos, null, array('class' => "form-control"));
      echo '<div class="invalid-feedback">Por favor, preencha esse campo.</div>';
      echo '</div>';

control (which has the validation):

public function cadastrar(){
        $this->load->library('form_validation');

        //regras de validação
        $this->form_validation->set_rules('tipoproduto', 'Tipo de Produto', 'trim|required|max_length[50]|min_length[1]');

        //teste de validação campos
        if($this->form_validation->run() == FALSE):
            $dados = [
                'titulo' => 'Cadastro de Produtos - WineApp',
                'formerror' => validation_errors(),
                'fotoerror' => null,
                'options_paises' => $this->ProdutosF_model->selectPais(),
                'options_tipovinhos' => $this->ProdutosF_model->selectTipodeProdutos()
            ];
            $this->template->show("cadastroprodutos.php", $dados);

model: (where I build the options you have in form_dropdown, not to put a lot of foreach code in the view and reuse this function)

    //pega todos tipos de vinho
    public function getTipoVinho(){
        return $this->db->order_by('nome_TipoProduto')->get('tb_TipoProduto');
    }

    //cria um select com todos tipos de produtos
    public function selectTipodeProdutos(){

        $produtos = null;
        $tipodeprodutos = $this->getTipoVinho();

        foreach($tipodeprodutos -> result() as $tipodeproduto){
            $produtos .= "<option value='{$tipodeproduto->nome_TipoProduto}'>{$tipodeproduto->nome_TipoProduto}</option>";
        }

        return $produtos;

    }

My question is if it is possible to use something like a set_value in form_dropdown, taking into account that I am populating it in this way that I showed with the above codes.

2 answers

0

You just use the set_value in the 3 function parameter form_dropdown. In your example you put null.

echo '<div class="form-group">';
echo form_label('Tipo de Produto: *', 'tipoproduto');
echo form_dropdown('tipoproduto', $options_tipovinhos, set_value('tipoproduto'), array('class' => "form-control"));
echo '<div class="invalid-feedback">Por favor, preencha esse campo.</div>';
echo '</div>';
  • Opa Kayo, beauty? So man, that way was not, that’s why I do not understand. I read the documentation and understood that it was that way. Can it be because I’m assembling the options elsewhere than in the view? I’ve assembled in the model pq I use this form_dropdown on several screens

0

Are you dealing with the method of the library, and it has its own requirements:

form_dropdown([$name = ''[, $options = array()[, $Selected = array()[, $extra = '']]]])

In the documentation there are 4 (four) parameters: $name (string), $options (array), $selected (array) e $extra (mixed).

Well, if you wear one array to define the list of products (you use $options_tipovinhos), data recovery will have to be done by manipulating that same array, as the documentation describes. Note that, not by chance, the third parameter ($selected, that you’re going through null) will always be a array. That means you will have to recover the data from $_POST to repopulate the FORM:

<?php
 $name = 'tipoproduto';
 $extra = [
  'class' => "form-control",
  'required' => true
 ];
 $options = [
  null        => 'Select one',
  'small'     => 'Small Shirt',
  'med'       => 'Medium Shirt',
  'large'     => 'Large Shirt',
  'xlarge'    => 'Extra Large Shirt',
 ];
 $selected = [//Tem que ser um array, pois o method form_dropdown() exige;
   set_value($name)//Se vc passar mais de um valor aqui, form_dropdown() vai automaticamente entender que isso é um select multiplo;
 ];
 echo form_label('Tipo de Produto:*', 'tipoproduto');
 echo form_dropdown($name, $options, $selected, $extra);
 echo '<div class="invalid-feedback">Por favor, preencha esse campo.</div>';

Cannot recover direct with set_value() because, as I said, the method form_dropdown() requires the third parameter to be a array, and set_value() returns a string (see here).

Browser other questions tagged

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