extending inputfields
Started by Sevarf2, Sep 08 2011 01:58 AM
5 replies to this topic
#3
Posted 08 September 2011 - 09:06 AM
I'm building a form builder based on the original inputfileds of PW. Now i'm at the point in which i have the same form in the admin and in the frontend, changing a field in the admin my form change in the same way, same for the post action. Now i want to add the automatic validation for every field and for making this i need to add a select near every inputfield in the admin for choose the right validator.
#4
Posted 08 September 2011 - 09:59 AM
I think I understand. I should say though that PW's fields and inputs are designed for administrative use rather than front end use. If I'm building something for the front end, I might still use PW pages to store the data, but I'm handling the input on my own and populating fields into page objects. That's what I recommend because you'll be able to do whatever you want without limitation and use PW's API in the manner it was built for. But if the Inputfields are suiting your need, then here's what you'd do to extend all of them. In the example, we add a hook to the field rendering (to add a select box with each one) and another hook to the input processing.
This example module should be pasted into a file called /site/modules/InputfieldHookTest.module
This example module should be pasted into a file called /site/modules/InputfieldHookTest.module
<?php
class InputfieldHookTest extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'Inputfield Hook Test',
'version' => 100,
'summary' => 'Just a test.',
'singular' => true,
'autoload' => true,
);
}
public function init() {
$this->addHookAfter('Inputfield::render', $this, 'render');
$this->addHookAfter('Inputfield::processInput', $this, 'processInput');
}
public function render(HookEvent $event) {
$inputfield = $event->object;
// optional, but better to limit to those you want
if(!$inputfield instanceof InputfieldText && !$inputfield instanceof InputfieldTextarea) return;
$name = $inputfield->name;
$event->return .= "<p><select name='test_$name'><option></option><option>test</option><option>test2</option></select></p>";
}
public function processInput(HookEvent $event) {
$inputfield = $event->object;
$name = $inputfield->name;
$post = $event->arguments[0];
$value = $post["test_$name"];
if($value) $this->message("You selected '$value' for '$name'");
}
}
#5
Posted 08 September 2011 - 01:38 PM
that kinda was what I was asking for here http://processwire.c...opic,465.0.html 
I already was this far to have the right hook on inputfields and all but missed the obvious, that playing with str_replace I replaced itself each recursion... was late night.
Thanks for the example that helped and made some things clearer.
I already was this far to have the right hook on inputfields and all but missed the obvious, that playing with str_replace I replaced itself each recursion... was late night.
Thanks for the example that helped and made some things clearer.
@somartist | modules created | support me, flattr my work flattr.com
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users













