Jump to content

Recommended Posts

I have a class... let's get the idea of it out there... (there is more to it, but here's the rundown)

[code=php:0]
class form {
public $action;
public $method;
public $name;
// blah blah more form related variables

public function create_form($values){
$values = func_get_args();
foreach($values as $value){
list($name, $type, $required, $value) = explode(',' $value);
// code to create form validation javascript, the form itself, etc....
}
}
}
[/code]

now the thing is... when I call the create_form() method as you can see I have to pass many values, that can end up looking pretty messy, something like this....

[code=php:0]
$form = new form();
$form->create_form("username,text,true,{$_POST['username']}", "password,password,true,false");
[/code]

you might think "that's messy... but not something to go posting to a forum about" well... take something like this... (this is ACTUALLY from my code, god I feel bad for even posting it here)...

[code=php:0]
// Left it as $GLOBALS['form'] so you can giggle about my weird way of connecting my classes
$GLOBALS['form']->create_form(
"name,text,true,{$_SESSION['name']}",
"email,text,true,{$_SESSION['email']}",
"message,textarea,true,{$_SESSION['message']}",
"false,submit,false,Login"
);
[/code]

As you can see it CAN get messy to read when it comes to things like submit buttons, textareas, and inputs all mixed together since they are quite different....

I'm wondering if anyone can think of a cleaner alternative to all this structure? I'd rather have structure suggestions, not PHP suggestions, that's why it's in the design section not the code section :D
Link to comment
https://forums.phpfreaks.com/topic/30851-cleaner-way-to-make-a-complex-function/
Share on other sites

I'll give it a try, it should be interesting because the fact of the matter is, the validation javascript has to be created before form entities themselves...

I really can't recall, is it possible to run two buffers side by side without dumping either? I COULD create the validation and the form entities at the same time, and put them into separate buffers. Then when all the entities/validations have been created I could dump the buffers in the correct order?

As far as I remember the following isn't possible... but please correct me if I'm wrong! I would love it!

[pre]
start buffer 1
    start buffer 2 to contain the validation
    start buffer 3 to contain the form items
    create a form entity
          dump the form entity's validation into buffer 2
          dump the form entity's html <input>,<textarea>, etc into buffer 3
    create another form entity
          dump that one's validation into buffer 2 as well
          dump that one's html into buffer 3 as well
    create more entities above etc until I have all the form entities I want in
    dump buffer 2 into buffer 1 to create the validation javascript
    dump buffer 3 into buffer 1 to create the form itself
dump buffer 1
[/pre]

I really don't know if buffers can be used like that, as far as I remember all they can do is stack one on top of the other like... well... the stack, and not have two run side by side. Pleaseeee correct me if I'm wrong, if I'm wrong.. or even if I'm right maybe give me a better idea :D
If you are referring to php's output buffering capabilties by use of ob_*, then no. But there are other ways to 'buffer'. Simply don't output until you're ready for it. But I think it would be better not to render anything until the object has been fully configured, for increased flexibility:
[code]
<?php
$form = new Form('MyForm', 'defaultValidation');
$textfield = new TextField('MyTextField', 'validationRule1');
$textfield->value = 'MyDefaultValue';
$form->attach($textfield);
$textfield->attach(new ComboBox('MyComboBox', 'validationRule2'));
$html = $form->render();
?>[/code]

Note that Form, TextField and ComboBox all share a common interface, empowering them to attach child elements, render html, set html entity attributes and bind to a validation rule.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.