CakePHP form - remove month field name - php

I'm setting up a Stripe payment form, so I need to remove the names of my month and year fields so they aren't sent to my server. The following code though, still gives the field a name of '[month]' and if the text of the array's name variable were 'xyz', the field would be named 'xyz[month]'. How can I remove the entirety of the field's name?
echo $this->Form->month('expiration_month', array('name' => '', 'data-stripe' => 'exp_month', 'default' => 'January'));

According to the documentation, the name of the <select> element is derived from the first function argument ("expiration_month" in your example.) If you take a look at the code, you can see the value "month" is hard-coded.
The only way around this is to manually build your own <select> element, or just ignore the value when it comes to your server. But why make users fill out a form element that isn't going to be processed by your server?

So, the quick and dirty way around this is to find the select field with js and overwrite the name attribute.

Related

Is it better to create N hidden fields for N element of array, or one hidden field with serialized array as value?

So I am using CakePHP and I need to POST array of values. How it should be done? Either by creating input for every element or one input with all elelents. First one is fine with the convention, but pumping DOM with dozens of inputfields feels counter-productive.
So witch approach is considered as good practice?
foreach($tms['deletable'] as $tm){
echo $this->Form->control("ids[]", [
"type" => "hidden",
"value" => $tm->id,
]);
}
Or
echo $this->Form->control("ids", [
"type" => "hidden",
"value" => implode(",",collection($tms['deletable'])->extract("id")->toList())
]);
I'd say if you don't plan to modify the hidden field values in JavaScript, then go with the one field.
On the other hand, it requires you to do special handling on the receiving side (exploding), so use multiple fields.
A dozen fields are nothing if you look at the bloated HTML pages these days.

form_multiselect gives only one option

Sorry, but this is kind a homework question...
I have to make a webpage with codeigniter and I have to use multiple select component.
So my code.
Part in *view.php file:
<br>Keywords:<br>
<?php echo form_multiselect('keywords', $keys); ?>
Also there is submit button, and after it pressed I take POST data. For debugging tried:
var_dump($_POST['keywords']);
This always shows, that there is only one option selected, for example, string(1) "2"
Can someone advice how should I modify my code to get all selected items.
Please try:
<?php echo form_multiselect('keywords[]', $keys); ?>
A multiselect form field must have a name with array notation.
You would expect codeignitors function to accommodate this, but it doesnt (well not when i last used CI in 2010)
From the Codeigniter documentation:
form_multiselect()
Lets you create a standard multiselect field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value or values you wish to be selected. The parameter usage is identical to using form_dropdown() above, except of course that the name of the field will need to use POST array syntax, e.g. foo[].
The last sentence states you need to use POST array syntax, so the name of the select should be, in your case
name="keywords[]"

posting 2 values to array from single checkbox

I have a situation where I'd like to post two values to a search query string array from a single checkbox. Because of the design, I can't just add another checkbox.
The checkbox in question looks like so:
<input name="wpp_search[property_type][]" value="rental_home" type="checkbox" id="choice_c"/>
<label for="choice_c">For Rent</label>
what I now get in the query string is ...
{url}?wpp_search[property_type][0]=rental_home
but what I need is to tie two values to that one check so I end up with this:
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][1]=building
Any simple solutions? There is only one other checkbox that already feeds that array, so I could force this one to be
{url}?wpp_search[property_type][0]=rental_home&wpp_search[property_type][2]=building
You can seperate values with for example "|" like this value="value1|value2". Then later you can use the function explode: $p = explode("|", $value); and you get 2 values.
Generally it's not possible to send one value as two values.
One way I could imagine is that you reconfigure the ini-setting of arg_separator.input:
arg_separator.input = ";&"
This will allow you to use ; as well to separate values which then would allow you to inject the second value per that value:
<input name="wpp_search[property_type][0]"
value="rental_home;wpp_search[property_type][1]=building"
...
/>
If you make use of the value ; in other form values you naturally need to properly escape them to make this compatible.
Another way is that you insert a hidden field with that value and if your checkbox is checked, you change the name of it to the right name. If unchecked, you change the name of it back to something not right:
<input type="hidden" name="---wpp_search[property_type][1]" value="=building" />
Take the javascript reference of your choice and do the needed DOM manipulation on click of the other checkbox to remove the three --- at the beginning of the name.

Extract URL from link field in Drupal?

