Create icon inside of input with bootstrap and cakePHP - php

I have this cakePHP code:
<?php
echo $this->Form->input('about_me',
array('label' => __l('About Me'),
'class' => 'form-control',
'before' => '<div class="form-group form-group-icon-left"><i class="fa fa-user input-icon input-icon-show"></i>',
'after' => '</div>',
array('escape' => false)
)
);
?>
I would like the output to be (indented for readability):
<div class="col-md-6">
<div class="form-group form-group-icon-right"><i class="fa fa-map-marker input-icon"></i>
<label>About me</label>
<input class="form-control" placeholder="Write something" type="text" />
</div>
Anyone can help?

My solution was like this:
<div class="form-group form-group-icon-right">
<i class="fa fa-map-marker input-icon"></i>
<?= $this->Form->input('about_me', [
'div' => false,
'label' => 'About me',
'placeholder' => 'Write something',
];?>
</div>
I usually set 'label' => false, used the CakePHP function only for the input field and customized the structure in pure HTML.

Skatch's answer is good, but you would need to modify it further to make sure it takes into account validation. If you don't use the form helper for the wrapping div you don't get the error and required classes generated for you where appropriate.
It looks like you're almost there with your example code. However, rather than set the <div> in the after and before properties use the div property to define the <div> class:-
<div class="col-md-6">
<?php
echo $this->Form->input('about_me',
array('label' => __l('About Me'),
'class' => 'form-control',
'before' => '<i class="fa fa-user input-icon input-icon-show"></i> ',
'div' => 'form-group form-group-icon-left',
'placeholder' => 'Write something'
)
);
?>
</div>
You can also define the placeholder as a property of the input as shown. You shouldn't need to use 'escape' => false.

Related

Couldn't add required input in the form [duplicate]

This is my code :
<div class="form-group">
<div class="col-md-4">
<label class="control-label">Mobile Number:</label>
</div>
<div class="col-md-8">
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15]) !!}
</div>
</div>
Here i want to give this mobile number field as required, how can i add it in my code?
And i am having 3 steps in the creation of form and i am having mobile number in step1, when i am clicking the next button without adding mobile number, it should display an alert message..Now it is displaying alert message of mobile field required, only after clicking submit button in last step (i.e) in step 3..
Add 'required' => true to the array:
{!! Form::input('number', 'mobile', null, ['required' => true, 'type' => 'number'....
You need to check it with your next_button using jquery
<div class="form-group">
<div class="col-md-4">
<label class="control-label">Mobile Number:</label>
</div>
<div class="col-md-8">
{!! Form::input('number', 'mobile', null, ['type' => 'number', 'min' => 0, 'id' => 'mobile', 'class' => 'input-lg form-control TabOnEnter', 'placeholder' => 'Eg: 9876543210', 'tabindex' => 15]) !!}
<button style="margin-right:20px;" class="next_button btn btn-info active nextBtn btn-md pull-right" type="next" >Next</button>
</div>
</div>
$(document).ready(function(){
$('.next_button').click(function(event){
if ($('#mobile').val() == ""){
event.preventDefault();
alert('Enter the number');
}
});
});
Added next_button class in your existing code

CakePHP 3 input type file design

I want to set span before input type file. Here is my existing code.
<?= $this->Form->input('logo', ['onchange'=>'onFileImage(this);',
'label' => false,
'type' => 'file',
'class'=>'']);?>
<lable class="inva_img"></lable>
I want the output to resemble the code below.
<span class="btn btn-default btn-file">
<span>Choose file</span>
<input type="file" name="logo" id="logo" onchange="onFileImage(this);" />
</span>
You can do it by below code
<?php
echo $this->Form->input('logo', [
'templates' => [
'inputContainer' => '<span class="input file required btn btn-default input {{type}}{{required}}"><span>Choose file</span>{{content}}</span>',
],
'onchange'=>'onFileImage(this);',
'class' => 'form-control',
'type' => 'file',
'label' => false
]);
?>
For details please see ndm ans.
<span class="btn btn-default btn-file">
<span>Choose file</span>
<input type="file" name="logo" id="logo" onchange="onFileImage(this);" />
</span>
Just replace <input> tag field with cakephp standard keeping others code unchanged. Here is the conversion:
<span class="btn btn-default btn-file">
<span>Choose file</span>
<?php
echo $this->Form->input(
'logo', array(
'id' => 'logo',
'type' => 'file',
'onchange' => 'onFileImage(this);'
)
);
?>
</span>
try the before/after options
echo $this->Form->input('field_name', array(
'before' => '-before-',
'after' => '-after-',
'between' => '-between-'
));
read more of this in cakephp book.
try this for cakephp2:
<?php
$this->Form->input('logo', ['onchange'=>'onFileImage(this);',
'label' => false,
'before' => '<span class="btn btn-default btn-file"><span>Choose file</span>',
'after' => '</span>',
'type' => 'file',
'class'=>'']);
I hope that there is similar concept for cakephp3
HTH.

How can I use cjuiautocomplete on index.php's form?

I have my index.php where i am implementing searching functionality, the functionality is working, like user input some business name, and input some city name, after submit the form the business is retrieve from the database. Now my next task is to implement submission of form by using cjuiautocomplete. Like when user start typing the name of business, the businesses should come down in the drop down. The main hurdle in my way is that I am in index.php. I was following this http://www.yiiframework.com/wiki/162/a-simple-action-for-cjuiautocomplete/ but this is for view file of a controller. How can i implement this in my index.php. Given below is my form in index.php.
<form action="business/searchingtesting" method="GET">
<div class="form-group form-group-lg">
<h2 class="title">Find the best places to eat, drink, shop, or visit in Islamabad. </h2>
<div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
<input type="text" class="form-control" name="business" id="lg" placeholder="I'm looking for....">
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="text" class="form-control" id="sm" name="city" placeholder="Islamabad">
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="submit" class="btn btn-primary btn-lg" value="submit">
</div>
</div>
</form>
If I follow the above link and use the below code in my form I get this error "undefined variable model".
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'attribute' => 'my_name',
'model' => $model,
'sourceUrl' => array('my/aclist'),
'name' => 'business_name',
'options' => array(
'minLength' => '3',
),
'htmlOptions' => array(
'size' => 45,
'maxlength' => 45,
),
)); ?>
First of all read documentation. You may use CJuiAutoComplete like with model as without it. To use with model you need to specify two params: model and attribute. If you use it without model then only name. As I can see you doesn't use model in your form, so this example for you:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'my_name',
'sourceUrl' => array('/my/aclist'), // you need first slash if you want properly working URL from web root
));
<form action="business/searchname" method="GET">
<div class="form-group form-group-lg">
<h2 class="title">
Find the best places to eat, drink, shop, or visit in Islamabad.
</h2>
<div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
<?php
$model = Business::model()->findAll();
$modelcity = Address::model()->findAll(array(
'select' => 't.city',
'group' => 't.city', //selecting distinct values as many businesses hass same cities, so the drop down was filled with only one city
'distinct' => true,
));
foreach ($model as $mo) {
$store[] = $mo->business_name;
}
foreach ($modelcity as $c) {
$city[] = $c->city;
}
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'business',
'source' => array_values($store),
// additional javascript options for the autocomplete plugin
'options' => array(
'minLength' => '2',
),
'htmlOptions' => array(
'style' => 'height:45px;width:415px;',
'placeholder' => ' I am Looking for................ ',
),
));?>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'city',
'source' => array_values($city),
// additional javascript options for the autocomplete plugin
'options' => array(
'minLength' => '2',
),
'htmlOptions' => array(
'style' => 'height:45px; width:250px;',
'placeholder'=>' City................ ',
),
));
?>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="submit" class="btn btn-primary btn-lg" value="submit"/>
</div>
</div>
</form>

