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

Bug: Error in Seeder #2653

Closed
JoelCatantan opened this issue Mar 1, 2020 · 15 comments
Closed

Bug: Error in Seeder #2653

JoelCatantan opened this issue Mar 1, 2020 · 15 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@JoelCatantan
Copy link

JoelCatantan commented Mar 1, 2020

Direction
Record did not save upon seeding when there is validationRules property in model

Describe the bug
When seeding, the value became an array instead of string/null.

CodeIgniter 4 version
CI 4.0.2, Develop Branch

Affected module(s)
system\CodeIgniter\Validation\Validation::processRules

Expected behavior, and steps to reproduce if appropriate
Put a validationRules in model then create a seeder and run. I have in_list rule in my validation and the I got an Exception:
Type: TypeError Message: Argument 1 passed to CodeIgniter\Validation\Rules::in_list() must be of the type string or null, array given, called in ...\system\Validation\Validation.php on line 279 Filename: ...\system\Validation\Rules.php Line Number: 191
However, when I remove the in_list rule, no error occurs and yet, no record had been saved

Context

  • OS: Windows 10 pro
  • Web server: Apache 2.4
  • PHP version 7.4
@JoelCatantan JoelCatantan added the bug Verified issues on the current code behavior or pull requests that will fix them label Mar 1, 2020
@MGatner
Copy link
Member

MGatner commented Mar 1, 2020

Can you please provide an example of what you are passing to the validation? E.g. what your Seed is actually trying to insert?

@michalsn
Copy link
Member

michalsn commented Mar 1, 2020

I was trying to reproduce this error but with no luck. I could get this error only when I was using an array as an input value.

@JoelCatantan
Copy link
Author

Thanks for taking a look at this. I tried to replicate this in my end but I figured out that I had it wrong by using fzaninotto/faker. I used $faker->randomElements instead of $faker->randomElement. The $faker->randomElements(['m', 'f']) will return something like array('m').

However, the second issue I stumbled upon is the matches[] rule. No error will be shown upon seeding and yet, no record will be stored;

    protected $validationRules = [
        'first_name' => 'required',
        'last_name' => 'required',
        'gender' => 'in_list[m,f]',
        'password' => 'required',
        'reenter' => 'matches[password]'
    ];

@michalsn
Copy link
Member

michalsn commented Mar 1, 2020

The rule for matches work ok.

The thing is there will be no valuation errors displayed by default. You have to write some code to handle this, so something like this should help (after insert):

if ($errors = $yourModel->errors())
{
    foreach ($errors as $error)
    {
        \CodeIgniter\CLI\CLI::write($error, 'red');
    }
}

My guess is that this is something related to your database structure? You don't have reenter field in the table but this field is on allowerField array or something? You should figure it out when you see an error.

@JoelCatantan
Copy link
Author

reenter is not included in $allowedFields. About your snippet, I think this will be in the code by default in the first place, no? Hmmm, anyway I will try your snippet and will digging more re: the issue.

Thanks!

@Tysonpower
Copy link

The rule for matches work ok.

The thing is there will be no valuation errors displayed by default. You have to write some code to handle this, so something like this should help (after insert):

if ($errors = $yourModel->errors())
{
    foreach ($errors as $error)
    {
        \CodeIgniter\CLI\CLI::write($error, 'red');
    }
}

My guess is that this is something related to your database structure? You don't have reenter field in the table but this field is on allowerField array or something? You should figure it out when you see an error.

I had a similar Error just now, i think it has a Problem if you have a field in your Validation Rule that you don't want to insert and also isn't in your allowedFields.

The behavior was different in the RC Version, there it worked fine to use Validation rules in a model that include fields that are not inserted into the DB.

@michalsn
Copy link
Member

michalsn commented Mar 8, 2020

@JoelCatantan things like data validation in seeders are up to you. From my perspective validating data in the seeder is not a very common thing since we usually know well what we're inserting. But I get it - for you, it's not the case... that's why data validation is not included by default.

