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.
Related
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'));
?>
I'm trying to place data in hidden text in yii, but I don't know how.
I need a similar code to a regular php syntax:
<input type="hidden" name="field_name" value="a"/>
It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.
Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.
in views
hidden field with model and form:
<?php echo $form->hiddenField($model, 'name'); ?>
or without model
<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>
Yii hidden input :
<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
In Yii2 this has changed too:
<?= Html::activeHiddenInput($model, 'name') ;?>
References:
http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/
https://github.com/yiisoft/yii2/issues/735
if data from database and value or size field:
echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Yii 1
<?php echo $form->hiddenField($model, 'name'); ?>
Yii2
<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>
Also, worth noting for Yii2, the array parameter works different to a normal form field.
E.G. A normal input would look more like this.
<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>
Hope this helps.
for yii2 you can try this
<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>
It worked for me
Alternatively,
echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));
This would set hidden field value as the id from model. The [$i] is useful for multiple record update.
Here are two ways to do that...
without model
echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));
with model
echo $form->hiddenField($model, 'name');
I have tried this below validations for my fields.
My fields are Title, Content.
var $validate = array('title' =>array('alphaNumeric'=>array('rule'=>'alphaNumeric','required'=>'true','message'=>'Enter a title for this post',)),
'content'=>array('alphaNumeric'=>array('rule'=>'alphaNumeric','required'=>'true','message'=>'Enter some content for this post',)));
But whenever i enter some text in my form and try to submit, it shows error message...
Is there any problem with the validations ?
Here's my form
<h2> Add a Post Here </h2>
Please fill in all the fields.
<?php
echo $this->form->create('Post');
echo $this->form->error('Post.title');
echo $this->form- >input('Post.title',array('id'=>'posttitle','label'=>'title','size'=>'50','maxlength'=>'255','error'=>false));
echo $this->form->error('Post.content');
echo $this->form->input('Post.content',array('id'=>'postcontent','type'=>'textarea','label'=>'Content:','rows'=>'10','error'=>false));
echo $this->form->end(array('label'=>'Submit Post'));
?>
you can use custom regular expression.
'rule' => array('custom', '[a-zA-Z0-9, ]+'),
I had to change the regular expression as follows to get it to work.
'alphaNumeric' => array(
'rule' => array('custom', '/[a-zA-Z0-9, ]+/'),
'message' => 'Only letters and numbers allowed',
Why won't the built in alphaNumeric rule do this already?
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.
I have table user which have fields username, password, and type. The type can be any or a combination of these: employee, vendor and client e.g. a user can be both a vendor and a client, or some another combination. For the type field I have used the multiple checkbox (see the code below). This is the views/users/add.ctp file:
<div class="users form">
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php __('Add User'); ?></legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('type', array('type' => 'select', 'multiple' => 'checkbox','options' => array(
'client' => 'Client',
'vendor' => 'Vendor',
'employee' => 'Employee'
)
));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
In the model file I have used a beforeSave callback method:
app/models/user.php
function beforeSave() {
if(!empty($this->data['User']['type'])) {
$this->data['User']['type'] = join(',', $this->data['User']['type']);
}
return true;
}
This code saves the multiple values as comma separated values in the database.
The main problem comes when I'm editing a user. If a user has selected multiple types during user creation, none of the checkboxes is checked for the type.
you have $this->data['User']['type'] = join(',', $this->data['User']['type']); in the beforeSave() so you would need to do the same in afterFind() $this->data['User']['type'] = explode(',', $user['User']['type']); which would make it an array again.
This is however a horrible design, you should consider doing it properly.
#dogmatic69 after following this $this->data['User']['type'] = explode(',', $user['User']['type']); and the array is properly arranged the checked box is still unchecked