08-11-2020, 01:38 AM
I am trying to validate Entity by calling Model->validate( Entities\Promocodes $promo ).
But It looks like `Model->validate( Array $promo )` is the only option for this?
I have an Entitiy class:
And I have a model with validation rules. Which works great, if I pass an Array to it.
But it will always return true, if I pass Entities class Object.
Model with validation rules:
This will return false, as expected:
The problem
I understand, that passing Entities Object to Model may not be possible by design. But this is confusing in that case, because it should not return True anyway!
I have workaround by always using Array for data fill, validate, but I would like to know, if there is sych Entities validation available, or this should be posted as feature request?
But It looks like `Model->validate( Array $promo )` is the only option for this?
I have an Entitiy class:
PHP Code:
namespace App\Entities;
use CodeIgniter\Entity;
use App\Models\PromocodesModel;
class Promocodes extends Entity
{
// Anything else here is empty, no need for that, right?
}
And I have a model with validation rules. Which works great, if I pass an Array to it.
But it will always return true, if I pass Entities class Object.
Model with validation rules:
PHP Code:
namespace App\Models;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;
use CodeIgniter\Validation\ValidationInterface;
use Config\Services;
class PromocodesModel extends Model
{
protected $table = 'promo_codes';
protected $primaryKey = 'id';
protected $returnType = Promocodes::class;
protected $useSoftDeletes = true;
protected $allowedFields = [
'code',
'valid_from',
'valid_to',
];
protected $useTimestamps = true;
// These are validation rules !!! min Length: 3 !
protected $validationRules = [
'code' => 'required|is_unique[promo_codes.code]|alpha_numeric_space|min_length[3]',
];
This will return false, as expected:
PHP Code:
model('PromocodesModel')->validate(['code'=> 'a']); // min length must be 3!
The problem
PHP Code:
// Validating Entities object Will ALWAYS return TRUE!
model('PromocodesModel')->validate(new Entities\Promocodes(['code' => 'aa']));
// Will NOT insert/save values.
model('PromocodesModel')->save(new Entities\Promocodes(['code' => 'aa']));
I understand, that passing Entities Object to Model may not be possible by design. But this is confusing in that case, because it should not return True anyway!
I have workaround by always using Array for data fill, validate, but I would like to know, if there is sych Entities validation available, or this should be posted as feature request?