CakePHP 4 Forms: do not preselect on multiple select - cakephp

Here's the situation: I have a multi-staged tunnel that basically passes a user from page to page via form submits. Fill out stage one, click submit, get sent to page two and soforth. The trouble is that my fields are creating similarly-named form elements, so when the new page loads, Cake seems to see it as the same form as the previous one. Thus, the checkboxes selected in the first form get checked in the second form, even though they are of completely different values.
TL;DR is there a way to specify with this call that there should absolutely NOT be any prefilling of the fields?
<?= $this->Form->control('estimate_options.0.option_value', [
'type' => 'select',
'multiple' => 'checkbox',
'options' => $deliverables,
'label' => false,
'value' => !empty($selectedDeliverables->option_value) ? $selectedDeliverables->option_value : false,
]);
?>
As you can see, I'm attempting to pass "false" (I've also tried NULL) as the 'value' to specify that nothing should be selected, but they get checked anyway?

Related

How to ensure that empty select multiple option still will show up as key in request->data in cakephp3?

I am using select. I want to ensure that in CakePHP3 FormHelper, I always have the key inside request->data regardless if it is empty.
Currently, my code is
<?= $this->Form->select('rooms[]', $rooms, ['id' => 'room-tags', 'multiple', 'empty' => '']); ?>
I have tried hiddenField. It does not work.
I need to ensure that inside request->data I will always have the key rooms which points to an empty array.
You are defining the multiple option wrong, while passing it as a value makes it into the HTML element as an attribute, the form helper will not be recognize it as an option.
This is how you have to define it in order to for it to act as a option.
'multiple' => true

CakePHP: Multiple validation rule - don't show mandatory hint in select options

I have created a form in CakePHP where the user can select languages for a project via checkboxes. For these checkboxes, I have implemented a validation rule (more than one checkbox has to be checked). Everything works fine, but ALL checkboxes show the "mandatory" star:
This is very ugly in my opinion because it seems to the user that every checkbox MUST be checked to complete the form. My validation rules look like the following:
'Language' => array(
'rule' => array(
'multiple',
array(
'min' => 1
)
),
'message' => 'Please select at least one language'
)
'Language' is a seperate model and I use this for propagating my checkboxes. So, is there any trick to don't show this star near the checkboxes? The best would be to don't need to change anything on the Cake core...
EDIT:
The view code I am using for this is:
echo $this->Form->input('Project.Language', array('type' => 'select', 'multiple' => 'checkbox'));

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.

get data from combobox

Hi there i want to get data from the below combo box in cakephp for comparison.
echo $form->input('ac_owner', array('label' => 'Account Owner',
'options' => array('A','B','C'), 'default' => 2));
is it possible to get this value using javascript
please help.
thank you in advance........
The result of this is a normal <select> element that can of course be accessed by Javascript. The id, if you're looking for that, should be something along the lines of ModelNameAcOwner. Inspect the resulting HTML or DOM tree to find it.

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...

Resources