CodeIgniter Forums
How to restrict hidden input field in save() method. - 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: How to restrict hidden input field in save() method. (/showthread.php?tid=89006)



How to restrict hidden input field in save() method. - sknair143 - 12-20-2023

Hello Everyone,

I have a from where i get some inputs from the user, i use same for for create and edit. In edit case i need to use hidden input field for some id reference purpose. But when i save using the save() method the hidden input field also passing into the database that gives me error.

Form :

Code:
  <div class="mb-3 col-md-3">
                                <label for="division_id" class="form-label">Divison</label>
                               
                                <select id="divisionId" name="division_id" class="form-select">
                                <option value="">Select</option>   
                                </select>
                                <span class="error">
                                    <?= session('emp_error.division_id') ?>
                                </span>
                                <input type="hidden" name="edit_division_id" id="edit_division_id" value="<?=isset($employee->division_id) ? $employee->division_id : ''?>">

                            </div>



Method :


PHP Code:
  if($this->request->getPost() ) {

            if ($this->model->save($this->request->getPost()) === false) {


                return redirect()->back()
                    ->with('warning''Please fix following errors.')
                    ->with('emp_error'$this->model->errors())
                    ->withInput();

            }
            else {

                




                
return redirect()->to('dashboard/employees')
                    ->with('success''Employee record created');
            



How i can restrict the hidden fields passed into the save() method ?


RE: How to restrict hidden input field in save() method. - kenjis - 12-20-2023

See https://codeigniter4.github.io/CodeIgniter4/libraries/validation.html#the-controller


RE: How to restrict hidden input field in save() method. - sknair143 - 12-20-2023

@kenjis

Thanks for your reply. Can you give a example. I'm new to codeigninter.


RE: How to restrict hidden input field in save() method. - kenjis - 12-20-2023

1. You can specify an array of field names like $this->request->getPost(['field1', 'field2']).
2. It is better to validate all user input data.


RE: How to restrict hidden input field in save() method. - sknair143 - 12-20-2023

(12-20-2023, 12:54 AM)kenjis Wrote: 1. You can specify an array of field names like $this->request->getPost(['field1', 'field2']).
2. It is better to validate all user input data.

@kenjis

So here i can exclude those hidden input fields right ?


RE: How to restrict hidden input field in save() method. - kenjis - 12-20-2023

Yes, if you pass an array with required fields to $this->request->getPost().