From d955f831cc63da3b50ffd642c1ffc14179598dd1 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 31 Aug 2022 06:52:30 +0900 Subject: [PATCH 1/4] docs: add a new section title --- user_guide_src/source/models/model.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index b88f1761d841..a0afd14f32a7 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -380,8 +380,11 @@ Cleans out the database table by permanently removing all rows that have 'delete .. literalinclude:: model/026.php +In-Model Validation +=================== + Validating Data -=============== +--------------- For many people, validating data in the model is the preferred way to ensure the data is kept to a single standard, without duplicating code. The Model class provides a way to automatically have all data validated @@ -454,7 +457,7 @@ and simply set ``$validationRules`` to the name of the validation rule group you .. literalinclude:: model/034.php Retrieving Validation Rules -=========================== +--------------------------- You can retrieve a model's validation rules by accessing its ``validationRules`` property: @@ -473,7 +476,7 @@ value an array of fieldnames of interest: .. literalinclude:: model/037.php Validation Placeholders -======================= +----------------------- The model provides a simple method to replace parts of your rules based on data that's being passed into it. This sounds fairly obscure but can be especially handy with the ``is_unique`` validation rule. Placeholders are simply From 962a0c0c07f495df8da781fc9bc7210173782bc9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 31 Aug 2022 06:58:34 +0900 Subject: [PATCH 2/4] docs: add note for validation --- user_guide_src/source/models/model.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index a0afd14f32a7..88f94057b9f6 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -390,6 +390,10 @@ For many people, validating data in the model is the preferred way to ensure the standard, without duplicating code. The Model class provides a way to automatically have all data validated prior to saving to the database with the ``insert()``, ``update()``, or ``save()`` methods. +.. important:: The validation in the model class only validate provided fields. + So when you set the rule ``required``, if you don't pass the required field data, + the validation won't fail. This is to avoid validation errors when updating only some fields. + The first step is to fill out the ``$validationRules`` class property with the fields and rules that should be applied. If you have custom error message that you want to use, place them in the ``$validationMessages`` array: From 9010060c6af11671b653ef202b72a8374ce99524 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 31 Aug 2022 07:02:16 +0900 Subject: [PATCH 3/4] docs: update RST format These are not PHP functions but methods. --- user_guide_src/source/models/model.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 88f94057b9f6..9f61e708beef 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -401,7 +401,11 @@ be applied. If you have custom error message that you want to use, place them in The other way to set the validation rules to fields by functions, -.. php:function:: setValidationRule($field, $fieldRules) +.. php:namespace:: CodeIgniter + +.. php:class:: Model + +.. php:method:: setValidationRule($field, $fieldRules) :param string $field: :param array $fieldRules: @@ -412,7 +416,7 @@ The other way to set the validation rules to fields by functions, .. literalinclude:: model/028.php -.. php:function:: setValidationRules($validationRules) +.. php:method:: setValidationRules($validationRules) :param array $validationRules: @@ -424,7 +428,7 @@ The other way to set the validation rules to fields by functions, The other way to set the validation message to fields by functions, -.. php:function:: setValidationMessage($field, $fieldMessages) +.. php:method:: setValidationMessage($field, $fieldMessages) :param string $field: :param array $fieldMessages: @@ -435,7 +439,7 @@ The other way to set the validation message to fields by functions, .. literalinclude:: model/030.php -.. php:function:: setValidationMessages($fieldMessages) +.. php:method:: setValidationMessages($fieldMessages) :param array $fieldMessages: From f58db312fe2c36b0261e2d900ee26d48d9be14a3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 3 Sep 2022 06:57:50 +0900 Subject: [PATCH 4/4] docs: fix incorrect explanation The cleanRules() method controls whether to remove validation rules or not. --- user_guide_src/source/models/model.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user_guide_src/source/models/model.rst b/user_guide_src/source/models/model.rst index 9f61e708beef..86b3de4549df 100644 --- a/user_guide_src/source/models/model.rst +++ b/user_guide_src/source/models/model.rst @@ -390,7 +390,7 @@ For many people, validating data in the model is the preferred way to ensure the standard, without duplicating code. The Model class provides a way to automatically have all data validated prior to saving to the database with the ``insert()``, ``update()``, or ``save()`` methods. -.. important:: The validation in the model class only validate provided fields. +.. important:: When you update data, the validation in the model class only validate provided fields. So when you set the rule ``required``, if you don't pass the required field data, the validation won't fail. This is to avoid validation errors when updating only some fields.