Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Set value on Array
#1

(This post was last modified: 10-16-2023, 07:44 AM by pippuccio76.)

HI , i have a form with a number of field text variable :

Code:
                <?php for ($i = 0; $i <$numMacchineLottoSpostabili ; ++$i) : ?>


                    <!-- INIZIO seriale -->

                    <div class='row '>
                        <div class='col-md-12'>
                            <label class='col-md-12 col-xs-12 control-label'>Seriale*</label>
                            <div class='col-md-12 col-xs-12'>
                                <input  type='text'  value='<?=set_value('seriale[]')?>'

                                <?php if (!$i): ?>
                                                                   

                                    required='required' 

                                <?php endif ?>

                                class='form-control seriali enter' name='seriale[]'  >
                            </div>
                        </div>

                    </div>


                    <!-- FINE seriale -->

                <?php endfor ?>

How can i set properly value='<?=set_value('seriale[]')?>' ? because in this way doen't work ... I try <?=set_value('seriale[<?=$i?>]')?> but same problem
Reply
#2

Example.

Before form:
$seriale = set_value("seriale", []);

In input:
echo $seriale[$i] ? $seriale[$i] : "';

P.S. Wrote on phone
Reply
#3

how can i set on error correctly :

this is my validation :

Code:
            $rules=
            [

                                    'seriale.*'=>[
                                                'rules'=>'required',
                                                'errors'=>[
                                                            'required'=>'Seriale {field} obbligatorio', 
                                                          ]
                                                ],

                                   
            ];


But error showed is :  Seriale seriale.* obbligatorio , can i insert in the error message the value passed by post too?
Reply
#4

See https://codeigniter4.github.io/userguide...all-errors
Replace {field}
Reply
#5

(This post was last modified: 09-22-2023, 02:16 AM by pippuccio76.)

(09-15-2023, 03:55 AM)ozornick Wrote: Example.

Before form:
$seriale = set_value("seriale", []);

In input:
echo $seriale[$i] ? $seriale[$i] : "';

P.S. Wrote on phone


There is something wrong , this is my code :

view :

Code:
<?php     


$seriale = set_value("seriale", []);


?>

<?php
$attributes = array('class' => 'col-md-12',
                    'id' => 'myform',
                    'name' => 'form_macchina',
                    'method' => 'POST',
                    'onSubmit' =>'return controllaSerialiDuplicati()'
       

                    );
echo form_open (base_url() .'/admin_Macchina/cambia_clienti_massivo',$attributes);

            ?>
        <?php if(!empty($validation) ) :?>


          <div class="alert alert-danger border-0 bg-danger  fade show py-2">
            <div class="d-flex align-items-center">
              <div class="font-35 text-white"><i class="bx bxs-message-square-x"></i>
              </div>
              <div class="ms-3">
                <div class="text-white"> <?= $validation->listErrors(); ?></div>
              </div>
            </div>
          </div>

        <?php endif ?>

<!-- INIZIO id_clienti -->


<div class='row'>

            <div class='col-md-12'>
                <label class='col-md-12 col-xs-12 control-label'>Cliente*</label>
                <div class='col-md-12 col-xs-12'>
                <select id='id_id_clienti'  class=' form-control form-control-lg select2' name='id_clienti' required>
                    <option value=''>Seleziona un valore...</option>
                    <?php foreach ($tutti_id_clienti as $v) :?>
                        <option value='<?=$v->id ?>'

                                <?php if($v->id==set_value('id_clienti')): ?>

                                    selected='selected'
                               
                                <?php endif ?>


                                <?php if(isset($_SESSION['id_cliente_selezionato']) AND $v->id==$_SESSION['id_cliente_selezionato']): ?>

                                    selected='selected'
                               
                                <?php endif ?>


                               
                        >

                            <?=$v->nome . ' - ' . $v->stato?>

                        </option>

                    <?php endforeach ?>
                </select>
                </div>
            </div>

</div>
<!-- FINE id_clienti -->




<div id="lotto1_50">
<?php for ($i = 1; $i <=50 ; ++$i) : ?>




            <div class="row">
                <div class="col-md-12">
                  <label class='col-md-12 col-xs-12 control-label'>Seriale <?=$i ?></label>
                  <div class='col-md-12 col-xs-12'>
                        <?php

                           
                            if(isset( $seriale[$i] )){


                                $val = $seriale[$i];
                           
                            }else{

                                $val ='';
                            }

                        ?>

                        <input  type='text'  value='<?=$val?>' <?php if ($i==1): ?>required='required'<?php endif ?>  id='id_class_seriale_<?=$i ?>'  class='seriali form-control form-control-lg' name='seriale[]' maxlength='80'  >
                  </div>
                </div>
            </div>

<?php endfor ?>


controller :

Code:
if($this->request->getMethod() === 'post')           
        {
            $validation =  \Config\Services::validation();

            $rules=
            [
                                    'id_clienti'=>[
                                                'label'=>'Id_clienti',
                                                'rules'=>'required',
                                                'errors'=>[
       
                                                            'required'=>'Cliente obbligatorio',
                                                         
                                                          ]
                                                ],
                                   
                                    'seriale.*'=>[
                                                //'label'=>"Seriale[]",
                                                'rules'=>'seriale_corretto_cambio_clienti|macchina_is_present_Picking|permit_empty',
                                                'errors'=>[

                                                            'seriale_corretto_cambio_clienti'=>'Il seriale.* non è un seriale corretto',
                                                            'macchina_is_present_Picking'=>'Il seriale.* non è presente in magazzino ',

                                                          ]
                                    ],
                                               
                                   

                                   
            ];



            //dd($rules);



            if($this->validate($rules)){


If  i insert  on field seriale 1 "seriale 1" and  on field seriale 2 "seriale 2" ( value that return falsse on seriale_corretto_cambio_clienti) .

When i send the post i have in field seriale 1 the value seriale 2 the other is empty .

edit until line :

found this error : $i start to 1 on field list

change

Code:
<?php

                            $n = $i-1;
                            
                            if(isset( $seriale[$n] )){


                                $val = $seriale[$n];
                            
                            }else{

                                $val ='';
                            }

                        ?>

--------------------------------------------------------------
The error message are two (but the same) :

Il seriale.* non è un seriale corretto

Il seriale.* non è un seriale corretto
Reply
#6

(This post was last modified: 10-16-2023, 07:43 AM by pippuccio76.)

Find solution:


Code:
                                   
                                    'seriale.*'=>[
                                                'label'=>"seriale",
                                                'rules'=>'seriale_corretto_cambio_clienti|macchina_is_present_Picking|permit_empty',
                                                'errors'=>[

                                                            'seriale_corretto_cambio_clienti'=>'Il seriale {value} non è un seriale corretto',
                                                            'macchina_is_present_Picking'=>'Il seriale.* non è presente in magazzino ',

                                                          ]
                                    ],
Reply




Theme © iAndrew 2016 - Forum software by © MyBB