Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[solved] Showing Form Validation Messages above the form (instead of inline)


Go to End


7 Posts   5275 Views

Avatar
Silverfish

Community Member, 59 Posts

24 January 2012 at 5:11am

Edited: 24/01/2012 5:11am

Hi There,

I would like to have a form that displays all validation messages in one box above the form instead of every message inline under their form-field.
I saw that silverstripe even generates a

<p id="Form_TestForm_error" class="message " style=""></p>

paragraph at top of the form, so I wonder if there's a (simple) way to display messages there?

Thanks for any hint!

Regards
S.F.

Avatar
Devlin

Community Member, 344 Posts

24 January 2012 at 9:26am

Edited: 24/01/2012 10:29am

$form->sessionMessage('hello world', 'good');

Edit: If you're thinking of disabling js validation, otherwise you'll have some js coding to do, you can get all validation errors from $form->validator->getErrors().

Avatar
Silverfish

Community Member, 59 Posts

25 January 2012 at 12:55am

Thx Devlin 4 your answer.

Ok, so I know how to add a message to the form. Now have to look for a way to only add it if validation of the form failed and to get all the (custom?) error messages (from requiredFields?) in there.

I am searching the docs and the net for hors for something like

if ( $form->is_valid() ) {
foreach($form->RequriedFields() as $no => $field) {
messages[] = $field->errorMessage;
}

without any results.

Seems like i have to do the whole validation myself / customized just to get the errore messages somewhere else?

Still grateful for any hints :)

Regads
S.F.

Avatar
Devlin

Community Member, 344 Posts

25 January 2012 at 1:41am

Edited: 25/01/2012 2:29am

The form won't call the submit method unless all required fields are correct validated. To do what you want the 'simple way', you can just parse the error messages in your page controller and call it via your template.

class FormPage extends Page{
	function showFormErrors() {
		$errors = Session::get('FormInfo.FormName.errors');
		$_errors = array();
		if( !empty($errors) && is_array($errors) ) foreach($errors AS $error) {
			$_errors[] = $error['message'];
		}
		Session::clear('FormInfo.FormName.errors');
		return !empty($_errors) ? "<ul><li>".implode("</li><li>", $_errors)."</li></ul>" : false;
	}
}

Or if have a form subclass you can add your own validation method.

class MyForm extends Form{
	function validate() {
		parent::validate();

		if ($this->validator) {
			$errors = $this->validator->getErrors();
			$data = $this->getData();

			// edit
			$_errors = array();
			if( !empty($errors) && is_array($errors) ) foreach($errors AS $error) {
				$_errors[] = $error['message'];
			}

			if( !empty($_errors) ) {
				$this->sessionMessage(implode(', ', $_errors), 'bad');
				return false;
			}
		}
		return true;
	}
}

* Code not tested

Avatar
Silverfish

Community Member, 59 Posts

25 January 2012 at 4:18am

Hi Delvin,

since I also have to do some custom validation I chose the 2nd, subclassing method. Works perfect!

Thanks a lot for saving the rest of my Day! :)

Regards
SF

Avatar
geekdenz

Community Member, 37 Posts

18 April 2016 at 10:47am

Hi There,

I am writing a module for SilverStripe that makes a SilverStripe site a Single Page App (SPA). We have this requirement for a web application we are writing, because it is very client side heavy and has a geo app that requires saving state on the client between page changes.

So far so good, it is using AngularJS and TypeScript and routing, the app etc works fine. However, I'm having a problem with Forms. They render fine into the template and get loaded via AJAX and everything, including submission, security check and validation over AJAX etc works.

However, I cannot get the form to display with error messages when the validation fails.

In my naivety I just triied

$form->forTemplate()

but that gives me the Form without errors displayed, even though I ran
$form->validate()

I can post some more code if required, but I hope you can understand the problem from this.

Any help much appreciated!
Tim

Avatar
geekdenz

Community Member, 37 Posts

18 April 2016 at 1:41pm

This seems to do what I wanted:

if ($errors) {
			$fields = $form->Fields();
			foreach ($errors as $error) {
				$field = $fields->fieldByName($error['fieldName']);
				$field->setError($error['message'], $error['messageType']);
			}
			// ...
}

Maybe this will help someone.