drupal7: using form_set_value() - php

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.

Related

Warning: array_keys() expects parameter 1 to be array, null given, although array appears properly formed?

I'm working with user data in WordPress, but I think this error is more of a general PHP issue on my end.
I use a function wp_update_user() which takes an array of keys and values corresponding to certain user fields, like display_name.
The code responsible looks as follows (note that I'm hardcoding the value for display_name for debugging purposes):
$returnValue = wp_update_user(array( 'ID' => $user->ID, 'display_name' => 'Test 123' ));
if (is_wp_error($returnValue)) {
print_r($returnValue);
} else {
echo "User update was a success, ID returned is " . $returnValue;
}
The if statement would output an error object if there is an error. However, the value is updating in the database as expected, and no error is returned from WP. The server, however, is giving me a series of warnings, which begin with Warning: array_keys() expects parameter 1 to be array, null given in /srv/www/mysite.com/current/web/wp/wp-includes/user.php on line 1993. I looked up the function array_keys() but as far as I can tell, I'm passing a correctly-formed array as required. This warning does stem from the wp_update_user() function.
As stated above, the actual code seems to work and do what I want it to do, so this warning isn't actually impeding any functionality. I suppose I can hide this warning output but in the interest of best practices I would like to get to the bottom of it. How can I solve this?
Pretty sure its not the parameter you pass that is wrong , but an internal WP process.Try calling that with a plain integer as id of an already existing user

CakePHP form - remove month field name

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.

Use a loop to look through POST variables

I'm trying to make a POST method that will receive a value from a table (that is dynamically generated). This value will be equal to a company name, and a hidden field will be there that is equal to company name + "id" appended to it.
Here's my code:
if(isset($_POST))
{
foreach ( $users as $balance_user ) {
if(isset($_POST[$balance_user]))
{
//update user meta with new balance
$newBalance = $_POST[$balance_user];
$postedID = $_POST[$balance_user.'id'];
update_user_meta($postedID, 'balance', $newBalance);
}
}
}
I keep getting the error Illegal offset type in isset or empty. Can I not pass variables in that way? For example if a company is called Acme, and that particularly named input has a value in it, I want to loop through all of the companies in the POST method, and if that part of the loop equals the company passed in the variable, it should do something.
Add these three lines to see the data, as others have indicated, clearly you are assuming some value is in $balance_user which is not there, or is different.
echo '<pre>:';
var_dump($balance_user);
echo ':</pre>';
if(isset($_POST[$balance_user]))
The pre makes it easier to read the debugging output. the :..: will show null values.
Once you run that, you will probably discover that one of your entries in $users is empty.
The output order will show you where that empty user value is.
However:
$postedID = $_POST[$balance_user.'id'];
That could be the error source as well, is there a post value that is. say, $balance_user == fred
fredid
if there isn't, of course you will instantly get that error. You aren't giving the line number of the error so I can't tell which it is, the line number will show it instantly.

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[]"

Is it better to set an object value in a function, or return a value from a function?

Lets say I have an array of data, and I want to process some function that will add or amend data in this array. e.g.
$my_data = array(
'val1' => 'my value 1',
'val2' => 'my value 2'
);
Traditionally I would pass the array or value to a function and return some data, e.g.
$my_data['val1'] = $this->get_new_var($my_data['val1']);
Well I just started doing something different which I think is better... I create the array within the $this object, like so (I am using Expression Engine):-
$this->EE->my_data = $my_data;
Then I just call a set method (rather than get) which does not need to return a value, because it sets the value within the function, e.g.
$this->set_new_var();
And within the set_new_var() function I can call the $this->EE->my_data array and manipulate it and set it as follows:
$this->EE->my_data['val1'] = $my_new_var;
So this seems to me to be cleaner, no need to pass variables to and from functions.
It also means I can set multiple values within the set function if need be, instead of returning one value from a function (or passing the entire the array and returning that).
So my question is, whether this set() method would be considered better, or should I stick to a traditional get() method.
Thanks
In my own opinion, a set() method allows me to handle the input in my own way and operate upon it, i.e. I can sanitize the input according to my use case and only pass on clean filtered input values to my function. Moreover, the set() method can be used to do more than just setting the variable. For example, in a particular use case, I may want to run a particular method or a process or even a delayed job, whenever a new value is set for a given variable (OOP). The set() method can be used to do exactly that, everytime. No need to repeat all your statements everytime such a behaviour is expected.
But, this is my own opinion.

Categories