Skip to content

Commit 026eb6b

Browse files
authored
Merge pull request #7685 from kenjis/docs-in-model-validation
docs: improve In-model Validation
2 parents 053f85f + eda2ff4 commit 026eb6b

7 files changed

Lines changed: 51 additions & 16 deletions

File tree

user_guide_src/source/helpers/form_helper.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ The following functions are available:
536536
The returned array is the same as ``Validation::getErrors()``.
537537
See :ref:`Validation <validation-redirect-and-validation-errors>` for details.
538538

539+
.. note:: This function does not work with :ref:`in-model-validation`. If you
540+
want to get the validation errors in model validation, see
541+
:ref:`model-getting-validation-errors`.
542+
539543
Example::
540544

541545
<?php $errors = validation_errors(); ?>
@@ -555,6 +559,10 @@ The following functions are available:
555559

556560
This function uses :php:func:`validation_errors()` internally.
557561

562+
.. note:: This function does not work with :ref:`in-model-validation`. If you
563+
want to get the validation errors in model validation, see
564+
:ref:`model-getting-validation-errors`.
565+
558566
Example::
559567

560568
<?= validation_list_errors() ?>
@@ -575,6 +583,10 @@ The following functions are available:
575583

576584
This function uses :php:func:`validation_errors()` internally.
577585

586+
.. note:: This function does not work with :ref:`in-model-validation`. If you
587+
want to get the validation errors in model validation, see
588+
:ref:`model-getting-validation-errors`.
589+
578590
Example::
579591

580592
<?= validation_show_error('username') ?>

user_guide_src/source/models/model.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Using CodeIgniter's Model
44

55
.. contents::
66
:local:
7-
:depth: 2
7+
:depth: 3
88

99
Models
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+
459461
In-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+
476481
The first step is to fill out the ``$validationRules`` class property with the fields and rules that should
477482
be 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+
481491
The 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+
531544
Now, 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-
546561
Retrieving 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+
575593
In this set of rules, it states that the email address should be unique in the database, except for the row
576594
that 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

584602
So 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+
586607
This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic
587608
keys passed in don't conflict with your form data.
588609

user_guide_src/source/models/model/027.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
class UserModel extends Model
88
{
99
protected $validationRules = [
10-
'username' => 'required|alpha_numeric_space|min_length[3]',
11-
'email' => 'required|valid_email|is_unique[users.email]',
12-
'password' => 'required|min_length[8]',
13-
'pass_confirm' => 'required_with[password]|matches[password]',
10+
'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
11+
'email' => 'required|max_length[254]|valid_email|is_unique[users.email]',
12+
'password' => 'required|max_length[255]|min_length[8]',
13+
'pass_confirm' => 'required_with[password]|max_length[255]|matches[password]',
1414
];
1515
protected $validationMessages = [
1616
'email' => [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

33
$fieldName = 'username';
4-
$fieldRules = 'required|alpha_numeric_space|min_length[3]';
4+
$fieldRules = 'required|max_length[30]|alpha_numeric_space|min_length[3]';
55

66
$model->setValidationRule($fieldName, $fieldRules);

user_guide_src/source/models/model/029.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
$validationRules = [
4-
'username' => 'required|alpha_numeric_space|min_length[3]',
4+
'username' => 'required|max_length[30]|alpha_numeric_space|min_length[3]',
55
'email' => [
6-
'rules' => 'required|valid_email|is_unique[users.email]',
6+
'rules' => 'required|max_length[254]|valid_email|is_unique[users.email]',
77
'errors' => [
88
'required' => 'We really need your email.',
99
],

user_guide_src/source/models/model/038.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class MyModel extends Model
88
{
99
protected $validationRules = [
10-
'email' => 'required|valid_email|is_unique[users.email,id,{id}]',
10+
'id' => 'max_length[19]|is_natural_no_zero',
11+
'email' => 'required|max_length[254]|valid_email|is_unique[users.email,id,{id}]',
1112
];
1213
}

user_guide_src/source/models/model/040.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
class MyModel extends Model
88
{
99
protected $validationRules = [
10-
'email' => 'required|valid_email|is_unique[users.email,id,4]',
10+
'id' => 'max_length[19]|is_natural_no_zero',
11+
'email' => 'required|max_length[254]|valid_email|is_unique[users.email,id,4]',
1112
];
1213
}

0 commit comments

Comments
 (0)