@@ -380,21 +380,32 @@ Cleans out the database table by permanently removing all rows that have 'delete
380
380
381
381
.. literalinclude :: model/026.php
382
382
383
+ In-Model Validation
384
+ ===================
385
+
383
386
Validating Data
384
- ===============
387
+ ---------------
385
388
386
389
For many people, validating data in the model is the preferred way to ensure the data is kept to a single
387
390
standard, without duplicating code. The Model class provides a way to automatically have all data validated
388
391
prior to saving to the database with the ``insert() ``, ``update() ``, or ``save() `` methods.
389
392
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
+
390
397
The first step is to fill out the ``$validationRules `` class property with the fields and rules that should
391
398
be applied. If you have custom error message that you want to use, place them in the ``$validationMessages `` array:
392
399
393
400
.. literalinclude :: model/027.php
394
401
395
402
The other way to set the validation rules to fields by functions,
396
403
397
- .. php :function :: setValidationRule($field, $fieldRules)
404
+ .. php :namespace :: CodeIgniter
405
+
406
+ .. php :class :: Model
407
+
408
+ .. php :method :: setValidationRule($field, $fieldRules)
398
409
399
410
:param string $field:
400
411
:param array $fieldRules:
@@ -405,7 +416,7 @@ The other way to set the validation rules to fields by functions,
405
416
406
417
.. literalinclude :: model/028.php
407
418
408
- .. php :function :: setValidationRules($validationRules)
419
+ .. php :method :: setValidationRules($validationRules)
409
420
410
421
:param array $validationRules:
411
422
@@ -417,7 +428,7 @@ The other way to set the validation rules to fields by functions,
417
428
418
429
The other way to set the validation message to fields by functions,
419
430
420
- .. php :function :: setValidationMessage($field, $fieldMessages)
431
+ .. php :method :: setValidationMessage($field, $fieldMessages)
421
432
422
433
:param string $field:
423
434
:param array $fieldMessages:
@@ -428,7 +439,7 @@ The other way to set the validation message to fields by functions,
428
439
429
440
.. literalinclude :: model/030.php
430
441
431
- .. php :function :: setValidationMessages($fieldMessages)
442
+ .. php :method :: setValidationMessages($fieldMessages)
432
443
433
444
:param array $fieldMessages:
434
445
@@ -454,7 +465,7 @@ and simply set ``$validationRules`` to the name of the validation rule group you
454
465
.. literalinclude :: model/034.php
455
466
456
467
Retrieving Validation Rules
457
- ===========================
468
+ ---------------------------
458
469
459
470
You can retrieve a model's validation rules by accessing its ``validationRules ``
460
471
property:
@@ -473,7 +484,7 @@ value an array of fieldnames of interest:
473
484
.. literalinclude :: model/037.php
474
485
475
486
Validation Placeholders
476
- =======================
487
+ -----------------------
477
488
478
489
The model provides a simple method to replace parts of your rules based on data that's being passed into it. This
479
490
sounds fairly obscure but can be especially handy with the ``is_unique `` validation rule. Placeholders are simply
0 commit comments