Localization of array value in CakePHP - php

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.

Related

Function that creates a input name from a string in CakePHP

I would like know if there is a function in CakePHP that transforms Mymodel.Mycolumn into data[Mymodel][Mycolumn].
I know how to do this with PHP only, but I would like to know if there is a built-in CakePHP function for it.
EDIT:
I don't need the input, only the name.
See the pretty complete CakePHP Cookbook. To use the current model:
echo $this->Form->input('Mycolumn');
Or to specify the model:
echo $this->Form->input('Mymodel.Mycolumn');
Creates:
<input type="text" id="MymodelMycolumn" name="data[Mymodel][Mycolumn]">
if you're sending, you can do this:
<?php
echo $this->Form->create('Mymodel');
echo $this->Form->input('Mymodel.Mycolumn', array('label' => 'add data for Mycolumn'));
echo $this->Form->submit('submit', array('class' => 'form-submit', 'title' => 'Enter'));
?>

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.

` (backtick) appearing in Cakephp view

A backtick appears in my rendered view in my cakephp application. They sometimes appear in two places:
At the very start of the body
After echo $this->Form->create();
I can't see a pattern that causes the problem. Is there a known problem that produces backticks?
Example of backtick appearing after Form->create()
<div class="users form">
<h2><?php echo __('Change password'); ?></h2>
<?php
echo $this->Form->create();
echo $this->Form->input(
'password_new',
array(
'label' => 'New Password',
'type' => 'password',
'div' => 'input password required',
'required' => 'required',
)
);
echo $this->Form->input(
'password_new_confirm',
array(
'label' => 'New Password Again',
'type' => 'password',
'div' => 'input password required',
'required' => 'required',
)
);
echo $this->Form->end(__('Change password'));
?>
</div>
<div class="actions">
<?php echo $this->Html->link(__('Back To Settings'), array('action' => 'settings',)); ?>
</div>
Screenshot of HTML output:
Found a backtick in my User model, such an idiot, thanks Summea
As you have noted in your answer, it sounds like the backtick ended up being in another file (in this case, your User model file.)
This is one of the downsides of working with CakePHP: text that is outside of your <?php ... ?> tags will be rendered to the final, outputted view that end users will see (in addition to the actual "view" that was only supposed to be shown to the end user.)
This can even happen when using an echo statement in a particular controller, as well.
For example, using:
echo 'random text that should not be displayed from a controller';
will result in displaying that "random text" in the final, outputted view.
Basically, CakePHP does not seem to enforce a super clean view "output" process (and this is probably, in part, because of the way PHP inherently works...) so it is mostly up to the developer to keep track of desired view output.

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.

cakephp input type number

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.

Categories