@Tysonpower are you saying you get an error or you experienced an unexpected behavior? Could you by any chance provide some code sample that allows us to recreate this issue?

@Tysonpower
Copy link

@JoelCatantan things like data validation in seeders are up to you. From my perspective validating data in the seeder is not a very common thing since we usually know well what we're inserting. But I get it - for you, it's not the case... that's why data validation is not included by default.

@Tysonpower are you saying you get an error or you experienced an unexpected behavior? Could you by any chance provide some code sample that allows us to recreate this issue?

@michalsn Here is a short Code snippet, it is unexcepted/Different Behaviour then on the RC.

The Problem is the passwordrepeat Field, i need to check it to validate the Form but i don't insert it into the DB Of course. The RC Just ignored the passwordrepeat while saving because it isn't in the Entity and also not in the Allowed Fields list.

Controller

//Check Form
if($this->validate($userModel->validationRules)){

    if($userModel->save($new_user)){
	    // User Saved				
    } else {
	    // An Error occoured;
    }
}

userModel

protected $allowedFields = [
        'username', 'email', 'password', 'status', 'account_type', 'pro_until', 'clublog_usr', 'clublog_pwd', 'token'
];
protected $returnType    = 'App\Entities\User';
protected $useTimestamps = false;
protected $useSoftDeletes = true;
protected $validationRules = [
        'username'  => 'required|alpha_dash|min_length[3]|is_unique[user.username]',
        'email'  => 'required|valid_email|is_unique[user.email]',
        'password'      => 'required|min_length[8]'
        'passwordrepeat'      => 'required|min_length[8]|matches[password]'
];

@michalsn
Copy link
Member

michalsn commented Mar 8, 2020

Well... if passwordrepeat field is not in the $allowedFields list it won't be added. I tested it with version 4.0.2 and see no problem.

@Tysonpower
Copy link

Well... if passwordrepeat field is not in the $allowedFields list it won't be added. I tested it with version 4.0.2 and see no problem.

I just tested it with my code above again (entity has no passwordrepeat field btw.) and the model->errors() show the following:

[passwordrepeat] => The passwordrepeat field is required.

So your Solution is just adding the passwordrepeat Field to the Entity that is given to the model->save() right? I don't really like that but well, if that is the new Behaviour instead of the one in the RC than i'm okay with that and just store the Validation Rules for the Form in the Controller.

@michalsn
Copy link
Member

michalsn commented Mar 8, 2020

@Tysonpower you know what? Can I ask you to start a thread on the forum with a minimum amount of code that will make it possible to recreate this issue? I still can't reproduce it but maybe I am missing something here...

I don't want to spam everyone with our conversation since the originally reported issue is (I believe) already resolved.

@Tysonpower
Copy link

@Tysonpower you know what? Can I ask you to start a thread on the forum with a minimum amount of code that will make it possible to recreate this issue? I still can't reproduce it but maybe I am missing something here...

I don't want to spam everyone with our conversation since the originally reported issue is (I believe) already resolved.

Jup, no Problem. Will make a post when i got some time on hand

@Tysonpower
Copy link

@Tysonpower you know what? Can I ask you to start a thread on the forum with a minimum amount of code that will make it possible to recreate this issue? I still can't reproduce it but maybe I am missing something here...

I don't want to spam everyone with our conversation since the originally reported issue is (I believe) already resolved.

Post is currently waiting for approval, but here is the link already: https://forum.codeigniter.com/thread-75709.html

@MGatner
Copy link
Member

MGatner commented Mar 9, 2020

Judging from this thread and the forum post the issues here are resolved. @Tysonpower if you agree please close.

@Tysonpower
Copy link

Judging from this thread and the forum post the issues here are resolved. @Tysonpower if you agree please close.

I didn't create the ussue here, som i can't close it. But yeah it can be closed in my opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

4 participants