What generates the class required by the owner in SilverStripe forms

I am creating a contact form in SilverStripe.

When checking validation, if I leave the required fields blank and click submit, a class will be added to these input fields .holder-required. Even if I reload the page, they will not disappear. (in fact, error messages *** is requiredwill remain there after a reboot). I just stopped messages from showing).

I searched the entire project folder, but there is no file there that even exists holder-required.

Where did the class come from .holder-required?

+4
source share
1 answer

, holder-required, , SilverStripe, , .

a FormField , "extraClass" , .

FormField:

public function extraClass() {
    $classes = array();

    $classes[] = $this->Type();

    if($this->extraClasses) {
        $classes = array_merge(
            $classes,
            array_values($this->extraClasses)
        );
    }

    if(!$this->Title()) {
        $classes[] = 'nolabel';
    }

    // Allow custom styling of any element in the container based on validation errors,
    // e.g. red borders on input tags.
    //
    // CSS class needs to be different from the one rendered through {@link FieldHolder()}.
    if($this->Message()) {
        $classes[] .= 'holder-' . $this->MessageType();
    }

    return implode(' ', $classes);
}

, , , holder-{Whatever_Your_Message_Type_Is} .


, $this->Message() - ​​ , , .

Form, FormField::setError(), .

public function setupFormErrors() {
    $errorInfo = Session::get("FormInfo.{$this->FormName()}");

    if(isset($errorInfo['errors']) && is_array($errorInfo['errors'])) {
        foreach($errorInfo['errors'] as $error) {
            $field = $this->fields->dataFieldByName($error['fieldName']);

            if(!$field) {
                $errorInfo['message'] = $error['message'];
                $errorInfo['type'] = $error['messageType'];
            } else {
                $field->setError($error['message'], $error['messageType']);
            }
        }

        // load data in from previous submission upon error
        if(isset($errorInfo['data'])) $this->loadDataFrom($errorInfo['data']);
    }

    if(isset($errorInfo['message']) && isset($errorInfo['type'])) {
        $this->setMessage($errorInfo['message'], $errorInfo['type']);
    }

    return $this;
}

Form code, , . : clearMessage resetValidation.

clearMessage forTemplate. resetValidation SilverStripe CMS Framework.

, , .

+5

Source: https://habr.com/ru/post/1607360/


All Articles