0

I have tried many things to set the message field as not required.

1-The more obvious in form_alter

$form['message']['widget'][0]['value']['#required']=false;
$form['message']['widget'][0]['#required']=false;
$form['message']['widget']['#required']=false;
$form['message']['#required']=false;

2-Set a white space as a value (if field is empty) in form_validate

$msg= $form_state->getValue('message');
if(!isset($msg['value'])){
    $form_state->setValue('message', 
        [
            'value' => '  '
        ]
    );
}

3-Set not required in after build

function alter_contact_ds(&$form, FormStateInterface $form_state, $form_id)
{
    $form['#after_build'][] = '_remove_msg_required';
    ...
}
function _remove_msg_required($element){
    $element['message']['widget'][0]['value']['#required']=false;
    $element['message']['widget'][0]['#required']=false;
    $element['message']['widget']['#required']=false;
    $element['message']['#required']=false;
    return $element;
}

4- In form_alter, I have used the #state to set the required property

$form['message']['#states'] = ['required' => ['select[id="edit-object"]' => [
    'value' => 'info'
]]];
//Note that ['select[id="edit-object"]' => ['value' => 'info']]]
//is used succesfully on another field (but not the requied part)

The best I could get is not having the regular behavior explaining that the field must be completed (and no red asterix near the field to mark it as required)

But each time I press on the submit button, I get a drupal_set_message("This value could not be null",error) [freely translated from french]

SOLUTION:

Following the idea of Alireza Tabatabaeian, I could solve this problem by

1-Set the field as not required in form alter

$form['message']['widget'][0]['value']['#required'] = false;

2-Set my validation function de first one in form_alter

$form['#validate'] = ['my_validate_contact','::validateForm'];

3-In my validate function set the value of the field equal to a space if the field is empty (it is not really nice, but I could accept it)

$msg = $form_state->getValue('message');
if (! isset($msg['value'])) {
    $form_state->setValue('message', [
        [
            'value' => '  '
        ]
    ]);
}

2 Answers 2

2

I've solved this issue this way:

function hook_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'contact_message') {
    if (isset($fields['message']) && $fields['message'] instanceof BaseFieldDefinition) {
      /* @var BaseFieldDefinition $field */
      $field = &$fields['message'];
      $field->setRequired(FALSE);
    }
  }
}
1
  • This is the way to go!!!
    – Baud
    Aug 2, 2019 at 8:38
0

As many form in drupal are manageable in drupal (like node forms, user registration form, webforms and ...) the first attempt to alter form can be took using UI.

Hence in some cases the form cant be altered using UI and in this situation we need to use hook_form_FORM_ID_alter.

to make a field required or non-required two different steps should be took

one step is to remove or add client side verification, this can be done using form #states property, a sample code could be :

$form['ELEMENT']['#states'] = ['required' => [Array of conditions]];

see some examples here

as this element is required by another module there must be a validation function which wont allow submission of empty value, so the next step is to add our custom validation function to form in a manner that our function being called before any other validation function, in that function a non empty value can be assigned to element, so the codes can be something like below :

in hook_form_alter :

$form['#validate'] = ['my_validate_contact','::validateForm'];

and in validation function :

$msg = $form_state->getValue('message');
if (! isset($msg['value'])) {
  $form_state->setValue('message', [
    [
        'value' => '  '
    ]
  ]);
}
7
  • Thank you for your help, but as you can see in my updated question, I couldn't succeed wuth the #state trick
    – Baud
    Jul 24, 2018 at 22:22
  • sorry my bad, it is not #state, it must be #states, and that should work by that Jul 24, 2018 at 22:42
  • When using #states, it is removing the red star next the field (to show that the field is required), but clicking the submit button fires the regular error: "Please fill out this field"
    – Baud
    Jul 24, 2018 at 23:40
  • In this case you will need to add a new verification function in the zero index of form verification array, there set a default value for the field Jul 24, 2018 at 23:43
  • If you mean 'form_validation', I already tried this (see point 2)...
    – Baud
    Jul 25, 2018 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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