0

I have a helper which generates a custom form input.

Helper (simplifed code)

public function customInput($field, array $options = array()) {

    $defaultOptions = array(
        'class' => 'custom-input',
        'label' => false
    );
    $options = array_merge($defaultOptions, $options);

    return $this->Form->input($field, $options);
}

Now how can I modify the name attribute of the input by prefixing it with another 'model'. For example, the input will by default have the following name attribute:

<input type="text" name="data[MyModel][field]" />

But I want it to be:

<input type="text" name="data[_custom][MyModel][field]" />

Mainly, what seems tricky is that I don't know how to get the model name that will be used by default. Also, I need something that works if the default model hierarchy is more complicated, like:

<input type="text" name="data[MyModel][AssociatedModel][field]" />

Would need to be modified to:

<input type="text" name="data[_custom][MyModel][AssociatedModel][field]" />

3 Answers 3

2

You want name

echo $this->Form->input('whatever', array('name' => 'data[_custom][MyModel][field]'));

There is nothing like data[_custom][MyModel][AssociatedModel][field] in cakes form helper. Your options as far as automation go is:

  • field // normal, use current model
  • Model.field // used with not default model / relations
  • Model.$i.field // User hasMany Post would be Post.$i.field
Sign up to request clarification or add additional context in comments.

Comments

0

For the input helper, CakePHP uses $this->model() to get the name of the current model.

You can see it inside lib\Cake\view\FormHelper, or directly from the online API: http://api20.cakephp.org/view_source/form-helper#line-942

$modelKey = $this->model();

Maybe that helps.

1 Comment

This is what I ended up doing, as it seems to be the only thing I can use to generate the name dynamically but even then I think it will fall down if there is anything other than a basic Model.field key. Model.Model.field or Model.0.field won't be possible I guess.
0

well you can do: $this->Form->input('_custom.MyModel.field'); to create an input in the format you require.

It becomes a case of passing the appropriate model name and associated model with it.

I don't know how you could do this automatically as obviously each relation is different/may have multiple associations.

So using your helper: echo $this->YourHelper->CustomInput('_custom.MyModel.MyAssociation.field', $options) might do the trick.

1 Comment

Yes, but that doesn't work because I don't know the model name of the form that will be using the Helper. It could be any model name. So the question really is how does the Form::input() method generate the name field itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.