• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Label in Form Validation

#1
How do I put the name of the label in validation?
Not the field name.

in codeigniter 3 I did this
PHP Code:
$this->form_validation->set_rules('first_name','First Name','required'); 

how do i do this codeigniter n 4?
I'm trying this, but I can not.
PHP Code:
$validation->setRule('first_name','First Name','required'); 

This also does not work
PHP Code:
$validation->setRules(['first_name' => 'First Name''required']); 
Reply

#2
CodeIgniter 4 Users Guide - Validation
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#3
I have not found anything in the documentation so that the name of the label can appear, not the field.

you can post an example of how I can do this?
Reply

#4
If codeigniter 4 can not do this, I'm wasting time.
pois terei muito trabalho para criar uma menssagem personalizada para cada validação de cada campo de formulário.
Does anyone have a solution?
Reply

#5
PHP Code:
public function email_agreement()
    {
        
$messages = [
            
'notice_email' => [
                
'required' => 'Email is required',
                
'valid_email' => 'Email is invalid',
            ],
        ];
        
$this->validation->setRules([
            
'notice_email' => 'required|valid_email',
        ], 
$messages);

        if (
$this->request->getPost('submitted') == null || 
                ! 
$this->validation->withRequest($this->request)->run()) {
            print 
view('header', [
                
'current' => 'register'
                
'title' => 'Email Agreement'
                
'session' => $this->session,
            ]);
            print 
view('email_agreement', [
                
'validation' => $this->validation
                
'session' => $this->session,
                
'confirm' => $this->request->getGet('confirm'),
            ]);
            print 
view('footer');
        } else {
            
$this->session->set($this->request->getPost());
            
$confirm $this->request->getPost('confirm');
            if (
$confirm == 'yes')
                
redirect('/register/confirm_input');
            else
                
redirect('/register/subscriber_and_contact_info');
        }
    } 
Simpler is always better
Reply

#6
I do not want it this way.
I do not want to use custom error message for each field.
This is very laborious if I have a large form.
I want to use codeigniter's own messages 4.
but I want the Label name to be displayed instead of the field name.
Field Name = first_name
Label Name = First Name (Display this)
Reply

#7
Currently, I don't think you can. I'd like to do it that way too. It's less code.
Simpler is always better
Reply

#8
Okay, I'm still waiting for more suggestions
Reply

#9
Currently, no, it doesn't work like the old way. We are open to PR's though, to improve it. I'm slammed with work right now and am not getting much extra time to put into it for the next few weeks, likely.

At the very least, add an issue at GitHub. Though I don't see a really elegant way to handle it with the current system. Am open to suggestions on syntax that works with the current system and doesn't require an entire rework to match the old way just because we're comfortable with it. Smile
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4
Reply

#10
I think an alternative syntax to what we have would be more user friendly

PHP Code:
$validation->setRule('first_name''required', [
  
'label' => 'First Name',
  
'errors' => [ 'required' => 'The {attribute} field is required.' ]
]);
// or single rule per call like so, bit more verbose
$validation->setRule(['first_name''required'
  
'message' => 'The first name is required.' 
]); 
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.