Tuesday, 19 August 2014

Setting id of input wrapper div in cakephp and hiding…

It might be a simple thing for most of CakePHP pro’s but at the same time it could be a tricky thing for CakePHP newbies. I just caught one newbie CakePHP developer of my team spending time on figuring this out thus adding it here for future reference. I hope it helps someone else as well.
So this about setting a ID of the wrapper div which contains label and input field in a CakePHP form. Consider the following example:
echo $this->Form->input(‘username’);
which outputs:
<div class=”input text”>
<label for=”BookPrice”>Price</label>
<input name=”data[Book][Price]” maxlength=”150″ type=”text” id=”BookPrice”>
</div>
Imagine that in a case you wanted to hide this entire div and wanted to show some click event and for this to happen you want a ID attached to this div element. By the virtue of CakePHP it is quite simple provided you have read related documentation. To add an ID to hide it by default you obviously would want to add id=”price-wrapper” and style=”display:none” to the <div> element with classes “input text”. Just revisit the line above and write it as:
echo $this->Form->input(‘username’, array(‘div’=>array(‘id’ => ‘price-wrapper’, ‘style’=>’display:none;’)));
and it will output:
<div  class=”input text” id=”price-wrapper” style=”display:none;”>
<label for=”BookPrice”>Price</label>
<input name=”data[Book][Price]” maxlength=”150″ type=”text” id=”BookPrice”>
</div>
which you wanted. Isn’t that simple!

1 comment:

  1. Hi, perfect article.

    1. CakePHP shopping cart and CMS development, and Payment gateway integration.
    2. CakePHP customization, maintenance, integration and upgradation.
    3. CakePHP extendibles and applications for faster E-Commerce growth and profitability.

    Features of CakePHP
    CakePHP Web Development
    CakePHP Portal Development

    ReplyDelete