Jump to content

Recommended Posts

Posted

I've created an Inputfield module but have some questions about how the initialization works. The field works, but I don't understand how the class is being initialized and default values supplied to ___render. If I don't set the value in my code that executes wire('modules')->get('InputfieldMine') then no default value is supplied to ___render even though it's clearly set in __construct.

Excerpted logic for my module/class (InputfieldMine):

public function __construct() {
    parent::__construct();
    $this->set('minLength', 6);
    wire('log')->save('trace', "__construct $this->minLength");
}

public function init() {
    parent::init();
    // other stuff
    $ml = $this->minLength;
    wire('log')->save('trace', "init $ml");
}

public function ___render() {
    $ml = $this->minLength;
    wire('log')->save('trace', "___render $ml");
}
I'm writing a log in __construct, init, and ___render.
 

Excerpted logic from where I'm using the InputfieldMine:

    // get a my field
    wire('log')->save('trace', 'B4 LOAD');
    $field = wire('modules')->get("InputfieldMine");
    wire('log')->save('trace', "AFTER LOAD $field->minLength");

    // ellided setting of attributes and properties

    $field->minLength = 6;
    wire('log')->save('trace', "AFTER SET $field->minLength");
    $form->add($field);

I'm writing a log before loading the module, right after loading the module, and after setting the minLength.

The log sequence I get is:

B4 LOAD

__construct

init

AFTER LOAD

AFTER SET 6

___render 6

So it's clear that the ->get method causes both the constructor and the init function to get invoked as I'd expect. What's not clear to me is why the value set in __construct isn't get-able after $this->set('minLength', 6); is executed. The 6 is present in a var_dump - it's in the data: array['minLength'].

If I remove the line:

$field->minLength = 6;

right before the "AFTER SET" log call then the value is null when ___render is called. What is the purpose of setting minLength to 6 in the constructor? And how would I cause the Inputfield to use it?

Posted

I'm not sure. With your code making it an Inputfield I get a different result:

<?php

class InputfieldMine extends Inputfield {

    public static function getModuleInfo(){
        return array(
            "title" => "Inputfield Mine",
            );
    }

    public function __construct() {
        parent::__construct();
        $this->set('minLength', 6);
        wire('log')->save('trace', "__construct $this->minLength");
    }

    public function init() {
        // other stuff
        $ml = $this->minLength;
        wire('log')->save('trace', "init $ml");
    }

    public function ___render() {
        $ml = $this->minLength;
        wire('log')->save('trace', "___render $ml");
    }

}

and

// get a my field
wire('log')->save('trace', 'B4 LOAD');
$field = wire('modules')->get("InputfieldMine");
wire('log')->save('trace', "AFTER LOAD $field->minLength");

// ellided setting of attributes and properties

$field->minLength = 6;
wire('log')->save('trace', "AFTER SET $field->minLength");
$form->add($field);
My log is:
 
B4 LOAD
__construct 6
init 6
AFTER LOAD 6
AFTER SET 6
___render 6
Posted

The only difference I see (and I should have included this) is that I am extending InputfieldText. I just tried changing to Inputfield rather than InputfieldText and it doesn't change things for me.

class InputfieldMine extends InputfieldText {

    public static function getModuleInfo() {
        return array(
            'title' => __('Inputfield of Mine', __FILE__),
            'summary' => __("Inputfield", __FILE__),
            'version' => 101,
            'permanent' => false,
            );
    }

I'm working on my own password from the existing InputfieldPassword module; I can send a the file if that would help but I've attached a compare. For me, looking it over nothing is obvious. Is it possible that the module is not installed correctly? My module name does not show up when I look at setup->fields.

Thank you for the time you've already put into this.

passwordcompare.html

Posted

When I change it to extend InputfieldText it's the same result.

B4 LOAD
__construct 
init 6
AFTER LOAD 6
AFTER SET 6
___render 6
 
Have you refreshed modules? What PW Version?
 
It doesn't show up under fields since it's a Inputfield with no Fieldtype. Fields are Fieldtypes that can have one or multiple Inputfields for their "input".
Posted

PW 2.5.3.

I tried changing the InputfieldText to Inputfield before I sent the last message and got the same result I previously did.

The diff is an HTML side-by-side file. Nothing looks strange to me - that's why I'm looking for some external factor. Am I missing something obvious?

Posted

:\

I'm sorry. Thank you. I had looked at that many times and failed to see it yet it is so obvious now. I had specifically looked at my usage vs. the line that was there and just couldn't see the difference. Repeatedly. I don't have a good explanation.

I owe you a beer. And I owe you a better question next time too.

Thank you for pointing out what should have been obvious to me.

  • Like 1
Posted

You know what? It doesn't matter. We all been there and even seasoned programmers sometimes tap into it. Our mind is ignoring things, otherwise we would go crazy ;) 

  • Like 3
Posted

Don't I know it! Usually I try to explain to someone why the bug is impossible and it becomes obvious to me. But there was no one here for me to explain how right the code was. ;)

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
×
×
  • Create New...