CakePHP 3: Form Input Name Gets Changed In Controller - forms

I am implementing a form to try and add images associated with a product.
Here is the process I am using:
Perform an AJAX call to get get all potential images.
Display a thumbnail of each image with a checkbox underneath it.
The user selects one or more checkboxes and submits the form.
In order to associate the checkbox with the image, I am setting the checkbox name as the URL of the image. That way when the user submits the form any URL's passed to my controller will represent images the user has selected.
On Form Submission however, these URL's are being manipulated ('.' chars become '_' chars). This is posing a bit of a problem:
Example 1:
<label>Use this Image
<input name="http://images.somesite.com/pic/Qmh8Yx6f7Uq1Pnh6mjyfqg.c-o.jpg" type="checkbox">
</label>
On submission however, in my controller when I print out $this->request->data
Array(
[http://images_somesite_com/pic/Qmh8Yx6f7Uq1Pnh6mjyfqg_c-o_jpg] => on
)
I can't use a str_replace('_', '.', $url), because some image's URL's naturally have an underscore in them.
Example 2
<label>Use this Image
<input name="http://ecx.images-amazon.com/images/I/71LJdh3kWnL._SL1390_.jpg" type="checkbox">
</label>
becomes
Array(
[http://ecx_images-amazon_com/images/I/71LJdh3kWnL__SL1390__jpg] => on
)
Is there a way to prevent CakePHP from manipulating these input names before they get passed to the controller?
Thanks!

Related

how to create a form elements with value in moodle

I am trying to create a form element text with value in moodle.
I trying the below :
$mform->addElement('text','test', get_string('test'));
This is used to create a text box . i want to add value also like
<input type='text' value='<?php .... ?>' />
How to do that in moodle
When you instantiate the form, you can pass the relevant data into it, e.g.
$form = new my_form();
$formdata = (object)array('test' => 'The value to display in the textbox');
$form->set_data($formdata);
(Usually the data passed into the form is some existing data retrieved from the database).
I'm not sure what kind of data did you mean here.
If you want to set user data (for example, you are developing a form that edits existing record), then use $form->set_data() after creating a form instance as Davo suggested.
If you want to pre-fill the form with default value, then use this inside the form definition:
$mform->addElement('text','test', get_string('test'));
$mform->setDefault('test', 'your default value');
You can use both methods, in which case the data from set_data() will have priority.

Submit Form with nameless input in HtmlUnit

Good morning!
I need to post a form, I've already changed all the values inside that form (drop-drown list, text fields , check-boxes, etc), in this way:
final HtmlPage page4 = webClient.getPage("somepagemakinmemad");
HtmlForm formx= page4.getFormByName("lista_grupos");
changing values like:
HtmlSelect duracion = (HtmlSelect) page4.getElementByName("p_duracion_"+datum.getCrn()+"_1");
HtmlOption option1 = duracion.getOptionByValue("0029_029");
duracion.setSelectedAttribute(option1, true);
That is, I am changing the values through obtaining the html element from the page, not through the form (is that okay anyway?).
And then I try to submit the form, through:
<input id="p_guardar" class="boton" type="button" onclick="validaAPG()" value="Guardar">
Which is the "button-like" input where someone clicks when is over with the form.
I have to say, when I print the html code of the form, with "asXml()", I see the form the with selected values I've intended to select.
Question: How can I click on that button and send the form? That input runs a script, when clicked
Thanks everyone, let me know if you need any other kind of info.
You can use below code to click on the button to submit the form.
HtmlButtonInput button = (HtmlButtonInput)page.getElementById("p_guardar");
page = button.click();
You dnt need to care what script runs when clicked.If you dont need to run the script remove the attribute.

Is there a way to deliberately make a form field that doesn't submit?

A lot of folks on Stack Overflow are probably trying to fix forms that don't submit, but I'm actually hoping to do the opposite!
What I'd like to do for an art project is make a form with a "joke" field -- say, your SSN, your bank account number, your fingerprints or retina scans or DNA code, or something super personal like that. But I don't want the number in our server logs, and I don't want it to be transmitted over the internet at all. I don't want any legal liability!
Basically the idea is just to ask for something audacious, but not to handle the data that may or may not come from users who actually put it in.
So, is there a way to make a field that acts as a normal form field, but where nonetheless we would feel "safe" that users who actually do put their sensitive info in the field will be protected?
What's the "safest" approach to something like this?
Form fields require a name to be submitted:
If any of the following conditions are met, then skip these substeps for this element:
[…]
The field element is not an input element whose type attribute is in the Image Button state, and either the field element does not have a name attribute specified, or its name attribute's value is the empty string.
[…]
So you could simply use an input without name attribute:
<input type="text">
Be careful with your "jokes", if you want that the information of the field is not submitted, then, you can simply leave it out of the form element like this:
<form action="... >
<input type="... >
</form>
<input type="... > <!-- This field won't be submitted-->

Getting a value from a label at POST

There's a place into my screen that I populate a label with a specific string value after some interaction with my user during the runtime. I use javascript for that.
Is there anyway to get the value of this lavel with my controller after its POST method is activated ?
Thanks, guys !
Option #1
Put the value in an HTML <input> element with a name attribute? Might need to dress down
the input element, since it will look like a textbox.
Option #2
Mirror the value in a hidden input <input type="hidden" value="yourValue" /> inside the form you're posting.

Programmatically submitting a form while using AjaxForm

I wanted to find a way to upload a single file*, in the background, have it start automatically after file selection, and not require a flash uploader, so I am trying to use two great mechanisms (jQuery.Form and JQuery MultiFile) together. I haven't succeeded, but I'm pretty sure it's because I'm missing something fundamental.
Just using MultiFile, I define the form as follows...
<form id="photoForm" action="image.php" method="post" enctype="multipart/form-data">
The file input button is defined as...
<input id="photoButton" "name="sourceFile" class="photoButton max-1 accept-jpg" type="file">
And the Javascript is...
$('#photoButton').MultiFile({
afterFileSelect: function(){
document.getElementById("photoForm").submit();
}
});
This works perfectly. As soon as the user selects a single file, MultiFile submits the form to the server.
If instead of using MultiFile, as shown above, let's say I include a Submit button along with the JQuery Form plugin defined as follows...
var options = {
success: respondToUpload
};
$('#photoForm').ajaxForm(options);
... this also works perfectly. When the Submit button is clicked, the form is uploaded in the background.
What I don't know how to do is get these two to work together. If I use Javascript to submit the form (as shown in the MultiFile example above), the form is submitted but the JQuery.Form function is not called, so the form does not get submitted in the background.
I thought that maybe I needed to change the form registration as follows...
$('#photoForm').submit(function() {
$('#photoForm').ajaxForm(options);
});
...but that didn't solve the problem. The same is true when I tried .ajaxSubmit instead of .ajaxForm.
What am I missing?
BTW: I know it might sound strange to use MultiFile for single-file uploads, but the idea is that the number of files will be dynamic based on the user's account. So, I'm starting with one but the number changes depending on conditions.
The answer turns out to be embarrassingly simple.
Instead of programmatically submitting using...
document.getElementById("photoForm").submit();
... I used...
$("#photoForm").submit();
Also, since I only need to upload multiple files on occasion, I used a simpler technique...
1) The form is the same as my original...
<form id="photoForm" action="image.php" method="post" enctype="multipart/form-data">
2) The file input field is basically the same...
<input id="photoFile" "name="sourceFile" style="cursor:pointer;" type="file">
3) If the file input field changes, submit is executed...
$("#photoFile").change(function() {
$("#photoForm").submit();
});
4) The AjaxForm listener does its thing...
var options = {
success: respondToUpload
};
$('#photoForm').ajaxForm(options);