Impresspages Widget disappears after drag and drop

I've seen a strange behavior for a custom plugin. It's skin generates some html and it works fine. As soon as i drop the widget onto the page it is only visible after a page refresh. Same is true when i try to drag the widget to another position.
Has anybody experienced the same issue? I am not sure whether its a bug or i might miss something in my code.
[edit] There is an issue with ipContent()->getCurrentPage()->getId() which is NULL after performing any drag and drop action [edit]
This is my skin file:
<div>
<div class="form-group">
<div class="checkbox">
<label>
<input id="auth-email-check-nutzung" type="checkbox"><?php echo ipSlot('text', array(
'id' => 'nutzungsbedingungen_' . ipContent()->getCurrentPage()->getId(),
'default' => 'Ich stimme den Nutzungebedingungen zu.',
'attributes' => array(
'id' => 'auth-email-check-nutzung-text'
)
)); ?>
</label>
</div>
<div class="checkbox">
<label>
<input id="auth-email-check-daten" type="checkbox"><?php echo ipSlot('text', array(
'id' => 'datenschutz' . ipContent()->getCurrentPage()->getId(),
'default' => 'Ich stimme der Datenschutzerklärung zu.',
'attributes' => array(
'id' => 'auth-email-check-daten-text'
)
)); ?>
</label>
</div>
</div>
<form class="form" role="form">
<div class="form-group">
<label for="inputEmail1" class="control-label">Email</label>
<input type="email" class="form-control" id="inputEmail1" placeholder="Email">
</div>
<div class="form-group">
<?php if ((ipIsManagementState())) :?>
<?php
// Use a simulated Button to edit text and prevent any default button event
// for user friendly inline editing
echo ipSlot('text', array(
'id' => 'button' . ipContent()->getCurrentPage()->getId(),
'default' => 'senden',
'tag' => "div",
'class' => 'btn btn-danger auth-button btn-block',
'attributes' => array(
'data-target' => 'bs-button',
'id' => 'auth-email-button-div'
)
)); ?>
<?php endif; ?>
<?php if (!(ipIsManagementState())) :?>
<?php
// Display real Button only on the live page with the contents of ipSlot :
// 'id' => 'button' . ipContent()->getCurrentPage()->getId()
echo ipSlot('text',array(
'id' => 'button' . ipContent()->getCurrentPage()->getId(),
'tag' => "button",
'class' => 'btn btn-danger btn-block',
'attributes' => array(
'data-target' => 'bs-button',
'id' => 'auth-email-button',
'disabled' => 'disabled'
)
)); ?>
<?php endif; ?>
</div>
</form>
</div>
Thanks in advance.
When you reload the page, "currentPage" really exists. That's why the code works. But when you drag a new widget or move the widget, it's content is loaded using AJAX. AJAX request has no real page. So getting current page id in widget's skin is not possible at some points.

Using CakePHP FormHelper with Bootstrap Forms

CakePHP's FormHelper is how you generate forms when making CakePHP applications. As one might assume, this includes generating input elements, like so:
$this->Form->input('abc');
Which will produce HTML something like this:
<div class="input text">
<label for="ModelAbc">Abc</label>
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
Now, sadly, Bootstrap wants something like the following:
<div class="control-group">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
</div>
How do I make CakePHP produce this output?
Inspired by lericson's answer, this is my final solution for CakePHP 2.x:
<?php echo $this->Form->create('ModelName', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
)));?>
<fieldset>
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label'), // the preset in Form->create() doesn't work for me
)); ?>
</fieldset>
<?php echo $this->Form->end();?>
Which produces:
<form...>
<fieldset>
<div class="control-group required error">
<label for="Fieldname" class="control-label">Fieldname</label>
<div class="controls">
<input name="data[Fieldname]" class="form-error" maxlength="255" type="text" value="" id="Fieldname"/>
<span class="help-inline">Error message</span>
</div>
</div>
</fieldset>
</form>
I basically added the 'format' and 'error' keys, and added the control-label class to the label element.
Here's a solution for Bootstrap 3
<?php echo $this->Form->create('User', array(
'class' => 'form-horizontal',
'role' => 'form',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'form-group'),
'class' => array('form-control'),
'label' => array('class' => 'col-lg-2 control-label'),
'between' => '<div class="col-lg-3">',
'after' => '</div>',
'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
))); ?>
<fieldset>
<legend><?php echo __('Username and password'); ?></legend>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
In case a field needs its own label:
<?php echo $this->Form->input('username', array('label' => array('text' => 'Your username', 'class' => 'col-lg-2 control-label'))); ?>
Here's one way:
<?php echo $this->Form->create(null, array(
'inputDefaults' => array(
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'class' => '')
)); ?>
Your answer is correct, but for the benefit of other users there's some other tweaks you can do to take advantage of the error/help text:
Add form-horizontal to class in the Form->create() for more compact forms (labels on the left of the input, rather than on top)
Here's how to put help text underneath a field (has to be done for each field), not forgetting to close the </div>.
echo $this->Form->input('field_name', array(
'after'=>'<span class="help-block">This text appears
underneath the input.</span></div>'));
and to correctly display errors:
// cake 2.0
echo $this->Form->input('abc', array(
'error' => array('attributes' => array('class' => 'controls help-block'))
));
Outputs:
<div class="control-group required error">
<label for="ModelAbc" class="control-label">Abc</label>
<div class="controls">
<input name="data[Model][Abc]" class="" maxlength="250" type="text" id="ModelAbc">
</div>
<!-- error message -->
<div class="controls help-block">This is the error validation message.</div>
<!-- error message -->
</div>
It's extra mark-up and not as neat as bootstrap but it's a quick fix. The alternative is to do each error message individually.
and it lines up nicely. I haven't discovered an easy way to make use of inline messages yet however.
Applying the same principle as above to the form->end function as follows:
<?php echo $this->Form->end(array(
'label' => __('Submit'),
'class' => 'btn',
'div' => array(
'class' => 'control-group',
),
'before' => '<div class="controls">',
'after' => '</div>'
));?>
To produce:
<div class="control-group">
<div class="controls">
<input class="btn" type="submit" value="Submit">
</div>
</div>
small add for another comments:
if you whant add class and change label base text, you can write next
<?php echo $this->Form->input('Fieldname', array(
'label' => array('class' => 'control-label','text'=>'HERE YOU LABEL TEXT')
)); ?>
I had the same problem using slywalker / cakephp-plugin-boost_cake, I open a ticket and he had it fix in a few hours, he updated to 1,03 and told me to use it like this
<?php echo $this->Form->input('email', array(
'label' => array(
'text' => __('Email:'),
),
'beforeInput' => '<div class="input-append">',
'afterInput' => '<span class="add-on"><i class="icon-envelope"></i></span></div>'
)); ?>
I hope it helps some one else too
To get it working with a horizontal form in bootstrap with bootswatch I had to use:
echo $this->Form->create(
'User',
array(
'action' => 'add',
'admin' => 'false',
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array( 'before', 'label', 'between',
'input', 'error', 'after' ),
'class' => 'form-control',
'div' => array( 'class' => 'form-group' ),
'label' => array( 'class' => 'col-lg-2 control-label' ),
'between' => '<div class="col-lg-10">',
'after' => '</div>',
'error' => array( 'attributes' => array( 'wrap' => 'span',
'class' => 'text-danger' ) ),
)
)
);
Then you can just use it as normal:
echo $this->Form->input( 'User.username' );
Luc Franken posted this link in his comment: http://github.com/slywalker/cakephp-plugin-boost_cake
It took me a while to notice it, so for those who are still looking for the simplest solution:
Simply add the CakePHP Bootstrap plugin from GitHub and let the helper do the job for you!

Categories