It would be great to have a better way to handle multiple models per form. For example, I have the following two models:
class Blog extends \Phalcon\Mvc\Model {
public $blog_name;
}
class Tags extends \Phalcon\Mvc\Model {
public tag_name;
}
Now in our form we want to include both of the models and validate them.
class BlogForm extends \Phalcon\Forms\Form {
...
}
class TagsForm extends \Phalcon\Forms\Form {
...
}
Honestly, I don't know the best approach to this, whether it be embedding forms like Symfony2 or using a Yii like approach. I am just looking at a better "Phalcon" way to handle this. Currently, I use multiple models and multiple forms to render one form. So I can check to see if each model is valid and to save it in the correct order, because some models are dependent on other models. I really like the Yii way, but I have grown adjusted to it. They handle multiple models and forms by loading the elements name on the form as Blog[blog_name] or Tags[tag_name]. When rendering the element it takes in the model name as an argument along with its field name. Just a though. Thanks!
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Can you write code using PHP as the desired effect.
Say you have one form. For this example: new Post ... It would be nice to have an entity for a form except an array of objects. So it would take in Blog and Tags as an entity. Thus whenever you create a new field element on the form it would then take as an optional parameter for the model. Here is some example code:
namespace Mac\Backend\Forms;
use Phalcon\Forms\Form,
Phalcon\Forms\Element\Text;
use Mac\Models\Tags,
Mac\Models\Blog;
/**
* Description of PostsForm
*
* @author aaron
*/
class PostsForm extends Form
{
public function initialize()
{
$blog = new Blog();
$tags = new Tags();
$this->setEntity(array($blog, $tags));
$this->add(new Text($blog,'title'));
$this->add(new Text($tags,'info'));
}
}
On initialization the form would then generate the name to be Blog['title'] and Tags['info']. That way phalcon knows exactly what model to add the data too.
Have the same problem. It would be great have possibility to specify multiple entities:
new Form(array($blogModel, $tagsModel))
$form->setEntity(array($blogModel, $tagsModel))
$form->isValid($_POST, array($blogModel, $tagsModel))
:+1:
Agree, this would be a great feature.
+1 on this. Phalcon's ORM is incomplete without this feature
+1 on this too, looking forward to read a nice solution!
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues
@sergeyklay This is still relevant.
Most helpful comment
Have the same problem. It would be great have possibility to specify multiple entities:
new Form(array($blogModel, $tagsModel))
$form->setEntity(array($blogModel, $tagsModel))
$form->isValid($_POST, array($blogModel, $tagsModel))