`form_set_error` 출력의 위치를 ​​어떻게 변경합니까?


9

Drupal 7에서 출력 위치를 변경하는 방법이 form_set_error있습니까?

현재는 drupal_set_message모든 양식의 오류를 화면 상단에 대기 시키는 호출합니다 .

대신 각 메시지가 해당 필드 아래에 표시됩니다.

이것이 가능하지 않은 경우 MODULE_form_name_validate()함수 사용 하지 않고 함수 내에서 양식을 "잘못된"것으로 수동으로 플래그 지정할 수 form_set_error있습니까?

답변:


7

인라인 양식 오류 모듈은 그 기능이 있습니다 :

IFE 또는 인라인 양식 오류를 사용하면 양식 제출 오류를 양식 요소와 함께 배치 할 수 있습니다. 인라인 오류 동작을 설정하기위한 세 가지 옵션이 제공됩니다. 기본 동작을 구성하거나 양식별로 동작을 재정의 할 수 있습니다. 원하는만큼 양식을 추가 할 수 있습니다.

Drupal 7 릴리스는 알파 버전이지만 시도해 볼 가치가 있다고 말합니다. 문제가 있더라도 자신의 버전을 구현하기에 좋은 장소가 될 것입니다. 다음은 모듈 스크린 샷입니다.

여기에 이미지 설명을 입력하십시오


이 모듈은 정말 오래되었습니다. 나는 그것을 테스트했지만 사용자 정의 측면에서 너무 나쁘다. 슬프게도 쓸모없는 양식은 이러한 클릭으로 만든 양식을 만들지 않고 이미 만들었습니다.
kwoxer 2016 년

8

Clive의 (올바른) 답변을 확장하면서 IFE 코드를 살펴 보았습니다. 나는 이것에 전념하는 전체 모듈이 실제로 필요하지 않았으므로 여기에 몇 가지 스 니펫을 채택하여 필요한 결과를 얻었습니다. 나는 그것이 정답이라고 결론을 내리기 때문에 그의 대답을 올바른 것으로 표시했다.

코드는 다음과 같습니다. 모든 크레딧은 Clive와 IFE 팀에 전달됩니다. 비슷한 답변을 원하는 사람을 위해 간단한 버전을 제시하고 싶었습니다.

// Standard gear - do some custom validation and set the errors
// as you go..
// 
// Once all the validation has been done, call MODULE_errors_reset
// which will return an array of all errors against their ID. 
// Expose this array however you like to your template, or loop
// over your form adding a #suffix to each element with an error
//
function MODULE_form_name_validate($form, &$form_state) {
    drupal_set_message("validating...");

    form_set_error("description", "There is an error here!!!!");
    form_set_error("tags", "Yep, and here too!!!");

    $reset_errors = MODULE_errors_reset( $form );

    drupal_set_message( "<pre>" . print_r( $reset_errors, true ) . "</pre>" );
}

// This part is adopted from IFE. It's changed in two ways, it returns
// an array (which also merges with its recursive self). 
// And it also skips all the 'display' stuff present in the original
// Essentially it extracts out the error messages from the session and unsets 
// them. I am assuming that Drupal 7 marks the success of a validation based not
// whether the SESSION variable contains anything, the SESSION seems to be only
// for the message at the top of the screen.
//
function MODULE_errors_reset( $element ) {
    if( ! isset( $_SESSION[ 'messages' ] ) ) {
        return;
    }

    $reset_errors = array();

    // Recurse through all children.
    foreach( element_children( $element ) as $key ) {
        if( isset( $element[ $key ] ) && $element[ $key ] ) {
            $reset_errors += MODULE_errors_reset( $element[ $key ] );
        }
    }

    // Check for errors and settings
    $errors = form_get_errors();
    $element_id = implode( '][', $element[ '#parents' ] );

    if ( !empty( $errors[ $element_id ] )) {
        $error_message = $errors[ $element_id ];

        // Get error id
        $error_id = array_search( $error_message, $_SESSION[ 'messages' ][ 'error' ] );

        if( $error_id !== FALSE ) {
            unset( $_SESSION[ 'messages' ][ 'error' ][ $error_id ] );
            $_SESSION[ 'messages' ][ 'error' ] = array_values( $_SESSION[ 'messages' ][ 'error' ]  );

            if( count( $_SESSION[ 'messages' ][ 'error' ] ) <= 0 ) {
                unset( $_SESSION[ 'messages' ][ 'error' ] );
            }

            $reset_errors[ $element[ '#id' ] ] = $error_message;
        }
    }

    return $reset_errors;
}

// If there are no form errors, we still hit here, even after the 'reset', this is
// a good thing. 
function MODULE_form_name_submit( $form, &$form_submit ) {
    drupal_set_message("submited!");
}

안녕하세요 크리스, "이 배열을 템플릿에 표시하거나 오류가있는 각 요소에 # 접미사를 추가하여 양식을 반복하십시오"라고 말하면 validate 양식에 반환 된 $ reset_errors 변수에 어떻게 액세스 할 수 있습니까? 함수? 그것에 대한 샘플 디스플레이를 제공해도 괜찮습니까? 감사!
Leolando Tan

@LeolandoTan 죄송합니다-2013 년부터 Drupal을 사용하지 않았습니다-이 질문에 대한 답변도 기억할 수 없습니다!
Chris
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.