CodeIgniter Forums
set_value() is working but set_select() not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: set_value() is working but set_select() not working (/showthread.php?tid=88432)

Pages: 1 2


set_value() is working but set_select() not working - sknair143 - 09-07-2023

Hello,

I'm trying to use set_value() and set_select(), Everything is correct but set_select() is not working.

Form :

Code:
<select name="doc_type_id" class="form-select">
                    <option value="">Select</option>

                    <?php foreach ($doc_types as $doc): ?>
                        <option value="<?= $doc['id']; ?>" <?= set_select('doc_type_id', $doc['id']) ?>> <?= $doc['name']; ?>
                        </option>
                    <?php endforeach; ?>
                </select>

Model :

Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class DocumentsModel extends Model
{

    protected $table = 'documents';

    protected $returnType = '\App\Entities\DocumentsEntity';

    protected $allowedFields = ['no', 'expiry_date', 'doc_type_id', 'doc_file'];

    // Dates
    protected $useTimestamps = true;

    protected $validationRules = [

        'no' => 'required',
        'expiry_date' => 'required',
        'doc_type_id' => 'required',
        'doc_file' => 'uploaded[doc_file]|ext_in[doc_file,png,jpg,gif]|max_size[doc_file,800]',

    ];
    protected $validationMessages = [

        'no' => [

            'required' => 'Please enter valid doc no',

        ],

        'expiry_date' => [

            'required' => 'Please select a valid date',

        ],


        'doc_type_id' => [

            'required' => 'Please select a document type.',

        ],


        'doc_file' => [

            'uploaded' => 'Please upload a valid file.',
            'ext_in' => 'Invalid file type',
            'max_size' => 'File should be less than 800kb.'

        ],


    ];


    public function getDocs()
    {

        $this->builder()
            ->select('a.id as doc_id, a.no as doc_no, a.expiry_date as doc_expiry, b.name as doc_type, a.doc_file as doc_file, a.updated_at as updated_at')
            ->from('documents as a')
            ->join('documents_type as b', 'a.doc_type_id = b.id', 'INNER')
            ->groupBy('a.id');

        return $this;


    }


}


Here set_value() function is working as expected. but set_select() is not working, the same i used in another form is working fine.


RE: set_value() is working but set_select() not working - sammyskills - 09-07-2023

What exactly are you trying to achieve?

And why not show your controller code also?


RE: set_value() is working but set_select() not working - sknair143 - 09-07-2023

(09-07-2023, 08:11 AM)sammyskills Wrote: What exactly are you trying to achieve?

And why not show your controller code also?



Controller :


Code:
public function createDocument()
    {

        if ($this->request->getPost()) {
            $model = new DocumentsModel;
            $documents = new DocumentsEntity;

            $documents->no = $this->request->getPost('no');
            $documents->doc_type_id = $this->request->getPost('doc_type_id');
            $documents->expiry_date = $this->request->getPost('expiry_date');
            $documents->doc_file = $this->request->getFile('doc_file');

            if ($model->save($documents)) {

                return redirect()->back()
                    ->with('success', "Document added success")
                    ->withInput();
            } else {

                return redirect()->back()
                    ->with('warning', 'Please fix following errors!')
                    ->with('errors', $model->errors())
                    ->withInput();

                // return redirect()->back()
                //    ->with('success', "Document added success")
                //    ->withInput();
            }


        }

        $doc_types = new DocumentTypesModel;
        $doc_types = $doc_types->findAll();

        return view('employees/documents/create', [
            'doc_types' => $doc_types,
        ]);
    }


What im trying to achieve is, I have a form to add employee's supporting documents like ID Card, Passport etc... So in the form I have one select box and other input boxes. I need to keep the old entered value to the field if validaiton fails. So for that i use set_select() for select box and set_value() for input boxes.

set_value() is not working.


RE: set_value() is working but set_select() not working - InsiteFX - 09-07-2023

Your problem is your passing an object array to the view from find not an associated array
PHP Code:
// so to access just use:
<option value="<?= $doc->id; ?>" <?= set_select('doc_type_id'$doc->id?><?= $doc->name?>



RE: set_value() is working but set_select() not working - sknair143 - 09-07-2023

(09-07-2023, 10:26 PM)InsiteFX Wrote: Your problem is your passing an object array to the view from find not an associated array
PHP Code:
// so to access just use:
<option value="<?= $doc->id; ?>" <?= set_select('doc_type_id'$doc->id?><?= $doc->name?>



No. because the option values are getting printed. When i use your code it gives me error like :

Code:
Attempt to read property "id" on array



RE: set_value() is working but set_select() not working - sammyskills - 09-07-2023

Can you confirm by adding the code below, somewhere in your code, and run the validation again?

PHP Code:
<?= set_value('doc_type_id'?>

Just to confirm that the value is returned to the form if validation fails.

PS:

I see that you are using Entity Classes in your controller (DocumentsEntity).

Instead of adding the properties individually, you can use the fill() method to have the properties filled automatically for you, like so:

PHP Code:
$document = new DocumentsEntity();
$document->fill($this->request->getPost()); 

See docs.


RE: set_value() is working but set_select() not working - sknair143 - 09-08-2023

(09-07-2023, 11:56 PM)sammyskills Wrote: Can you confirm by adding the code below, somewhere in your code, and run the validation again?

PHP Code:
<?= set_value('doc_type_id'?>

Just to confirm that the value is returned to the form if validation fails.

PS:

I see that you are using Entity Classes in your controller (DocumentsEntity).

Instead of adding the properties individually, you can use the fill() method to have the properties filled automatically for you, like so:

PHP Code:
$document = new DocumentsEntity();
$document->fill($this->request->getPost()); 

See docs.

I checked. The value is returned is NULL


If i use fill() method, how i can process the uploaded doc file ?


RE: set_value() is working but set_select() not working - sammyskills - 09-08-2023

If the value is null, then that means that it is not being sent in the first place. Try adding this in your controller, before the checks, to see what your form sends:

PHP Code:
dd($this->request->getPost()); 



RE: set_value() is working but set_select() not working - sknair143 - 09-08-2023

(09-08-2023, 12:13 AM)sammyskills Wrote: If the value is null, then that means that it is not being sent in the first place. Try adding this in your controller, before the checks, to see what your form sends:

PHP Code:
dd($this->request->getPost()); 

Code:
$this->request->getPost() array (3)
doc_type_id => string (1) "2"
no => string (8) "sadasdsa"
expiry_date => string (10) "2023-09-15"

It returns here.


RE: set_value() is working but set_select() not working - sammyskills - 09-08-2023

(09-08-2023, 12:07 AM)sknair143 Wrote: If i use fill() method, how i can process the uploaded doc file ?

Oh, I didn't notice that you used the getFile() method. Which brings me to wonder how you were or wanted to save the doc file in the database?