1

Consider for a content type, there is a text field with multiple values capability activated.

  1. How can I check its values by $form_state inside the Validate/Submit function?
  2. How can I set its values by $form_state inside the Validate/Submit function?

The single code line for each of them would be enough for me to see, Thanks.

Further discussion:

I need to set/check the MULTI-VALUE fields' values. It's related to the DELTA value and ... but I don't know how clearly I can do it!

3
  • I'm voting to close this question as off-topic because it is about writing code, but the OP is not showing any code they are actually using.
    – apaderno
    Apr 26 '15 at 17:07
  • Thanks for your attention, but I have answered it already and I would like to keep it alive for learning purposes.
    – Ehsan
    Apr 27 '15 at 9:44
  • Questions about code for which no code is shown gets closed.
    – apaderno
    Apr 27 '15 at 14:46
1

Clearly, for any multi-value field there is a delta value inside each fields' table in Drupal Database, which corresponds to the index of the value for that entity instance.

According to this knowledge, the structure of the form_state variable for a multi-value field with two values (e.g. X and Y) is:

[field_FIELD_NAME] => Array
                (
                    [und] => Array
                        (
                            [0] => Array
                                (
                                    [value] => X
                                    [_weight] => 0
                                )
                            [1] => Array
                                (
                                    [value] => Y
                                    [_weight] => 0
                                )

                            [add_more] => Add another item
                        )

                )

[0] and [1] are delta values for each value of this filed in the corresponding entity instance. For setting the values of a multi-value filed in FORM submit/validate hook, you should do as follows (consider again we have two values X and Y):

$form_state['values']['field_FIELD_NAME']['und'][0]=array("value"=>'X',"_weight"=>0);
$form_state['values']['field_FIELD_NAME']['und'][1]=array("value"=>'Y',"_weight"=>1);

The above lines ought to be written inside the VALIDATE hook.

For reading values for validation, do as follows:

$values=$form_state['values']['field_FIELD_NAME']['und'];

foreach ($values as $key => $value) {
  if($value['value']...
}
0

You can do this with hook_node_validate() function.

function hook_node_validate($node, $form, &$form_state) {
  if ($node->type == '[your_content_type]') {
    // $node : Changed value of Node
    // $form_state['node'] : Original Values if Editing a node
  }
}
2
  • Thank you for your reply, but Multi-value fields are the point for my question.
    – Ehsan
    Nov 6 '14 at 15:45
  • 1
    Multi values field's values will be available in your node object. You can validate and run form_set_error if you want. Nov 6 '14 at 15:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.