Hello, I am trying to validate a form in Drupal. I have tried the following 2 codes and they do not work for me. Could you help me with it? Thanks in advance !!!

I did not put any validation conditions to make the code less complicated and find the problem.

1.   

<?php

    use \Drupal\Core\Form\FormStateInterface;

    function validations_form_alter($form, &$form_state, $form_id){
        $form['#validate'][] = "validate";
    }
    
    function validate(&$form, &$form_state){
        $form_state->setErrorByName('Error',t('Error'));
    }

2.

<?php

    use \Drupal\Core\Form\FormStateInterface;

    function validations_form_validate($form, &$form_state){
        $form_set_error('Error',t('Error'));
    }

Comments

wombatbuddy’s picture

The first way should work if you add "&" symbol before $form, like this: 

function validations_form_alter(&$form, $form_state, $form_id){}

Also, the first parameter of the setErrorByName() is the name of the form element.

Lazaro Cruz’s picture

Thank you very much, yes it already works when making the corrections that you tell me, but what would I do if what I want is to validate that no more content of a specific type is inserted? In that case I cannot associate the error to any field.

This is what I try to do:

<?php

    function validations_form_alter(&$form, &$form_state, $form_id){
        if ($form_id == "node_logo_form") {
            $node = Drupal::entityQuery('node')
                  ->condition('type','logo')
                  ->execute();

            if(count($node) > 0){
                $form['#validate'][] = "restricted_creation";
            }	
        }
    }
	
    function restricted_creation(&$form, &$form_state){
		
        /* SHOW ERROR */
	   
    }
wombatbuddy’s picture

The use case is not clear, could you provide more info and also share the implementation of the validation function. 
You can use FormState::setError() instead and maybe, in your case it makes sense to pass the $form as the parameter. See the examples.

Lazaro Cruz’s picture

Yes, it's exactly what you were looking for, but it only prints the error if there is another one. if it's the only error then it ends up submitting the form when it shouldn't.

The idea is that it is not allowed to insert more than one content of type "logo". This is the code after the corrections:

<?php

    function validations_form_alter(&$form, &$form_state, $form_id){
	if($form_id == "node-logo-form"){
	    $form['#validate'][] = "restricted_logo_creation";
	} 	
    }
    
    function restricted_logo_creation(&$form, &$form_state){
        $node = Drupal::entityQuery('node')
              ->condition('type','logo')
              ->execute();

	if(count($node) > 0){
	    $form_state->setError($form,
            'Only one record of this content is allowed.');
	}		
    }
wombatbuddy’s picture

Your use case is still not clear for me. You can try to provide more extended explanation (what is the form you are talking about, what fields/elements does it have, where and how do you insert content of type "logo"?). If we understand the use case, we will try to help

Lazaro Cruz’s picture

Thank you very much but I already found the problem. The  $form_id is returning "node-logo-form--4cHWyNinmi0" and so $form_id == "node-logo-form" is returning false. Now the question is why does it contain that code behind that is always different?

I already tried and it happens to me only with that content type.

Lazaro Cruz’s picture

I already got what I needed, thank you very much for your help, it was very useful.

Here is the final code:

<?php

    function validations_form_alter(&$form, &$form_state, $form_id){
        $form['#validate'][] = 'validations'; 
    }

    function validations(&$form, &$form_state) {
        
        if($form['#form_id'] == 'node_logo_form'){
	   validate_restricted_creation('logo', $form, $form_state);
        } 
		
        if($form['#form_id'] == 'node_about_form'){
	   validate_restricted_creation('about', $form, $form_state);
        } 
		
        if($form['#form_id'] == 'node_map_form'){
	   validate_restricted_creation('map', $form, $form_state);
        } 
		
    }

    function validate_restricted_creation($type, &$form, &$form_state){
        $node = Drupal::entityQuery('node')
              ->condition('type',$type)
              ->execute();

        if(count($node) > 0){
            $form_state->setError($form,t('Only one record of this content is allowed.'));
        } 		
    }