Welcome Guest, Not a member yet? Register   Sign In
set_value() is working but set_select() not working
#1

(This post was last modified: 09-07-2023, 06:08 AM by sknair143.)

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.
Reply
#2

What exactly are you trying to achieve?

And why not show your controller code also?
Reply
#3

(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.
Reply
#4

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?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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
Reply
#6

(This post was last modified: 09-08-2023, 12:03 AM by sammyskills.)

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.
Reply
#7

(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 ?
Reply
#8

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()); 
Reply
#9

(This post was last modified: 09-08-2023, 12:15 AM by sknair143.)

(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.
Reply
#10

(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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB