patternphpMinor
Form builder improvment
Viewed 0 times
formimprovmentbuilder
Problem
What can I improve in this form builder? I'm looking for code review, optimization, and best practices.
```
form;
}
public function add_node($type, array $args = []){
$this->form .= 'setup_args($args).'>'.PHP_EOL;
return $this;
}
public function end_node($type){
$this->form .= ''.PHP_EOL;
return $this;
}
public function add_elem($text){
$this->form .= $text.PHP_EOL;
return $this;
}
public function img($data, array $args = []){
$this->form .= 'setup_args($args).'/>'.PHP_EOL;
return $this;
}
public function start_form(array $args = []){
$this->form .= 'setup_args($args).' >'.PHP_EOL;
return $this;
}
public function end_form(){
$this->form .= ''.PHP_EOL;
return $this;
}
public function input(array $args = []){
$this->form .= 'setup_args($args).$this->value($args).' />'.PHP_EOL;
return $this;
}
public function checkbox(array $args = []){
$result = (count($args)) ? array_merge(['type' => 'checkbox'], $args) : ['type' => 'checkbox'];
$this->input($result);
return $this;
}
public function radio(array $args){
$result = (count($args)) ? array_merge(['type' => 'radio'], $args) : ['type' => 'radio'];
$this->input($result);
return $this;
}
public function textarea(array $args = []){
$this->form .= 'setup_args($args).' >'.$this->value($args, false).''.PHP_EOL;
return $this;
}
public function select(array $value, array $args = []){
$this->form .= 'setup_args($args).'>'.PHP_EOL;
foreach($value as $key => $values){
if(is_array($values)){
$this->form .= "\t".PHP_EOL;
foreach($values as $k => $v)
$this->form .= $this->populate_select($k, $v);
$this->form .= "\t".PHP_EOL;
}else{
$this
```
form;
}
public function add_node($type, array $args = []){
$this->form .= 'setup_args($args).'>'.PHP_EOL;
return $this;
}
public function end_node($type){
$this->form .= ''.PHP_EOL;
return $this;
}
public function add_elem($text){
$this->form .= $text.PHP_EOL;
return $this;
}
public function img($data, array $args = []){
$this->form .= 'setup_args($args).'/>'.PHP_EOL;
return $this;
}
public function start_form(array $args = []){
$this->form .= 'setup_args($args).' >'.PHP_EOL;
return $this;
}
public function end_form(){
$this->form .= ''.PHP_EOL;
return $this;
}
public function input(array $args = []){
$this->form .= 'setup_args($args).$this->value($args).' />'.PHP_EOL;
return $this;
}
public function checkbox(array $args = []){
$result = (count($args)) ? array_merge(['type' => 'checkbox'], $args) : ['type' => 'checkbox'];
$this->input($result);
return $this;
}
public function radio(array $args){
$result = (count($args)) ? array_merge(['type' => 'radio'], $args) : ['type' => 'radio'];
$this->input($result);
return $this;
}
public function textarea(array $args = []){
$this->form .= 'setup_args($args).' >'.$this->value($args, false).''.PHP_EOL;
return $this;
}
public function select(array $value, array $args = []){
$this->form .= 'setup_args($args).'>'.PHP_EOL;
foreach($value as $key => $values){
if(is_array($values)){
$this->form .= "\t".PHP_EOL;
foreach($values as $k => $v)
$this->form .= $this->populate_select($k, $v);
$this->form .= "\t".PHP_EOL;
}else{
$this
Solution
Let's start with the dry review, and then jump to the juicy stuff.
Now for the more juicy stuff. Is your library useful? Why would I, a user, want to use your library instead of just writing HTML? Or generating HTML for that matter? The usage code seems a bit over-engineered, too large and "clumsy" to actually be useful.
Truth be told, it's a nice project to improve your knowledge with XSS and the structure of HTML and perhaps even OOP, but it's not actually useful in the real world, because generating HTML is simply easier.
- Naming convention, it's common in PHP to name Class names in uppercase,
class FormBuilder.
- Empty constructor, if you don't need a constructor, why make one?
- XSS vulnerabilities all over the place - You aren't escaping text or attributes which are directly injected into HTML! That's a big no-no. See this XSS cheat sheet.
- Non-standard coding style. I'm talking about spacing and indentation mostly, but that's more up to your personal preference.
Now for the more juicy stuff. Is your library useful? Why would I, a user, want to use your library instead of just writing HTML? Or generating HTML for that matter? The usage code seems a bit over-engineered, too large and "clumsy" to actually be useful.
Truth be told, it's a nice project to improve your knowledge with XSS and the structure of HTML and perhaps even OOP, but it's not actually useful in the real world, because generating HTML is simply easier.
Context
StackExchange Code Review Q#55515, answer score: 3
Revisions (0)
No revisions yet.