Welcome Guest, Not a member yet? Register   Sign In
Validate file if a select is...
#1

(This post was last modified: 03-16-2021, 01:08 PM by pippuccio76.)

i , sorry for english , i want validate a form with a  select and file , only if select  value is 6 the file must be uploaded .

How can i create this rule  ?

this is my code:

Code:
 <select id='id_id_stato_pratica'  class="form-control border border-warning mxn-shadow-input mb-4" name='id_stato_pratiche' >
                                  
                                  
       <option value='4'> FIRMATA </option>

       <option value='5'> IN LAVORAZIONE </option>
                                
       <option value='6'>  COMPLETATA </option>

</select>

<input type="file" name="pratica" id='file_pratica'>

   


This is my validation but don't work :

Code:
    public function validate_pratica_completata(string $str,string $fields, array $data)
    {

        if($data['id_stato_pratiche'] == 5){

            return true;

        }elseif ($data['id_stato_pratiche'] == 6 AND isset($data['pratica'])) {

            return true;

        }else{

            return false;
        }
         
    }
Reply
#2

(This post was last modified: 03-17-2021, 02:18 AM by iRedds.)

If you upload a file, then information about it is in a separate array.
Use a request instance instead of $data.

PHP Code:
$file Config\Services::request()->getFile('field_name');

if (
$file !== null && $file->valid()) { // file uploaded without errors 
  // ....



https://codeigniter.com/user_guide/libra...files.html
Reply
#3

(03-17-2021, 02:16 AM)iRedds Wrote: If you upload a file, then information about it is in a separate array.
Use a request instance instead of $data.

PHP Code:
$file Config\Services::request()->getFile('field_name');

if (
$file !== null && $file->valid()) { // file uploaded without errors 
  // ....

how can i do this in validation?
Reply
#4

You can create custom validation rules.
https://codeigniter4.github.io/CodeIgnit...stom-rules
Reply
#5

(This post was last modified: 03-18-2021, 12:50 AM by pippuccio76.)

(03-17-2021, 05:21 PM)kenjis Wrote: You can create custom validation rules.
https://codeigniter4.github.io/CodeIgnit...stom-rules

The question is how can i pass file to validation ? I must control if a file is updated if select value is 6
Reply




Theme © iAndrew 2016 - Forum software by © MyBB