zauberfisch / silverstripe-better-form-validator
为 SilverStripe 提供简单的自定义表单验证
dev-master
2016-09-14 12:05 UTC
Requires
- silverstripe/framework: >=3.1
This package is auto-updated.
Last update: 2024-09-04 08:04:03 UTC
README
正在进行中
此模块仍在开发中。此模块的存在是因为 SilverStripe 缺少一些功能,可能在其API更改或一旦 SilverStripe 提供了类似的功能,可能会变得完全过时。
安装
安装模块
- 使用 composer 安装模块或下载并放置在您的项目文件夹中
- 运行
?flush=1
用法
$fields = new FieldList([ new TextField('Name', _t('MyForm.Name', 'Name')), new CheckboxField('CallMe', _t('MyForm.CallMe', 'Do you want us to call you on your phone number?')), new TextField('Phone', _t('MyForm.Phone', 'Phone')), new EmailField('Email', _t('MyForm.Email', 'Email')), new TextareaField('Message', _t('MyForm.Message', 'Message')), ]); $actions = new FieldList([ new FormAction('SubmitMyForm', _t('MyForm.Submit', 'Submit')), ]); $validateCallback = function ($data, Form $form, LambdaValidator $validator) { // using $valid is currently optional because a form is automatically // considered invalid if there are any error messages $valid = true; if (isset($data['CallMe']) && $data['CallMe']) { // if the call me checkbox is ticked, then a phone number is required if (!$validator->requireField('Phone')) { $valid = false; } // requireField will automatically add an error message to the form and set it as invalid } if (isset($data['Message'])) { // Message is automatically required, but this code will still run even if its not set // so we have to check that it's set if (stripos($data['Message'], 'viagra') !== false && stripos($data['Message'], 'cheap')) { $valid = false; $validator->validationError( // fieldName 'Message', // message _t( 'MyForm.NoViagra', 'Sorry, but we are currently not looking for cheap Viagra' ), // message type and css class 'required' ); } } return $valid; }; $validator = new LambdaValidator( // Parameter 1 - php callable to perform custom validation $validateCallback, // Parameter 2 - boolean or array|FieldList of fields to validate // In this case boolean true to automatically validate all fields // Validate means check for correct value for a type, eg EmailField // is valid if its empty or if a valid email address is entered true, // Parameter 3 - boolean or array|FieldList of fields that are required (field can not be empty) ['Name', 'Email', 'Message'] ); // the above code can be in your own Form subclass or used in combination with the line below $form = new Form($controller, $name, $fields, $actions, $validator);
许可
Copyright (c) 2015, Zauberfisch
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name Zauberfisch nor the names of other contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.