Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>SilverStripe forms are more complex than one would believe at first glance. </p> <p>you are right, the validator is checked before submitting, and only if the validator returns true the form action will be called. (also note that the form action is never exposed as URL, the URL is always the form it self, which then calls the form action.)</p> <p>on validation error, errors are set (into session) and the form redirects back. then the errors are fetched from the session.</p> <p>silverstripe forms are built with custom validation in mind. There is a class called Validator, everything that extends this class can be used to validate a form. (RequiredFields is a subclass of Validator). so you can built your own validator this way. But if you are looking for common validation cases the awesome NetefxValidator module might be what you are looking for: <a href="https://github.com/lx-berlin/NetefxValidator" rel="nofollow">https://github.com/lx-berlin/NetefxValidator</a></p> <p>it provides many rules to validate your form, for example:</p> <ul> <li>less then</li> <li>greater than</li> <li>text equals</li> <li>text contains</li> <li>regex</li> <li>...</li> </ul> <p>it can also combine rules with <code>and</code>, <code>or</code> and <code>xor</code> (full list at <a href="https://github.com/lx-berlin/NetefxValidator/tree/master/code/rules" rel="nofollow">https://github.com/lx-berlin/NetefxValidator/tree/master/code/rules</a>) it even lets you specify a custom php function to validate, like so:</p> <pre><code># SilverStripe 3.x (but with some slight modifications it should also work in 2.x) $fields = FieldList::create(array( TextField::create('myField', 'some label') )); $actions = FieldList::create(array( FormAction::create('doSend', 'submit'), )); $myValidationFunction = function ($data, $args) { if ($data['myField'] == 'zauberfisch is awesome' &amp;&amp; $args['someOtherThing'] == 'yay') { // only allow this form to pass if the user acknowledges that zauberfisch is awesome return true; } else { return false; } }; $additionalArguments = array('something' =&gt; 'ohai', 'someOtherThing' =&gt; 'yay'); $rule = NetefxValidatorRuleFUNCTION::create( $fieldName = 'myField', $errorMessage = 'to bad, you did not fill this field correctly', null, array($myValidationFunction, $additionalArguments) ); $validator = NetefxValidator::create(array($rule)); $form = Form::create($this, $fields, $actions, $validator); </code></pre> <p>NOTE: <code>Form::create()</code> is basicly the same as <code>new Form()</code></p> <hr> <p><strong>about the idea that you want to set an error message in the form action: <em>BAD</em> idea, you will run into countless troubles trying to do this. the validator class exists for a reason, don't try to abuse something else to do what the validator should do</strong></p> <hr> <p>EDIT: to answer your question more clearly:</p> <p>no, it is no possible to set your own error message, you need to use a custom validator for this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload