1778 Posts in 581 Topics by 555 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 712 Views |
-
[solved] Showing Form Validation Messages above the form (instead of inline)

24 January 2012 at 5:11am Last edited: 24 January 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. -
Re: [solved] Showing Form Validation Messages above the form (instead of inline)

24 January 2012 at 9:26am Last edited: 24 January 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().
-
Re: [solved] Showing Form Validation Messages above the form (instead of inline)

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. -
Re: [solved] Showing Form Validation Messages above the form (instead of inline)

25 January 2012 at 1:41am Last edited: 25 January 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
-
Re: [solved] Showing Form Validation Messages above the form (instead of inline)

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
| 712 Views | ||
|
Page:
1
|
Go to Top |


