Welcome Guest, Not a member yet? Register   Sign In
Model validation vs validationService and required field
#1

Hello. 
I would like to get some précisions with the Model validation functions in CI4 
I’m experimenting a difference of comportement between Services::validation() and the validation of the Model object for the required rule. 
With Services::validation(), a required field will be checked and fail if the parameter is set and null or is not set.
With the validation function of the Model object, a required field will be checked only if the parameter is set. So, the rule will fail if the parameter is set and null. But if the parameter is not set, the validation rule will not fail. 
I haven’t found any data about this difference of comportment in the CI documentation. It may be interesting to have some words about that because this can be confusing. 
As I understand, validation in the Model ignore unseted fields to allow partials modifications of a database record. It make sense. But on another side a required field is a required field. It’s confusing that a required field rule is ignored in an update method. Maybe the update method should have a third parameter allowing partial rules check (unseted fields will be ignored) or full rule check (all rules will apply and unseted fields marked as required will raise an error) ?
Reply
#2

Just don't use the model for validation.
Think of it as just a repository.
Reply
#3

I think it is a bug that the validation passes without the required field.
Reply
#4

I'm not sure it's a bug.
Within the model->update function, if you want to update only one or two fields of your database record, it make sense that the function do not apply all required rules.
Otherwise thing's like that won't work :
PHP Code:
<?php
$data 
= [
    'active' => 1,
];
$userModel->update([123], $data); 

But i think it should be documented as an important notice in the Modeling Data » Using CodeIgniter’s Model documentation, because it can really be confusing that *required* rules can be ignored.
Reply
#5

Okay, I sent a PR https://github.com/codeigniter4/CodeIgniter4/pull/6459
Reply
#6

I sent another PR: https://github.com/codeigniter4/CodeIgniter4/pull/6588
You can use $cleanValidationRules property.
Reply
#7

(This post was last modified: 11-02-2022, 11:14 PM by BrusterThemand.)

Supposing I have Model Course

//A few of the attributes

        [Key]
        public int CourseId { get; set; }

        [Required(ErrorMessage = "Course title is required")]
        [StringLength(100, MinimumLength = 3, ErrorMessage = "Course title must be between {2} and   
    {1} characters long")]
        [DataType(DataType.Text)]
        [Display(Name = "Course Title")]
        public string CourseTitle { get; set; }

        [Required(ErrorMessage = "Course capacity is required")]
        [Display(Name = "Available places on course")]
        public int CourseCapacity { get; set; }
Would I replicate this exactly in an AddCourseViewModel with the same Data Annotations?

My understanding is the ViewModel will pass the data to the Model (I plan on using AutoMapper), if the data is validated on the omegle.2yu.co ViewModel does it still need validated again when its passed to the Model?

Also if no validation happens on the omeglz echat ViewModel that would mean someone could leave a required field empty? right?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB