cakephp input type number - php

I have a mobile website form that I want to add type attributes to the inputs so that there correct keyboard format will pop up.
However in cakephp setting the type as number a textarea is created instead of the input and the type is not set.
Setting type as text does work.
How do I overide this and have cakephp just keep it as a text input with type=number?
<?php echo $form->input('phone',array('type' => 'number')); ?>
Result:
<textarea id="UserCardExpires" rows="6" cols="30" name="data[User][card_expires]"class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset"></textarea>
This is ok:
<?php echo $form->input('postcode' ,array('type' => 'text')); ?>
Result
<input type="text" id="UserPostcode" name="data[User][postcode]" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">

On older versions of Cake, the Form helper won't automagically interpret $options['type'] as the HTML5 input-element type attribute. You have to force it by using "type" as an option on an explicit text element.
Use the following:
$form->text( 'phone', array( 'type' => 'number' ) );

I think phone numbers might be:
echo $form->text( 'phone', array( 'type' => 'tel' ) );
EDIT:
Sorry I'm an idiot, thats HTML5.

Related

Codeigniter form validation - how to reject default values

I have a form with default values set, e.g.:
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail', 'jdoe#example.com'); ?>" />
I validate the form using Codeigniter's form_validation class. For this field:
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email'
));
Here's my problem. When the user leaves the field untouched and submits it's default value 'jdoe#example.com', I want to reject this value. I know I can use a callback, but that would mean I have to write a callback for every field of the form. Not nice!
I've looked into the validation rules. 'differs' looked promising, but it only checks whether one field is different from another field.
Now what's the best way to deal with default values in Codeigniter?
Sounds like you should be using a placeholder instead of a default value. Otherwise what you are asking for doesn't make much sense.
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail'); ?>" placeholder="jdoe#example.com" />
Otherwise you could try the built in validation rule "in_list" e.g. in_list[red,blue,green]
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email|in_list[jdoe#example.com]'
));
EDIT: Actually you would want the opposite of the in_list rule, i think that would require a callback, but you would only need to write one callback to use for each field.

Getting CakePHP HtmlHelper to generate a "date" input

I've been making some basic CRUD pages for my cakePHP app using the HtmlHelper for the views. This is handy for building forms but for date inputs the helper by default generates 3 select boxes for the date which is quite cumbersome to use.
HTML5 introduces the input[type=date] and most browsers now incorporate some nice native interfaces to deal with it; e.g. Chrome produces a nice date-picker for date inputs.
I know it is possible to make the HtmlHelper just make the input a text box instead of the 3 dropdown by doing the following:
echo $this->Form->input('my_date', array('type' => 'text'));
But when I do
echo $this->Form->input('my_date', array('type' => 'date'));
it ignores the 2nd arguement and goes back to the 3 selects.
Is there a way to get the helper to make a date input?
It seem the HtmlHelper has not yet evolved to make use of the "date" input.
If you tell the helper to generate the date input as a text field, adding a jQuery one-liner can convert it to a date input.
So:
echo $this->Form->input('my_date', array('type' => 'text'));
to generate the field. Then:
$('#idOfMyDate').attr('type', 'date');
To change it to a date input.
If anyone has a better way I'd be keen to hear it.
The CakePHP FormHelper uses Widgets to render different input types. For "datetime" types, it uses the DateTimeWidget per default.
To get a regular input with the attribute type="date", you just have to tell CakePHP which widget to use.
In the View (usually App\AppView.php), you can configure the FormHelper:
<?php
namespace App\View;
use Cake\View\View;
class AppView extends View
{
public function initialize() {
$this->loadHelper('Form', [
'widgets' => [
'datetime' => ['Basic'],
],
]);
}
}
?>
The BasicWidget is the most basic widget which is used to render regular text inputs.
Then, in your view, you can just use 'type' => 'date' as expected:
echo $this->Form->input('my_date', array('type' => 'date'));
Or, since CakePHP already sets the type to "date" since the database column is a datetime field you can just leave it like this:
echo $this->Form->input('my_date');
The result is a regular text input with type="date".
For future readers: In the most recent version of CakePHP, you would use the method Form::control instead of Form::input. Everything else still applies.
Try this:
echo $this->Form->text('my_date',array('type' => 'date');
Like this it'll work as a charm
<div class="form-group">
<label for="Description"><?php echo __('Date'); ?></label>
<div class='input-group date' id='datetimepicker1'>
<?php echo $this->Form->input('scheduled_date', array('label'=> false, 'div' => false, 'class'=>'form-control', 'type' => 'text')); ?>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
Some explanation:
'div' => false: it's necessary to desable the div rendered by input() FormHelper function
'label' => false: it's necessary to desable the label rendered by input() FormHelper function
Take a look at cake PHP form helper doc for more details
I solved this problem with jquery
PHP
<?= $this->Form->control('birth_date', ['value' => $birth_date->i18nFormat('yyyy-MM-dd'), 'type' => 'text']); ?>
JS
$('#birth-date').attr('type', 'date');
It's obvious that Cake's FormHelper is messing up with <input type "date">. Therefore I solved this problem the following way (in CakePHP 2.x)
Copy FormHelper.php from lib\Cake\View\Helper\
Paste it to app\View\Helper. Now Cake will use your Form Helper instead of its own.
Open the new FormHelper.php, go to protected function _getInput($args) {, search for case 'date': and change it from:
case 'date':
$options += array('value' => $selected);
return $this->dateTime($fieldName, $dateFormat, null, $options);
to:
case 'date':
return $this->{$type}($fieldName, $options);
Cake will now stop transforming <input type="date"> into select boxes.
Keep in mind that with every future release of Cake 2.x you will have to transfer possible changes in Cake's Form Helper into your own Form Helper manually.

How do I set the 'value' in my Zend form checkbox?

The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.

Localization of array value in CakePHP

I am trying to localize an existing cakephp application. For the most part I have it working the way I want but there is one scenario I can't figure out.
I am using the form helper to create a form like this:
echo $this->Form->create('search', array('url' =>
array('controller' => '/',
'action' => '/search/searcher'),
'onsubmit'=>'return checkForm();',
'class'=>'find-form'));
echo '<fieldset>';
echo $this->Form->input('name', array(
'type'=>'hidden',
'div'=>false,
'id'=>'name',
'class'=>'nice',
'label'=>false,
'value'=>''
));
echo $this->Form->input('myvalue', array(
'type'=>'text',
'div'=>false,
'id'=>'searchval',
'class'=>'nice',
'value'=> __('Enter search string'),
'label'=>false));
The problem is with the line:
'value'=> __('Enter search string'),
The resulting html looks like this:
...
<fieldset>
<input type="hidden" name="data[search][name]"
id="name" value="" />
Enter Search String
<input name="data[search][myvalue]"
type="text"
id="searchval"
class="nice"
and so on.
The value "Enter Search String" is translated properly but it is outside of the input tag so it shows up outside of the search box.
I've tried various permutations of the code like:
'value'=> echo __('Enter search string'),
'value'=> `__('Enter search string')`,
And nothing seems to work. As I said it works in other areas of the view (in as an array value though) but I can't get this one working.
BTW, the code before I started looked like this:
'value'=> 'Enter search string',
Any ideas?
Thanks for your time.
In CakePHP 1.x the __() function echoes the content by default, while in this case you only want to store it. You can use the second argument to return rather than echo the contents, like this:
'value' => __('Enter search string', true),
From CakePHP 2.0 on upwards you should no longer have to do this, as it returns by default.

Enable WYSIWYG for a custom textarea form field in drupal

i have created a custom form in my custom drupal6 module and the form has a textarea.
I am outputting the form as html embedded inside the php code, like the following:
function custom_my_form()
{
$f = '<form name="add_user" action="" method="post">
<label> Name </label>
<p><input type="text" name="name" value="" /></p>
<label>About Yourself</label>
<p><textarea name="desc"></textarea></p>
<input type="submit" name="submit" value="Add">
</form>';
return $f;
}
I have installed and enabled the WYSIWYG module.
My default input format is "Filtered HTML" and i have chosen the FCK editor for this input format.
Now i want to enable the WYSIWYG for the textarea field in this form.
How can i go ahead with this ?
Well, how ever you create forms is up to you, it just depends if it has ever come back to bite you in the butt...
Look at the comment module. I had noticed that it was possible to choose input format for comments, and when Full/filtered HTML was selected, the WYSIWYG editor kicked in. Here is the related code:
$form['comment_filter']['comment'] = array(
'#type' => 'textarea',
'#title' => t('Comment'),
'#rows' => 15,
'#default_value' => $default,
'#required' => TRUE,
);
if (!isset($edit['format'])) {
$edit['format'] = FILTER_FORMAT_DEFAULT;
}
$form['comment_filter']['format'] = filter_form($edit['format']);
So, you define an array with two elements, one of which is the textarea itself, and the other one the format chooser generated by filter_form, and that's all.
This was from http://groups.drupal.org/node/104604
For D7, its even simple. We got new type called text_format.
$form['comment'] = array('#type' => 'text_format',
'#format' => 'full_html',
'#title' => t('Description'),
'#required' => TRUE);
Yes, you can do this. Just add the following javascript to your page.
<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
tinymce.init({selector:'textarea'});
</script>
This script automatically converts the text areas in your html code to rich text editors
More info at this link.
First, you should not create forms like that. Instead, use Drupal's built-in Forms API. Take a look at the Form API Quickstart Guide for more information.
This page on Drupal's site will help you add WYSIWYG to your custom forms: http://drupal.org/node/358316

Categories