GiarcTNA
GiarcTNA

Reputation: 499

Silverstripe - return form data after validation

I am trying to do server side validation with custom validation for the phone and email fields. I am doing the custom validation in the forms action.

Firstly is this the correct place to do it and secondly if so how can I get the data to return to the form if it doesn't meet validation?

Currently it will clear the entire form.

public function doSubmitForm($data, Form $form) {

        if (!preg_match("/^[\+_a-z0-9-'&=]+(\.[\+_a-z0-9-']+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i",$data['Email'])) {
            $form->addErrorMessage('Email', 'Invalid email', 'bad');
        return $this->redirectBack();
        }
        if (!preg_match("/^((?:\+)|0)(\d{9,14})$/i",$data['Phone'])) {
            $form->addErrorMessage('Phone', 'Please match the correct format eg: 0821234567', 'bad');
        return $this->redirectBack();
        }

        $form->sessionMessage('Thank you for your submission','alert alert-success');

    return $this->redirectBack();   
    }

Upvotes: 1

Views: 327

Answers (2)

Colin Burns
Colin Burns

Reputation: 65

I had to implement a 10 characters length for phone numbers the other day.

https://github.com/sheadawson/silverstripe-zenvalidator

I included the module above via composer and followed the set up in the README.

For the admin interface I created a getCMSValidator() method

public function getCMSValidator() {
    $validator = ZenValidator::create();
    $validator->setConstraint('Phone', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
    $validator->setConstraint('PhoneAH', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
    $validator->setConstraint('PhoneMobile', Constraint_length::create('range', 10, 10)->setMessage('Mobile numbers must be 10 digits in length'));
    $validator->disableParsley();
    return $validator;
}

For the front end just create a $validator and then add it to the form

$validator = ZenValidator::create();
$validator->setConstraint('Phone', Constraint_length::create('range', 10, 10)->setMessage('Phone numbers must be 10 digits in length'));
$validator->disableParsley();

$Form = new Form($this, 'FormName', $fields, $actions, $validator);

Took me about 20 minutes to implement that minimum 10 & maximum 10 characters on about 5 or 6 different forms.

I hope this helps.

Cheers, Colin

Upvotes: 1

bummzack
bummzack

Reputation: 5875

I suggest you don't do server-side validation like that. The easiest way is just to use the proper form-fields. Eg. EmailField and PhoneNumberField.

If these don't validate the way you want, just extend them or create your own FormField subclasses.

Here's how EmailField does it's validation: https://github.com/silverstripe/silverstripe-framework/blob/3.5/forms/EmailField.php#L39

Alternatively you could also implement a custom validator by extending Validator or RequiredFields. The validator will be applied to the whole form though and if you start validating individual field types there, you'd be better off just implementing the field as custom class (that way you have a re-usable component).

Upvotes: 2

Related Questions