I have a link field that is composed from a URL and the title, I need to print out only the URL of the link field without the title in my node content type tpl file, is that possible ?
Thanks!
It should be as easy as:
$url = $node->field_name_of_field[$node->language][0]['url'];
I'll break that down a bit:
Fields are members of the node object and are always prefixed with field_ so a field called my_field can be found with $node->field_my_field.
Each field members of the node object is itself an array of all different language versions for the field, keyed by the language key. To access the field value for the language that the node is denoted as you would use: $node->field_my_field[$node->language] or perhaps $node->field_my_field[LANGUAGE_NONE] (which is the default).
Further to that, each language array can potentially have multiple field values in it, if the cardinality of the field is greater than 1. If you have a field (e.g. images) with multiple values allowed you would run through each like this:
foreach ($node->field_my_field[$node->language] as $delta => $item) {
}
Within each item of the language array are the actual field values. Fields may have multiple columns (for example the link module has url, title and attributes). To continue with the previous example you would find the url and title like this:
$url = $node->field_name_of_field[$node->language][0]['url'];
$title = $node->field_name_of_field[$node->language][0]['title'];
Hope that helps!

drupal7: using form_set_value()

I am trying to update a value during the validate phase of a node-form, i.e. if the custom validation error is fired, just empty one of the fields.
for the last 30 hours I am trying to make sense of the drupal api, but I am giving up. I just do not seem to get the idea what the different values mean.
the function is: form_set_value($element, $value, &$form_state)
now i understand that the last value is simply the $form_state, that I am having through the validate function. but what about $element and $value?
I was trying a lot and apparently the desired value resides in $form['field_name']['und'][0]['value']['#value'] and only there.
but when I am trying form_set_value($form['field_name']['und'][0]['value']['#value'],'foo',$form_state) it raises
Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()
and when I am trying:
$newvalue = $form['field_name']['und'][0]['value'];
$newvalue['#value']='foo';
form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);
it raises:
Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()
Thanks for any help!
Unless I'm really mis-understanding what you're trying to do this will work:
$form_state['values']['field_name']['und'][0]['value'] = '';
When the form is re-built after a validation error the values in $form_state['values'] are used to re-populate the fields in the form. So, if you reset the value in the $form_state['values'] array it will not be there when the form is shown with the validation errors.
After a lot of debugging, I finally managed to make this work. The trick lies inside $form['complete form']. but first things first, how does form_set_value() work and what does it do?
the form_set_value() function
as the docs suggested:
if you want to update the value of $form['elem1']['elem2'], which should be stored in $form_state['values']['elem1']['elem2'], you would set $element['#parents'] = array('elem1','elem2').
now what does that mean? in my case, I had a textfield called 'field_event_title', which is the name I gave it on creation. in $form, all fields have a sub-array in $form['field_name'], which is in my case $form['field_event_title']. this is where the submitted value also is stored. now since it is a textfield, drupal maintains both the language and the delta [question for editors: is this right?] of the inputted data. so in fact, the value is not stored in $form['field_name']['value'], but in $form['field_name']['und'][0]['value'] (['und']=language; [0]=delta). note that 'und' is the drupal key for the default language of the site, if it is, say, in german, then it would be 'de', however, in most cases it should be 'und'.
to actually change the value using form_set_value(), one is ought to invoke the function by writing:
form_set_value($form['field_name'],array('und' => array(0 => array('value' => 'foo'))),$form_state);
i.e. $element = $form['field_name'] $value=array('und' => array(0 => array('value' => 'foo')))
updating a form to repopulate it with different values than submitted (or clearing them)
but that did not work in my case since I wanted to clear fields once a custom validation error has been invoked. now one would suspect that the form repopulates itself using the values inside $form_state['values'] (which is actually the place where the values are stored, the actual place that gets updated when using form_set_value() and the place which generates the $form later.), but that is not the case: it uses the values inside $form_state['complete form'], which is a 'copy' of $form (notice that it is spelled 'complete form', with a space, not an underscore).
so using $form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; is what updates the values that actually repopulate the form on a validation error. (note: you can, as do I in my usecase, set it to =NULL to simply empty the field).
summary
now where is the difference between $form['field_name'] (e.g. updating through form_set_value()) and $form['complete form']? well, the former updates the actual value, which then gets stored inside the database, the latter is being used to repopulate a form when it failed a validation.

Categories