Welcome Guest, Not a member yet? Register   Sign In
How to customize label on this codeigniter validation
#1

(This post was last modified: 04-10-2023, 10:47 PM by phili.)

There are more than 200 similar checks in my project:
if($this->request->getMethod() == 'post' && $this->validate([
            'category' => 'required',
            'title' => 'required|max_length[100]'
        ], [
            'title' => [
                'required' => 'No {field} provided'
            ]
        ]))

I only need to change the label. I tried like this:

if($this->request->getMethod() == 'post' && $this->validate([
            'category' => 'required',
            'title' => 'required|max_length[100]'
        ], [
            'title' => [
                'label' => 'Title'
            ]
        ]))

But it doesn't work! How to do this without rewriting a lot of code?
Reply
#2

(This post was last modified: 04-12-2023, 01:39 PM by digitalarts.)

Try this:

PHP Code:
if($this->request->getMethod() == 'post' && $this->validate([
     'category' => 'required',
     'title' => [
          'label' => 'Title',
          'rules' => 'required|max_length[100]',
      ],
])) 

And, if you want to add a custom message:

PHP Code:
if($this->request->getMethod() == 'post' && $this->validate([
     'category' => 'required',
     'title' => [
          'label' => 'Title',
          'rules' => 'required|max_length[100]',
          'errors' => [
              'max_length' => 'The {field} field cannot be more than 100 characters long.',
          ],
      ],
])) 

That said, if you have more than 200 validation rules, you should probably store them in a variable in the Validation configuration file.

https://codeigniter.com/user_guide/libra...ation.html
Reply
#3

Please in the future use the Forums editor and the php tag for php code.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB