CodeIgniter Forums
Label in Form Validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Label in Form Validation (/showthread.php?tid=69351)



Label in Form Validation - Flavio Cesarino - 11-08-2017

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']); 



RE: Label in Form Validation - InsiteFX - 11-09-2017

CodeIgniter 4 Users Guide - Validation


RE: Label in Form Validation - Flavio Cesarino - 11-09-2017

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?


RE: Label in Form Validation - Flavio Cesarino - 11-09-2017

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?


RE: Label in Form Validation - donpwinston - 11-09-2017

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');
        }
    } 



RE: Label in Form Validation - Flavio Cesarino - 11-09-2017

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)


RE: Label in Form Validation - donpwinston - 11-09-2017

Currently, I don't think you can. I'd like to do it that way too. It's less code.


RE: Label in Form Validation - Flavio Cesarino - 11-09-2017

Okay, I'm still waiting for more suggestions


RE: Label in Form Validation - kilishan - 11-09-2017

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


RE: Label in Form Validation - averroez - 11-15-2017

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.' 
]);