PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Codeigniter 4 Validation for optional values

 March 09, 2022     codeigniter, codeigniter-4, php     No comments   

Issue

How to validate optional values in codeigniter 4

for example conditions

  1. If one field is filled by user which is an optional
  2. If one field is left empty by user which is an optional

how should I validate in first condition

This is my model code where I want phone field to be optional

'create' => [
            'message'               => 'required|alpha_numeric_punct|max_length[10000]',
            'name'                  => 'required|alpha_space',
            'email'              => 'required|valid_email',
            'phone'                 => 'alpha_numeric_punct',           
            'status'                => 'required|integer',
        ],

Solution

You can use permit_empty which Allows the field to receive an empty array, empty string, null or false.

in your model code

'create' => [
            'message'               => 'required|alpha_numeric_punct|max_length[10000]',
            'name'                  => 'required|alpha_space',
            'email'                 => 'required|valid_email',
            'phone'                 => 'permit_empty|alpha_numeric_punct',           
            'status'                => 'required|integer',
        ],

for more validation rules please follow documentation

Hope this answer helps you.



Answered By - asimdev
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing