Skip to content

boolean cast in entity and validation error for required, in_list[0,1] #1372

Description

@nowackipawel

As we know we've got casting in entity class which could cast boolean values to integer (for database) and integers 0,1 to false, true from db.

And I am wondering how to fix that case:

My entity class:

<?php namespace App\Entities\File;

use CodeIgniter\Entity;

class FileConverted extends Entity
{
	protected $fco_id;
	protected $fco_is_active;

	protected $_options = [
		'casts' => [
			'fco_id'			=> 'integer',
			'fco_is_active'		=> 'boolean', //this cast
		],
		'dates'=> ['fco_created_at', 'fco_updated_at'],
		'datamap' => []
	];

and model:

<?php

class FileConvertedModel extends \CodeIgniter\Model
{
	protected $DBGroup 		= DB_ZTN_GROUP;
	protected $table 		= 't_file_converted';
	protected $primaryKey 	= 'fco_id';
	protected $returnType 	= 'App\Entities\File\FileConverted';

	protected $allowedFields = [
		'fco_is_active',
	];

	protected $validationRules 	= [
		'fco_fil_id'				=> 'required|is_natural_no_zero|less_than_equal_to[16777215]',
		'fco_is_active'				=> 'required|in_list[0,1]',
	];
}

and now from other model I am doing sth like this:


$file_converted->fco_fct_identifier = $file_conversion_task->fct_identifier;
$file_converted->fco_is_active = 0;
$file_converted->setConvertedName($file->guessExtension());
$file_converted_model->save($file_converted);

and I've got error:

The fco_is_active field is required.

bacause is_required gets FALSE instead of 0 which I assigned to fco_is_active.

For quick fix we could make new rule like 'valid_boolean', use it at one of the first rules and change $value inside of processRules to false -> 0, true -> 1 for other validation rules like required or in_list[0,1]

What do you think? Is it a bug o proper behavior ?

Or is any way to get casts of saved entity in Rules class methods without passing every cast through Model::save() , Validation::run() and Validation::processRules() ?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions