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

Form Validation


Go to End


2 Posts   1856 Views

Avatar
FredRF

Community Member, 3 Posts

19 April 2016 at 3:47am

Edited: 19/04/2016 3:48am

Hi,

I am fairly new silverstripe and I need to validate a form depending on the state of another form.
My first form is a shopping basket with 2 delivery option( 'free' or 'not free').
My second form is a set of delivery address details. Some of the fields are for the 'free' option. The other fields are for the 'not free' option. I can change the visibility of the fields using Jquery. That seems to be working.

My issue comes when validating the form. How can I tell silverstripe which fields are compulsory and which are not?

Currently I have:


            new FieldList(
                $TradeName = new TextField('Name', 'Name'),
                $TradeCompanyName = new TextField('CompanyName', 'Company Name'),
                $TradeEmail = new EmailField('Email', 'Email Address'),
                $TradePhone = new TextField('Phone', 'Telephone Number'),
                $TradeAddress1 = new TextField('Address1', 'Delivery Address Line 1'),
                $TradeAddress2 = new TextField('Address2', 'Delivery Address Line 2'),
                $TradeCity = new TextField('City', 'Delivery Town/City'),
                $TradePostcode = new TextField('Postcode', 'Delivery Postcode'),
                $TradeNotes = new TextareaField('Notes', 'Order Notes'),

		$CustomerName = new TextField('CustomerName', 'Customer Name'),
		$CustomerEmail = new EmailField('CustomerEmail', 'Email Address'),
		$CustomerPhone = new TextField('CustomerPhone', 'Telephone Number'),
                $CustomerAddress1 = new TextField('CustomerAddress1', 'Delivery Address Line 1'),
                $CustomerAddress2 = new TextField('CustomerAddress2', 'Delivery Address Line 2'),
                $CustomerCity = new TextField('CustomerCity', 'Delivery City'),
                $CustomerPostcode = new TextField('CustomerPostcode', 'Delivery Postcode'),
                $CustomerNotes = new TextField('CustomerNotes', 'Order Notes'),


            new FormValidator(array(
                'Name' => array('required'), 
                'Email' => array('required'),
                'Phone' => array('required'),
                'Address1' => array('required'),
                'City' => array('required'),
                'Postcode' => array('required'),
                 'CustomerName' => array('required'), 
                'CustomerEmail' => array('required'),
                'CustomerPhone' => array('required'),
                'CustomerAddress1' => array('required'),
                'CustomerCity' => array('required'),
                'CustomerPostcode' => array('required')
            ))

but I must have to add something as this depends on the delivery option. just don't know what it is...

As an laternative to Silverstripe validation, I am thinking of using jquery validation [url= https://jqueryvalidation.org/] [url]https://jqueryvalidation.org/ . Would that be better?

Avatar
ZarockNZ

Community Member, 17 Posts

1 August 2016 at 4:31pm

Hi,

Take a look a the Zen Validator module. https://github.com/sheadawson/silverstripe-zenvalidator. In my opinion this is the best validator module for SilverStripe.

In particular the "Validation Logic - Conditional Constraints" near the end of the readme which mentions using "Display Logic" like conditions. You could use this to only require certain fields, such as when the delivery option is free or not free.

In your function which creates the form fields...


$TradeAddress1->validateIf('DeliveryOption')->isEqualTo('free');
$TradeAddress2->validateIf('DeliveryOption')->isEqualTo('free');

$CustomerAddress1->validateIf('DeliveryOption')->isEqualTo('not-free');
$CustomerAddress2->validateIf('DeliveryOption')->isEqualTo('not-free');

Then create this function below the one which creates the form fields...


public function getCMSValidator()
{
    $validator = ZenValidator::create();
    
    $validator->addRequiredFields(
        array(
            'TradeAddress1',
            'TradeAddress2',
            'CustomerAddress1',
            'CustomerAddress2',
        )
    );

    return $validator;
}