In CakePHP 2 how to insert input field without creating form? - cakephp

I'd like to create well formatted Form element with Form helper but without creation of form itself. So i wrote:
$this->Form->input(
'Kid.id',
array(
'type' => 'text',
)
);
This is ajax inside "Kids" controller response, and I'd like to update form after user make some actions.
I have no idea how to do it, except manually write HTML code.

Shame on me. This works I just forgot to add an echo at the beginning of line.

Related

how to use html php special chars function in a form input?

I have input form in a add.ctp
echo $this->Form->input('Postdetail.0.post_text', array('label' => 'متن ', 'id' => 'editkoniman'));
And i want the result of that warped in a htmlspecialchars function befor saving in a database because i use summernote bootstrap WYSIWYG editor in a modal box
Let me explain how to implement your requirement.
As like #mark said, just save the data in your controller
$this->{YourModel}->save($this->request->data);
Here $this->request->data contains your post data.
To display the post_text in your view just put the strings in h() function to escape html characters
h($yourvariable['ModelName']['post_text'])

Cakephp forms - Is there a way to make a field read only (in the view)

I have a Cakephp 1.3 form that allows users to edit the profile data. But some of the information in the forms needs to be read only (sometimes).
Is my only option to echo and format the field contents in the read only case or is there a flag in the Cake form that allows for read only fields. Ideally the read only fields would be greyed out similar to other interfaces.
echo $this->Form->create('User', array('url' => array('controller' => 'User', 'action'=>'editUser')));
echo $this->Form->input('id', array('type'=>'hidden'));
If (!isset($IsAdmin)) {
// Only display username - read only! Add code here
echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
} else {
// Admins can edit user names
echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
}
... more fields here
echo $this->Form->end(__d('users', 'Submit',true));
You can add a 'disabled' key to the options array, however realise that this is only the front-end/presentation of the form, people will be able to override the 'disabled' property of the input field and modify its value.
To prevent unwanted changes to be saved, you need to specify a 'fieldList' when saving the data using your model
To output a disabled form field;
echo $this->Form->input('fieldname', array('type'=>'hidden', 'disabled' => 'disabled'));
Then, when saving the data, specify a fieldlist (documentation: http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#saving-your-data)
$this->MyModel->save($this->data, true, array('field1', 'field2'));
The fieldlist should include all fields that are allowed to be updated by the user
The disabled attribute is fine but actually, input fields have a 'readonly' attribute. And it sounds like you want the field to still be shown to the user so using 'hidden' isn't really addressing what you want done.
So an alternative (and actually specifically addressing your requirement for 'read only'):
echo $this->Form->input('fieldname', array('readonly' => 'readonly'));
I found that using disabled prevents jquery click triggers from firing versus readonly still fires e.g. using a bootstrap datepicker text field
Here's a link to WC3 for it: http://www.w3schools.com/tags/att_input_readonly.asp
if you really want to make a field read only why you want to use form field, just echo the value, one can easily change disable or readonly attribute of form field with simple javascript or firebug.
Okay, after having tried several approaches, here is what I like.
1) Use readonly (disabled will remove the value after hitting "save", when you are in update mode, which sucks):
echo $this->Form->input('email', array('readonly' => 'readonly'));
2) To prevent this from updating when removing 'readonly' via browser plugins, you can add this to beforeSave of your model:
if(isset($this->data[$this->alias]['id'])) // id is only set if we update
{
unset($this->data[$this->alias]['email']);
}
Field lists are not comfortable. Why should I add all the fields, when I actually only want to exclude one?
Unsetting will prevent CakePHP from updating it in the database. Of course, after hitting save with an (invalidly) updated e-mail address, this update will be shown in the form once. But as the user has manipulated the HTML form and since the database field stays unchanged, this should not matter.
You can do either of two things:
Make the field hidden
(Ex. echo $this->Form->input ('username, array ('type' => 'hidden'));
Reset the value of username to it's original value, before submitting the form or possibly in beforeSave.

Drupal 7: Modifying menu HTML output?

I am trying to modify the HTML output in a Drupal 7 theme that I am creating.
Basically, instead of the < li >s containing just plain < a >s with text, I want to include some additional HTML inside the < a >.
I know that it's possible to modify the HTML created by the menus in Drupal. I can see the following call in page.tpl.php:
<?php print theme('links__system_main_menu', array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu',
'class' => array('links', 'clearfix'),
),
'heading' => array(
'text' => t(''),
'level' => 'h2',
'class' => array('element-invisible'),
),
)); ?>
which apparently calls the theme function, which creates the output. One way to modify the output would be to modify the theme_links function in theme.inc, right?
http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links
I also know that you can put a hook in template.php to override the function which creates the HTML. I can't figure out how to create the actual override function. Can somebody point me in the right direction, please?
What you would do is implement a hook to modify the output, not modify the "theme.inc" file directly.
For example, the accepted answer on this page: Drupal Override Custom Menu Template
And as a general rule, when you want to modify the output of something, either implement a hook (in a module or in the template.php of the active theme) or use a template with a predefined file name when such a case exists (when no template already exists, you can also modify the list of template suggestions using a module or the theme).

