Jump to content

mechanism to initialize Inputfield configuration at construction or init time?


bmacnaughton
 Share

Recommended Posts

I would like to provide a way to for the caller to configure an Inputfield when getting it, i.e., when either __construct or init is called, as opposed to having to set each field independently after the field has been instantiated. How would I do that?

What I have now:

    // get a password field
    $field = wire('modules')->get("InputfieldPasswordGeneric");
    $field->attr('id+name', 'password');
    $field->attr('size', 40); 
    $field->required = true; 
    $field->label = 'Password:';

What I'd like to do (conceptually):

$defaults = array(
    'required' => true,
    'label' => 'Password:',
    'allow_whitespace' => false,
    'numDigits' => 0,
);

$field = wire('modules')->get("InputfieldPasswordGeneric", $defaults);
$field->attr('id+name', 'password');
$field->attr('size', 40);

I could just create a property, like defaults, and decode that. But the code won't see that until rendering time so will have to do additional processing each render. It seems like it belongs at initialization or construction time.

Link to comment
Share on other sites

I'm not sure I understand what the problem with first method is. Are you speaking about your Inputfield's default (in module) ?

Look at InputfieldText how it set defaults

public function __construct() {
    parent::__construct();
    $this->setAttribute('type', 'text'); 
    $this->setAttribute('size', 0); 
    $this->setAttribute('maxlength', self::defaultMaxlength); 
    $this->setAttribute('placeholder', '');
    $this->setAttribute('pattern', '');
    $this->set('initValue', ''); // optional initial value
    $this->set('stripTags', false); // strip tags from input?
}
Link to comment
Share on other sites

I didn't ask this very well.

I'm trying to find out if there is a way to specify the values at the time the field is being created, not within the field code itself.

$field = wire('modules')->get("InputfieldPasswordGeneric");

I'm looking for the rough equivalent of:

$field = new InputfieldPasswordGeneric($config);

where $config is an associative array that can override the defaults for selected properties so the field is configurable.

Does that make sense?

Link to comment
Share on other sites

In this case it would be good, so it can be directly saved to the variable, but it's not a necessity and it's your module, you decide what you do there. ProcessWire does also not return "$this" most of the time you chain stuff.

// PageArray -> Page -> Pageimages -> Pageimage -> String
$pages->get("id=1466")->images->eq(0)->url;
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...