Jump to content

Change label of built in InputfieldPassword


gebeer
 Share

Recommended Posts

Hello,

I'm using the API to populate a form in the frontend with PW 2.5

My code for appending password field

// create password inputs
$field = $modules->get("InputfieldPassword");
$field->label = "Set new Password";
$field->attr("id+name","pass");
$field->required = 1;
$form->append($field);

Whatever string I set $field->label to, I always get "Set Password" as label.

Is it possible at all to change the label on InputfieldPassword?

Link to comment
Share on other sites

I just created a module that does that just for you :) you can modify it to get what you want.

Create the folder ChangeLabels with 2 files:

ChangeLabels.module

<?php

/**
 * ChangeLabels
 * @author  Luis Santiago "blad"
 * 
 * ProcessWire 2.x 
 * Copyright (C) 2010 by Ryan Cramer 
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 * 
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */

class ChangeLabels extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => 'ChangeLabels', 
            'version' => 1, 
            'summary' => "ChangeLabels - Tested",
            'author' => 'Blad',
            'autoload' => true
            );
    }

    public function init() {
            $this->addHookBefore('InputfieldPassword::render', $this, 'HookChangeLabel');
        }
        
    public function HookChangeLabel(HookEvent $event) {
                $this->config->scripts->add($this->config->urls->ChangeLabels . 'ChangeLabels.js');    
         }
}

ChangeLabels.js
$(document).ready(function() {

$("li#wrap_Inputfield_pass label").empty();
$("li#wrap_Inputfield_pass label").append("Your label");

  
});

It works.
  • Like 3
Link to comment
Share on other sites

... The correct solution is yours.

Well, thanks for the props but that might not be the case.  A better solution to the problem might be found in either removing the hard-coded label set from the ___render() routine or in doing what PW does for its own admin login: use an InputfieldText rather than an InputfieldPassword!

Here it is straight from ProcessLogin.module...

$this->passField = $this->modules->get('InputfieldText');
$this->passField->set('label', $this->_('Password')); // Login form: password field label
$this->passField->attr('id+name', 'login_pass'); 
$this->passField->attr('type', 'password'); 
$this->passField->attr('class', $this->className() . 'Pass'); 

First time I've noticed that the login page isn't using InputfieldPassword.

Any mileage in that approach for you Gerhard?

  • Like 3
Link to comment
Share on other sites

Thanks netcarver!

I'm creating a form where users can change their password. So I need 2 fields to be able to check for typos. That's why I initially wanted to use the built in password field because it provides the validation field out of the box.

But I'm thinking of switching to 2 custom inputs and doing the validation logic myself.

Just thought it would be easier to reuse something that's already there....

Link to comment
Share on other sites

you posted while I was still writing.

For normal input fields like InputfieldText and InputfieldEmail the label override works just fine. It seems to be only the password field. So it seems not that big of an issue to me and I can live with it. But I will add it as an issue to see what the reason behind hardcoding in the render method is.

  • Like 2
Link to comment
Share on other sites

A better solution to the problem might be found in either removing the hard-coded label set from the ___render() routine or in doing what PW does for its own admin login: use an InputfieldText rather than an InputfieldPassword!

 I still do not know too much about Processwire  but is logical that a password have a InputfieldPassword  :huh:

Link to comment
Share on other sites

@blad

Yeah, it's logical but there are actually two different use-cases and I was getting them mixed up.

I think Gerhard touched on the difference between the two above; the admin login only needs to collect the username and one copy of the password, so it uses InputfieldText with the type set to 'password' to do it. In admin forms (once logged in) if you collect a password to set it or change it, then you usually get two copies (the password and a copy of the password) - just to make sure you typed it the same both times. I think (but didn't realise till now) that InputfieldPassword is for the second scenario; setting or changing a password. Gerhard's trying to reuse this Inputfield in his form but the hard-coded set of the label in InputfieldPassword's ___render() routine is getting in the way.

Anyway, that's my current understanding of the situation.

  • Like 1
Link to comment
Share on other sites

@Gerhard,

You are right - I used the words "hard coded" incorrectly. I meant that it's set in the render routine. My point still stands though; why have it set in the render routine at all? Couldn't it have a default value (translatable, yes) but otherwise it should use the label that the owning code assigns to it - so that it can be used in many different forms, potentially with a different label in each.

You can probably solve your issue by translating the string - but that doesn't mean the root problem is fixed (I use the term "problem" here lightly, Ryan might have a very good reason for setting the label in the render method!)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...