Non-existing fields no longer supported in FormHelper

Published on November 07, 2007 and tagged with cakephp  helper

Yesterday, I noticed that a Javascript function stopped to work in my application. The reason was that the id of an input field used by this Javascript has changed from “ModelnameFieldname” to “Modelname”. As I didn’t change anything in my form it had to be an issue with the form helper. The “funny” thing was that the issue only occurred if the model existed (and the field didn’t exist in the model, as I learned later).

Obviously this issue was caused by a recent change in the FormHelper, and according to a comment by Nate it was intentionally: “In order to enable model association mapping for FormHelper, non-existent fields are no longer supported. In order for a field to be recognized, it must exist in the model’s schema, or have a validation rule defined in Model::$validate.”

Hm, I have to say I don’t like it as it introduces dependencies which are not obvious. So the following snippet may work in one case, but not in another case:

echo $form->create('Model');
echo $form->input('Model.field');
echo $form->end();

If the model and the field exist, then the correct code is generated. The same if the model doesn’t exist. But if the model exists, and the field not, it generates wrong code (i.e. all input fields for non-existing model fields will have the same id). So, be careful when using virtual fields.

12 comments baked

  • daniel November 07, 2007 at 15:26

    Hi,

    I think it’s not a good solution… there might be an internal check if the field exists and THEN adding some fancy relationships etc.
    for other fields it would just generate a virtual field like now.

    regards
    daniel

  • Stefano November 07, 2007 at 16:13

    So what I have to do if I need to generate some “virtual” fields in the form?

    Stef.

  • Brandon P November 07, 2007 at 18:09

    Can you do?:

    echo $form->input(‘virtual_field’, array(‘id’ => ‘ModelVirtualField’));

  • cakebaker November 07, 2007 at 20:45

    @all: Thanks for your comments!

    @Stefano, Brandon: Yes, it works if you specify the id. But only if you use:

    echo $form->input(’Model.virtual_field’, array(’id’ => ‘ModelVirtualField’));
    

    If you use

    echo $form->input('virtual_field’);
    

    it creates the correct id, but then the name of the field is “data[Model][virtual_field][virtual_field]” instead of “data[Model][virtual_field]“…

  • Tane Piper November 08, 2007 at 01:01

    I’ve found that actually setting the form element type (like $form->textarea, $form->checkbox and $form->hidden) gets round these issues.

    Like so:

    input(‘User.email’, array(‘label’=>’Please enter your email’));
    echo $form->password(‘User.passwd’, array(‘label’=>’Please enter your password’));
    echo $form->text(‘passwd_conf’, array(‘type’=>’password’, ‘label’=>’Please confirm your password’, ‘type’=>’password’));
    echo $form->input(‘User.display_name’, array(‘label’=>’Please enter your desired display name’));
    echo $form->hidden(‘User.active’);
    ?>
    It is annoying, but it’s certainly workable.

  • Grant Cox November 08, 2007 at 01:12

    I hope that what we are seeing is only partial functionality – and before the next 1.2.x release a more complete solution is developed.

    I don’t think that accessing the form functions directly is always appropriate – then you have to manually add the div wrapping around each one?

  • nate November 08, 2007 at 17:16

    This functionality is still under development/review.

  • tclineks November 08, 2007 at 17:29

    I was battling with this yesterday, hope it ends up clean again.

  • phpsc November 09, 2007 at 10:23

    Not sure if many would be in favor of this new functionality. I wish its reverted back to the way it was.

  • cakebaker November 09, 2007 at 18:58

    @all: Thanks for your comments!

    @Tane: Yes, that’s a possible solution, but there are also strange results depending on the settings (“Model.field” vs. “field” as first parameter).

    And as Grant mentioned, it is not always appropriate.

  • Tane Piper November 10, 2007 at 00:14

    @cakebaker: Yea, it’s simply a case of trial and error at the moment, and I know its just going to break, but what can you do?

  • cakebaker November 11, 2007 at 18:17

    @Tane: I think the only thing we as framework users can do is waiting for the devs to fix it, as they are the only guys knowing in which direction this functionality will move (at least I hope they know it *g*)

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License