Skip to content

Commit dbd1bac

Browse files
authored
Merge pull request #6459 from kenjis/fix-docs-model-validation
docs: update Model validation
2 parents a8072a5 + f58db31 commit dbd1bac

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

user_guide_src/source/models/model.rst

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -380,21 +380,32 @@ Cleans out the database table by permanently removing all rows that have 'delete
380380

381381
.. literalinclude:: model/026.php
382382

383+
In-Model Validation
384+
===================
385+
383386
Validating Data
384-
===============
387+
---------------
385388

386389
For many people, validating data in the model is the preferred way to ensure the data is kept to a single
387390
standard, without duplicating code. The Model class provides a way to automatically have all data validated
388391
prior to saving to the database with the ``insert()``, ``update()``, or ``save()`` methods.
389392

393+
.. important:: When you update data, the validation in the model class only validate provided fields.
394+
So when you set the rule ``required``, if you don't pass the required field data,
395+
the validation won't fail. This is to avoid validation errors when updating only some fields.
396+
390397
The first step is to fill out the ``$validationRules`` class property with the fields and rules that should
391398
be applied. If you have custom error message that you want to use, place them in the ``$validationMessages`` array:
392399

393400
.. literalinclude:: model/027.php
394401

395402
The other way to set the validation rules to fields by functions,
396403

397-
.. php:function:: setValidationRule($field, $fieldRules)
404+
.. php:namespace:: CodeIgniter
405+
406+
.. php:class:: Model
407+
408+
.. php:method:: setValidationRule($field, $fieldRules)
398409
399410
:param string $field:
400411
:param array $fieldRules:
@@ -405,7 +416,7 @@ The other way to set the validation rules to fields by functions,
405416

406417
.. literalinclude:: model/028.php
407418

408-
.. php:function:: setValidationRules($validationRules)
419+
.. php:method:: setValidationRules($validationRules)
409420
410421
:param array $validationRules:
411422

@@ -417,7 +428,7 @@ The other way to set the validation rules to fields by functions,
417428

418429
The other way to set the validation message to fields by functions,
419430

420-
.. php:function:: setValidationMessage($field, $fieldMessages)
431+
.. php:method:: setValidationMessage($field, $fieldMessages)
421432
422433
:param string $field:
423434
:param array $fieldMessages:
@@ -428,7 +439,7 @@ The other way to set the validation message to fields by functions,
428439

429440
.. literalinclude:: model/030.php
430441

431-
.. php:function:: setValidationMessages($fieldMessages)
442+
.. php:method:: setValidationMessages($fieldMessages)
432443
433444
:param array $fieldMessages:
434445

@@ -454,7 +465,7 @@ and simply set ``$validationRules`` to the name of the validation rule group you
454465
.. literalinclude:: model/034.php
455466

456467
Retrieving Validation Rules
457-
===========================
468+
---------------------------
458469

459470
You can retrieve a model's validation rules by accessing its ``validationRules``
460471
property:
@@ -473,7 +484,7 @@ value an array of fieldnames of interest:
473484
.. literalinclude:: model/037.php
474485

475486
Validation Placeholders
476-
=======================
487+
-----------------------
477488

478489
The model provides a simple method to replace parts of your rules based on data that's being passed into it. This
479490
sounds fairly obscure but can be especially handy with the ``is_unique`` validation rule. Placeholders are simply

0 commit comments

Comments
 (0)