Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nowackipawel opened this issue Oct 28, 2018 · 0 comments · Fixed by #1373
Closed

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

nowackipawel opened this issue Oct 28, 2018 · 0 comments · Fixed by #1373

Comments

@nowackipawel
Copy link
Contributor

nowackipawel commented Oct 28, 2018

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

nowackipawel added a commit to nowackipawel/ci4-old that referenced this issue Oct 28, 2018
Turning cast for db save/update and validation inside of Model's::classToArray , will fix codeigniter4#1372 and probably codeigniter4#1359
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant