adamwathan / form

Super basic form HTML builder, only really exists so I can pull it in for some other more useful projects.
MIT License
232 stars 117 forks source link

Html input groups #33

Open daverdalas opened 9 years ago

daverdalas commented 9 years ago

Hey, I have not found a way to bind html input groups. For example:

$builder->text('group[email]')

I did a little work around but it is not good piece of code :( but you can take a look :)

protected function nameToArray($str)
    {
        if (strpos($str,'[') === false) {
            return $str;
        }
        $re = "/(.*?)\\[(.*?)\\]/"; 
        preg_match_all($re, $str, $matches);
        array_shift($matches);
        $matches = array_filter(call_user_func_array('array_merge', $matches));
        $arr_string = "";
        foreach ($matches as $match)
        {
            $arr_string .= "['{$match}']";
        }
        return $arr_string;
    }
protected function hasModelValue($name)
    {
        if (is_array($this->model))
        {
            $name = $this->nameToArray($name);
            $arr = $this->model;
            eval('$isset = isset($arr'.$name.');');         
            return $isset;
        }
        if (! isset($this->model)) {
            return false;
        }
        return isset($this->model->{$name});
    }
protected function getModelValue($name)
    {
        if (is_array($this->model))
        {
            $name = $this->nameToArray($name);
            $arr = $this->model;
            eval('$val = $arr'.$name.';');  
            return $val;
        }
        return $this->model->{$name};
    }
adamwathan commented 9 years ago

Ugh this is a shitty tricky problem. I think your idea of converting everything to a string representation is the way to go. I'm gonna mark this as an enhancement for now, hopefully get a chance to look into it soon.