Welcome Guest, Not a member yet? Register   Sign In
CODEIGNITER 4 IN-MODEL VALIDATION AND SAVE() NOT WORKING.
#1

(This post was last modified: 07-16-2021, 05:03 AM by InsiteFX.)

I'm trying to work with CodeIgniter's Model with save(), This is introduced new in the CodeIgniter 4, moreover, when I'm directly calling the below method data is not getting saved in the database
$this->itemModel->save($data)

I'm getting the below error on the browser
ErrorException

Attempt to read property "errno" on bool


<CONTOLLER - INVENTORY.PHP >>

PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Controller;
use 
App\Models\InventoryModel;

class 
Inventory extends Controller{
    public $itemModel;
    public function __construct(){
        $this->itemModel = new InventoryModel();
        helper(["url","form"]);
    }
  
    
public function addInventory(){
        if($this->request->getMethod()== "post"){
            $data = [
                "name" => $this->request->getVar("itemname",FILTER_SANITIZE_STRING),
                "quantity" => $this->request->getVar("itemquantity"),
                "remarks" => $this->request->getVar("itemremark",FILTER_SANITIZE_STRING)
            ];
            #$this->itemModel->save($data) #DATA NOT GETTING STORED IN DATABASE
            if($this->itemModel->save($data)=== true)
            {
                session()->setTempData("success","Item added to the inventory sucessfully !");
                return redirect()->to(current_url());
            }          
        
}
        return view('inventory',["errors"=>$this->itemModel->errors()]);
    }#EOC AddItem()
          


<<MODEL:- INVENTORYMODEL.PHP >>

PHP Code:
<?php
namespace App\Models;
use 
CodeIgniter\Model;

class 
InventoryModel extends Model {
    protected $table      'inventory';
    protected $primaryKey 'id';
    protected $useAutoIncrement true;
    protected $returnType    'array';
    protected $allowedFields = ['name''quantity','remarks'];

    protected $validationRules    = [
        "itemname" => "required",
        "itemquantity" => "required",
        "itemremarks" => "required"
    ];



<< VIEW: INVENTORY.PHP>>

Code:
<div class="form-panel w3-padding-16 w3-round">
                <?php if (! empty($errors)) : ?>
                <div class="alert alert-danger">
                <?php foreach ($errors as $field => $error) : ?>
                <p><?= $error ?></p>
                <?php endforeach ?>
                </div>
                <?php endif ?>
                <?php if(session()->getTempData("success")):?>
                <?= '<div class="w3-panel w3-green w3-card-4 w3-round w3-padding-16">'.session()->getTempData("success").'</div>' ?>
                <?php endif; ?>
                <?= form_open(current_url(),'role="form" class="form-horizontal style-form"'); ?>
                <div class="form-group">
                    <label class="col-lg-2 control-label"><sup class="w3-text-red">*</sup>Item Name</label>
                  <div class="col-lg-10">
                    <input type="text" placeholder="" id="f-name" class="form-control" name="itemname" >
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-lg-2 control-label"><sup class="w3-text-red">*</sup>Item Quantity</label>
                  <div class="col-lg-10">
                    <input type="number" placeholder=""  class="form-control" min="1" name="itemquantity" >
                  </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 control-label">Item Remarks </label>
                  <div class="col-lg-10">
                    <input type="text" placeholder="" id="l-name" class="form-control" name="itemremarks" >
                  </div>
                </div>             
                <div class="form-group">
                  <div class="col-lg-offset-2 col-lg-10">
                      <input type="submit" name="submit" value="Submit" class="btn btn-theme"/>
                      <input type="reset"  value="Clear" class="btn  w3-red"/>
                  </div>
                </div>
            <?= form_close();?>       
Reply
#2

I fixed your post next time please do not use cut copy and paste use the code tags in the editor menu.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB