@@ -4,7 +4,7 @@ Using CodeIgniter's Model
44
55.. contents ::
66 :local:
7- :depth: 2
7+ :depth: 3
88
99Models
1010******
@@ -456,6 +456,8 @@ Cleans out the database table by permanently removing all rows that have 'delete
456456
457457.. literalinclude :: model/026.php
458458
459+ .. _in-model-validation :
460+
459461In-Model Validation
460462===================
461463
@@ -473,11 +475,19 @@ prior to saving to the database with the ``insert()``, ``update()``, or ``save()
473475 If you want to check required fields, you can change the behavior by configuration.
474476 See :ref: `clean-validation-rules ` for details.
475477
478+ Setting Validation Rules
479+ ------------------------
480+
476481The first step is to fill out the ``$validationRules `` class property with the fields and rules that should
477482be applied. If you have custom error message that you want to use, place them in the ``$validationMessages `` array:
478483
479484.. literalinclude :: model/027.php
480485
486+ If you'd rather organize your rules and error messages within the Validation configuration file, you can do that
487+ and simply set ``$validationRules `` to the name of the validation rule group you created:
488+
489+ .. literalinclude :: model/034.php
490+
481491The other way to set the validation rules to fields by functions,
482492
483493.. php :namespace :: CodeIgniter
@@ -528,8 +538,18 @@ The other way to set the validation message to fields by functions,
528538
529539 .. literalinclude :: model/031.php
530540
541+ Getting Validation Result
542+ -------------------------
543+
531544Now, whenever you call the ``insert() ``, ``update() ``, or ``save() `` methods, the data will be validated. If it fails,
532- the model will return boolean **false **. You can use the ``errors() `` method to retrieve the validation errors:
545+ the model will return boolean **false **.
546+
547+ .. _model-getting-validation-errors :
548+
549+ Getting Validation Errors
550+ -------------------------
551+
552+ You can use the ``errors() `` method to retrieve the validation errors:
533553
534554.. literalinclude :: model/032.php
535555
@@ -538,11 +558,6 @@ errors at the top of the form, or to display them individually:
538558
539559.. literalinclude :: model/033.php
540560
541- If you'd rather organize your rules and error messages within the Validation configuration file, you can do that
542- and simply set ``$validationRules `` to the name of the validation rule group you created:
543-
544- .. literalinclude :: model/034.php
545-
546561Retrieving Validation Rules
547562---------------------------
548563
@@ -572,6 +587,9 @@ replaced by the **value** of the matched incoming field. An example should clari
572587
573588.. literalinclude :: model/038.php
574589
590+ .. note :: Since v4.3.5, you must set the validation rules for the placeholder
591+ field (``id ``).
592+
575593In this set of rules, it states that the email address should be unique in the database, except for the row
576594that has an id matching the placeholder's value. Assuming that the form POST data had the following:
577595
@@ -583,6 +601,9 @@ then the ``{id}`` placeholder would be replaced with the number **4**, giving th
583601
584602So it will ignore the row in the database that has ``id=4 `` when it verifies the email is unique.
585603
604+ .. note :: Since v4.3.5, if the placeholder (``id``) value does not pass the
605+ validation, the placeholder would not be replaced.
606+
586607This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic
587608keys passed in don't conflict with your form data.
588609
0 commit comments