CakePHP: Field label in model

Are there any approaches to set in model field label? I dont want use 'label' property in form helper.
Please make sure I understand this correctly, you want to set a field label in the model, rather than using the form helper?
That violates basic MVC architecture. While Cake is flexible on some things, I don't think this is a possible option. I also don't see why you'd want to do it -- is there some reason that you don't want to use the label property in the form helper?
The basic issue is that the label for a form is part of the presentation layer, while the model represents the data. As such, it isn't possible (and I can't think of a situation where it'd make sense...) to assign a label to a data field which would then be used whenever that field is output.
If I misunderstood your question, please clarify.
What I do is setting up a convention in my models. I added a public attribute called "fieldLabels" to all the models, to assign default text labels for generic forms.
Example:
class MyModel extends AppModel {
// ...
public $fieldLabels = array(
'username' => 'User name',
'email' => 'e-mail address',
'phone' => 'Phone No.',
);
// ....
}
Then I pass around the labels to the view and use the extra parameter for the input, as sibidiba said:
echo $this->Form->input('title', array(
'label' => $fieldLabels['title'] . ': ',
));
In case I need special labels, I'll handle each case as an exception. Of course, if you want internationalization, that's a whole different topic.
Do you want to set the label's value? This is done in the view, but of course the value can originate from the controller/model. Like this:
echo $this->Form->input('title', array(
'label' => $titleLabel,
));
you can also disable the label element:
echo $this->Form->input('title', array(
'label' => null,
));
Not 100% sure, but I think you might be looking for Model::displayField
i suggest u directly use the helper u want.. bcos $form-input() creates div .. labels..
i personally had to weed out this on each line
with the direct helper like
$form->text()
$form->textarea()
$form->select()
u can keep the code much cleaner.
Note: $form->input saves time when used right...

CakePHP passing parameters to action

Hi im kinda new in cakephp and having a lot of trouble adjusting.. Here's my biggest problem ..
Im trying to pass a parameter to an action, it does load, but when my script goes from the controller to the view, and goes back to the controller again, its gone.
CONTROLLER CODE
function add($mac = 0)
{
if(isset($this->params['form']['medico']))
{
$temp= $this->Person->find('first', array('conditions' => array('smartphones_MAC' => $mac)));
$id= $temp['Person']['id'];
$this->Union->set('events_id', $id+1);
$this->Union->set('people_id', $id);
$this->Union->save();
}
VIEW CODE (This is a menu, i only have one button right now)
<fieldset>
<legend>SELECCIONE SU ALERTA</legend>
<?php
echo $form->create('Event');
echo $form->submit('EMERGENCIA MEDICA',array('name'=>'medico'));
echo $form->end();
?>
</fieldset>
When you create the form you don't include the additional url parameters or the fields as inputs. Without either of these the parameters will vanish as they are not part of the new request. You can append additional parameters to the form submission url with
$form->create('Event', array(
'url' => array('something', 'somethingelse')
));
This will create a form that points at /events/add/something/somethingelse.
I'm no big fan of using some helpers (like $html) or some methods (like $form's create() and end()). I kinda didn't get your problem, but I think it might be that you have to make a POST request to the same url you are actually into.
<form method="GET" action="<?=$this->here ?>">
Maybe you should give a further explanation of what you are trying to achieve.
You might want to try using named parameters.
I asked a similar question which you might find helpful:
cakephp adding record with some parameters fixed

Resources