the-a-train
the-a-train

Reputation: 1143

silverstripe custom form field error message

In my form processing function I have:

$form->addErrorMessage('Email', 'Your email address is already registered in our system','bad');

and in my template:

$Fields.FieldByName(Email)

I can see that the bad class has been added to the input, but how do I display an inline error message?

Upvotes: 1

Views: 1015

Answers (2)

StefGuev
StefGuev

Reputation: 649

You have to only add bellow in your $form->addErrorMessage('Email', 'Your email address is already registered in our system','bad');

return $this->redirectBack();

I'm using this on all my forms. Be sure to use this inside a Submit function. You will be redirected to your form with the error message below the field Email. You dont need to create a custom template for this.

Upvotes: 1

mwiegboldt
mwiegboldt

Reputation: 2760

I'm not sure if this is what you are looking for, but I do something like this:

public function MyFormAction(){
    //do something
    if(//something is wrong){
        $this->MyErrorMessage = 'Something is wrong';
        return $this->renderWith(array('MyFormPage', 'Page'));
    }
}

And then in the template I can put where I want:

<% if $MyErrorMessage %>
<p class='bad'>$MyErrorMessage</p>
<% end_if %>

Upvotes: 1

Related Questions