New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Enhancment] Allow model to change the name used on forms. #470
Comments
…ch implements Enh yiisoft#470.
This is a good move. But I have some concerns.
|
Like @phpnode and @cebe commented on the pull request... this is more a "prefix" then a form name... any better name for this method? and there are CForm::loadData() and CACtiveForm::validate that use get_class() currently, so this should be adjusted too. Because of the problem that @qiangxue explained - "same model for two forms on same page" - maybe a set/get method would be better than overriding so that we can to something like this in the controller $model1 = new Model; $model1->formName = 'FormPrefixForFirstForm"; |
From security standpoint this change will not add anything. Security through obscurity isn't useful. |
Also I don't like the fact that form-specific method is added to the model. |
@samdark it's not about security but a way to differentiate the attribute names if the same model is used more than once or to just use something else as a prefix for the attribute name then the class name. |
This could be seen as something else then a formName... this can be a model ID or NAME or HASH it can be seen as a way to differentiate two objects created by the same class... I see the usability of using it in the active forms... but maybe can be used elsewhere too |
it would also be nice to be able to set this value to false, so that prefixing is turned off completely. This is useful for e.g. nicer URLs when submitting search forms. I agree a getter / setter is a better way to go |
Maybe the better way at least for the forms would be add a form name The above method would solve a lot of the issues raised. It being a prefix Thinking about it now the one id i need to check so i can load the Still it would be nice to be able to have the option to name the form data And yes I realize the routes also can be used to directly correlate to So to summarize a better method. Ron On Thu, Mar 8, 2012 at 8:39 AM, Charles Pick <
|
@ronaldfenner your suggestion means this will only work when using CForm, I think your original solution is better, it just needs to be a getter / setter than you can set per model instance, and it needs a better name. @samdark I think in practice it's fine to put this in the model, it's something that relates to individual model instances, not form instances. I think the alternatives would be restrictive. |
@phpnode yes, you're right. But I still think we should not name it like |
Changes made for calls to get_class where necessary.
I committed updated changes. After looking at what called resolve name, the I was unsure if the yiilite file needed to be changed. I believe it's auto Ron On Thu, Mar 8, 2012 at 11:40 AM, Alexander Makarov <
|
Yes, yiilite is generated automatically. No need to change it. |
…m name. Also made changes for using null instead of false.
As this is not the model name but a prefix... how about naming it modelPrefix? How about adding one more helper method populateAttributes that would be a helper for easier code writing and reading
So in the controller we would just have |
@mdomba I think that should be discussed in a separate issue. |
Yeah I agree it's not exactly the model's name more of it's form name but i I do like you suggestion it makes the code a little more human readable and On Thu, Mar 8, 2012 at 3:44 PM, Maurizio Domba <
|
I'm just thinking to not confuse users... as class_name would give one value but modelName would give another one if set manually... that's why I would rather not call it name as we are not changing the model/class name (one model is one class) If prefix is not good maybe call it as simple as tag?
About the additional method, I don't see why not discuss it here as it's related to this addition, but I agree we can open a new discussion for that after we solve the initial implementation for this. |
@mdomba tag is really confusing imho, and will likely break quite a lot of people's code, i personally have models and behaviors with getTag() methods that refer to completely different functionality. Tag is also used a lot in other parts of the framework to refer to html tags, so it becomes even more confusing, especially as we'd call this in CHtml itself. |
I originally thought formName perhaps modelFormName instead since tag might On Thu, Mar 8, 2012 at 4:07 PM, Maurizio Domba <
|
right, tag is not a good choice as it means something else... I personally dislike to add the word "model" to this why write "model" two time like
why not just
formName is not good too we already wrote that above... so we just need to find a good name for this... |
I think either |
Note that one of the main reasons for this enhancement is because we want to differentiate two instances of the same model class when they are used to build two forms. |
Just one more suggestion from my part, if not good then let's go with @qiangxue idea of modelName or modelID My idea is to call it "identifier"
|
Just found another place that may need to be changed:
This probably should be changed to
|
…ed to use getter explicitly
How about |
"uniqueId" is very appealing... it's just what this is meant for... I like it the most of all other suggestions |
After looking it over it doesn't need to be changed as the model class On Thu, Mar 8, 2012 at 4:22 PM, Qiang Xue <
|
@mdomba we need an other issue to not mix discussion and not loose overview, I have some arguments to say about your new method but that would mess up this discussion, please open a new issue for that.
|
not sure what you mean regarding the primary key... the controller unique id returns the controller ID prefixed with the module ID - http://www.yiiframework.com/doc/api/1.1/CController#getUniqueId-detail |
I agree with what cebe said. How about reverting back to |
we made a trip around the world to get to the starting point :D At this point any name is good for me, just to implement this and document it good. |
As for me this implementation breaks the MVC.
So “CActiveForm”, “CForm” and so on should pass their internal setting to this method (and methods, which use this method). So widget creation will look like:
Such solution will solve the issue, while keeping the MVC. No offence, but if we will evolve current suggested solution, the next step will be removing “CActiveForm” and allowing “CModel” to generate HTML for its attribute inputs on its own:
|
@klimov-paul makes sense. |
@klimov-paul note that this is not about HTML code... it's about a unique model representation (formname, modelname, modelId, whatever we name it)... This is not used only in the view for rendering the form, but even in the controller to validate the models... in your example if you set the modelHtmlName when calling the CActiveForm the controller would not know what is the model name to make for example the massive assignements... |
I am disagreed. In my example you can create a field inside the controller, which should specify the name, which is used to client-server data transfer:
Than you can use this property inside the view file and pass it to the widget:
View and controller should work around the model, not through it! The problem is the data transfer form should be an artifact at the controller layer, should have representation at the view layer and fill up the model.
|
Well, if we don't allow it in the model, then there should at least be something instead of no solution at all. I don't really agree to @klimov-paul: MVC is nice as a general rule of thumb but should not be taken to such an extreme that it's like the holy grail no one can ever touch: If there's a simple solution without any obvious harm and it breaks MVC, i'd go for it! I would take this issue here even further: In some scenarios i want my input fields to be named I know, that this is not pure MVC - but it's a pragmatic solution. And similar to what we already do with form labels. The only other solution i could think of, is to use some sort of helper class which i can feed into all |
@mikehaertl, you may wonder, but this entire issue has risen, because Yii has poor MVC. At its born Yii framework has no care about the input names and this was harcoded in its architecture inside "CHtml". At the moment you can alwasy walk around your issue extending the "CActiveRecord" class, overridding its input creating methods using lower level of "CHtml" functions. |
@klimov-paul I agree that a View layer would make a lot of sense. As far as i know Yii 2.0 will implement such a view class. I also agree that the root of these problems lies in CHtml. In my opinion this class is way too complex - it should focus on HTML rendering only and not be interwoven with Yii's form handling so much (read: should not have all these active*() methods). All form related mechanics could be moved to a HtmlForm widget or something. I once described it in a forum post here |
Currently when using the Active elements for a form generation they use the class name of the model. It would be nice if we could specify a name to be used rather than the class name. Reason for this is where you might have several models in different modules that come in through a generic ajax action.
The action takes care of identifying the module and then calling the modules handler for the action. By being able to override the class name used on the form the generic action can perform various checks for the form data being there and not have to bother continuing to locate the module especially if the modules name is included as a hidden field. In my case i'm storing an id for a record in the database that actually contains the module the form belongs to.
Implementation would be adding a new function to CModel that by default returns the class name of the instance. Change CHtml's resolveName function to call the new function to get the name to use on the form. Subclasses of CModel would then be able to override the CModel's function to change the class name for the form.
From a security standpoint this allows us to also hide the exact name we use in the application for our models. It would also allow us if we have a really long model name to use a shorter name on the form.
The text was updated successfully, but these errors were encountered: