本文整理汇总了PHP中RequiredFields::addRequiredField方法的典型用法代码示例。如果您正苦于以下问题:PHP RequiredFields::addRequiredField方法的具体用法?PHP RequiredFields::addRequiredField怎么用?PHP RequiredFields::addRequiredField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RequiredFields
的用法示例。
在下文中一共展示了RequiredFields::addRequiredField方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFrontEndFormValidator
public function getFrontEndFormValidator($flexi)
{
$validator = new RequiredFields();
foreach ($flexi->FlexiFormFields()->filter('Required', true) as $field) {
$validator->addRequiredField($field->SafeName());
}
return $validator;
}
示例2: FieldSet
static function get_edit_fields($extraFields = null)
{
$fields = new FieldSet(new TextField('FirstName', _t('Member.FIRSTNAME', 'First Name')), new TextField('Surname', _t('Member.SURNAME', 'Surname')), new EmailField('Email', _t('Member.EMAIL', 'Email')), new ConfirmedPasswordField('Password', _t('Member.db_Password', 'Password') . ' *'));
$requiredFields = new RequiredFields('FirstName', 'Surname', 'Email', 'Password');
if ($extraFields) {
foreach ($extraFields as $name => $title) {
$fields->push(new TextField($name, $title));
$requiredFields->addRequiredField($name);
}
}
return array($fields, $requiredFields);
}
示例3: __construct
public function __construct($controller, $product, $name = "AddItemForm")
{
$productID = $product ? $product->ID : 0;
$fields = FieldList::create(HiddenField::create('ProductID')->setValue($productID));
$actions = FieldList::create(FormAction::create('doAddItemToCart', _t('Commerce.AddToCart', 'Add to Cart'))->addExtraClass('btn'));
$requirements = new RequiredFields(array("Quantity"));
// If product colour customisations are set, add them to the item form
if ($product && $product->Customisations()->exists()) {
foreach ($product->Customisations() as $customisation) {
$field = $customisation->Field();
$fields->add($field);
// Check if field required
if ($customisation->Required) {
$requirements->addRequiredField($field->getName());
}
}
}
$quantity_fields = QuantityField::create('Quantity', _t('Commerce.CartQty', 'Qty'))->setValue('1')->addExtraClass('commerce-additem-quantity');
// Add quantity, so it appears at the end of the fields
$fields->add($quantity_fields);
parent::__construct($controller, $name, $fields, $actions, $requirements);
}
示例4: getRequiredFields
/**
* Get the required form fields for this form. Includes building the jQuery
* validate structure
*
* @return RequiredFields
*/
public function getRequiredFields()
{
$required = new RequiredFields();
$rules = array();
$validation = array();
$messages = array();
$onfocusout = "";
$hidelabels = "";
if ($this->Fields()) {
foreach ($this->Fields() as $field) {
if (!in_array($field->ClassName, array('EditableEmailField', 'EditableNumericField'))) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
}
if ($field->Required) {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
$required->addRequiredField($field->Name);
}
}
}
// Enable live validation
if ($this->EnableLiveValidation) {
$onfocusout = ", onfocusout : function(element) { this.element(element); }";
}
// Hide field labels (use HTML5 placeholder instead)
if ($this->HideFieldLabels) {
$hidelabels = '$("#Form_Form label.left").each(function(){$("#"+$(this).attr("for")).attr("placeholder",$(this).text());$(this).remove();});Placeholders.init();';
}
// Set the Form Name
$rules = $this->array2json($rules);
$messages = $this->array2json($messages);
// set the custom script for this form
Requirements::customScript(<<<JS
\t\t\t(function(\$) {
\t\t\t\t\$(document).ready(function() {
\t\t\t\t\t\$("#Form_Form").validate({
\t\t\t\t\t\tignore: ':hidden',
\t\t\t\t\t\terrorClass: "required",\t
\t\t\t\t\t\terrorPlacement: function(error, element) {
\t\t\t\t\t\t\tif(element.is(":radio")) {
\t\t\t\t\t\t\t\terror.insertAfter(element.closest("ul"));
\t\t\t\t\t\t\t} else {
\t\t\t\t\t\t\t\terror.insertAfter(element);
\t\t\t\t\t\t\t}
\t\t\t\t\t\t},
\t\t\t\t\t\tmessages:
\t\t\t\t\t\t\t{$messages}
\t\t\t\t\t\t,
\t\t\t\t\t\trules:
\t\t\t\t\t\t \t{$rules}
\t\t\t\t\t\t{$onfocusout}
\t\t\t\t\t});
\t\t\t\t\t{$hidelabels}
\t\t\t\t});
\t\t\t})(jQuery);
JS
, 'UserFormsValidation');
$this->extend('updateRequiredFields', $required);
return $required;
}
示例5: RenderForm
public function RenderForm()
{
if ($form_fields = $this->FormFields()) {
$fields = new FieldList();
if ($form_error = Session::get('FormError')) {
Session::set('FormError', false);
$fields->push(new LiteralField('form_error', '<p class="form-error">' . $form_error . '</p>'));
}
$validator = new RequiredFields();
$utils = new FormUtilities();
$fieldGroups = array();
foreach ($form_fields as $FieldName => $data) {
if ($data['Value'] && is_string($data['Value'])) {
$method_home = method_exists($this, $data['Value']) ? $this : (method_exists($utils, $data['Value']) ? $utils : false);
$data['Value'] = $method_home ? $method_home->{$data}['Value']() : $data['Value'];
}
$Label = $data['Group'] ? '' : ($data['Label'] ? $data['Label'] : FormField::name_to_label($FieldName));
$field = new $data['FieldType']($FieldName, $Label, $data['Value'] ? $data['Value'] : null, $data['Default'] ? $data['Default'] : null);
if ($data['ExtraClass']) {
$field->addExtraClass($data['ExtraClass']);
}
if ($data['Config'] && is_array($data['Config'])) {
foreach ($data['Config'] as $key => $value) {
$field->setConfig($key, $value);
}
}
if ($data['Required']) {
$validator->addRequiredField($FieldName);
$field->addExtraClass('required');
}
if ($data['Group']) {
if (!isset(${$data['Group']})) {
${$data['Group']} = new FieldGroup($data['Group']);
$fields->push(${$data['Group']});
${$data['Group']}->FieldCount = 0;
$fieldGroups[] = ${$data['Group']};
}
$field->setRightTitle($data['Label'] ? $data['Label'] : FormField::name_to_label($FieldName));
${$data['Group']}->push($field);
${$data['Group']}->FieldCount++;
} else {
$fields->push($field);
}
}
// update the class on the field groups to properly display the grouped fields horizontally
foreach ($fieldGroups as $fieldGroup) {
$fieldGroup->addExtraClass('stacked col' . $fieldGroup->FieldCount);
}
$submitText = "Submit";
if ($config = $this->FormConfig()) {
$submitText = $config['submitText'] ? $config['submitText'] : $submitText;
}
$actions = new FieldList(new FormAction('SubmitForm', $submitText));
$form = new Form($this, 'RenderForm', $fields, $actions, $validator);
if ($defaults = Session::get("FormInfo.Form_RenderForm.data")) {
$form->loadDataFrom($defaults);
Session::set("FormInfo.Form_RenderForm.data", false);
}
return $form;
}
return false;
}
示例6: getRequiredFields
/**
* Get the required form fields for this form. Includes building the jQuery
* validate structure
*
* @return RequiredFields
*/
public function getRequiredFields()
{
$required = new RequiredFields();
$rules = array();
$validation = array();
$messages = array();
if ($this->Fields()) {
foreach ($this->Fields() as $field) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
if ($field->Required && $field->CustomRules()->Count() == 0) {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
$required->addRequiredField($field->Name);
}
}
}
// Set the Form Name
$rules = $this->array2json($rules);
$messages = $this->array2json($messages);
// set the custom script for this form
Requirements::customScript(<<<JS
\t\t\t(function(\$) {
\t\t\t\t\$(document).ready(function() {
\t\t\t\t\t\$("#Form_Form").validate({
\t\t\t\t\t\tignore: [':hidden'],
\t\t\t\t\t\terrorClass: "required",\t
\t\t\t\t\t\terrorPlacement: function(error, element) {
\t\t\t\t\t\t\tif(element.is(":radio")) {
\t\t\t\t\t\t\t\terror.insertAfter(element.closest("ul"));
\t\t\t\t\t\t\t} else {
\t\t\t\t\t\t\t\terror.insertAfter(element);
\t\t\t\t\t\t\t}
\t\t\t\t\t\t},
\t\t\t\t\t\tmessages:
\t\t\t\t\t\t\t{$messages}
\t\t\t\t\t\t,
\t\t\t\t\t\trules:
\t\t\t\t\t\t \t{$rules}
\t\t\t\t\t});
\t\t\t\t});
\t\t\t})(jQuery);
JS
, 'UserFormsValidation');
$this->extend('updateRequiredFields', $required);
return $required;
}
示例7: getRequiredFields
/**
* Get the required form fields for this form. Includes building the jQuery
* validate structure
*
* @return RequiredFields
*/
function getRequiredFields()
{
$required = new RequiredFields();
$rules = array();
$validation = array();
$messages = array();
if ($this->Fields()) {
foreach ($this->Fields() as $field) {
$messages[$field->Name] = $field->getErrorMessage()->HTML();
if ($field->Required) {
$rules[$field->Name] = array_merge(array('required' => true), $field->getValidation());
$required->addRequiredField($field->Name);
}
}
}
// Set the Form Name
$rules = $this->array2json($rules);
$messages = $this->array2json($messages);
// set the custom script for this form
Requirements::customScript(<<<JS
\t\t\t(function(\$) {
\t\t\t\t\$(document).ready(function() {
\t\t\t\t\t\$("#Form_Form").validate({
\t\t\t\t\t\terrorClass: "required",\t
\t\t\t\t\t\tmessages:
\t\t\t\t\t\t\t{$messages}
\t\t\t\t\t\t,
\t\t\t\t\t\trules:
\t\t\t\t\t\t \t{$rules}
\t\t\t\t\t});
\t\t\t\t});
\t\t\t})(jQuery);
JS
, 'UserFormsValidation');
$this->extend('updateRequiredFields', $required);
return $required;
}
示例8: testFieldIsRequired
public function testFieldIsRequired()
{
//get the validator
$requiredFields = new RequiredFields($fieldNames = array('Title', 'Content', 'Image', 'AnotherField'));
foreach ($fieldNames as $field) {
$this->assertTrue($requiredFields->fieldIsRequired($field), sprintf("Failed to find '%s' field in required list", $field));
}
//add a new field
$requiredFields->addRequiredField('ExtraField1');
//check the new field is required
$this->assertTrue($requiredFields->fieldIsRequired('ExtraField1'), "Failed to find 'ExtraField1' field in required list after adding it to the list");
//check a non-existant field returns false
$this->assertFalse($requiredFields->fieldIsRequired('DoesntExist'), "Unexpectedly returned true for a non-existant field");
}
示例9: EnquiryForm
public function EnquiryForm()
{
if (!Email::validEmailAddress($this->EmailTo) || !Email::validEmailAddress($this->EmailFrom)) {
return false;
}
if (!$this->EmailSubject) {
$this->EmailSubject = 'Website Enquiry';
}
$elements = $this->EnquiryFormFields();
if ($elements->count() == 0) {
return false;
}
/* Build the fieldlist */
$fields = new FieldList();
$validator = new RequiredFields();
$jsValidator = array();
foreach ($elements as $el) {
$key = $this->keyGen($el->FieldName, $el->SortOrder);
$field = false;
$type = false;
if ($el->FieldType == 'Text') {
if ($el->FieldOptions == 1) {
$field = new TextField($key, htmlspecialchars($el->FieldName));
} else {
$field = new TextareaField($key, htmlspecialchars($el->FieldName));
$field->setRows($el->FieldOptions);
}
} else {
if ($el->FieldType == 'Email') {
$field = new EmailField($key, htmlspecialchars($el->FieldName));
} else {
if ($el->FieldType == 'Select') {
$options = preg_split('/\\n\\r?/', $el->FieldOptions, -1, PREG_SPLIT_NO_EMPTY);
if (count($options) > 0) {
$tmp = array();
foreach ($options as $o) {
$tmp[trim($o)] = trim($o);
}
$field = new DropdownField($key, htmlspecialchars($el->FieldName), $tmp);
$field->setEmptyString('[ Please Select ]');
}
} else {
if ($el->FieldType == 'Checkbox') {
$options = preg_split('/\\n\\r?/', $el->FieldOptions, -1, PREG_SPLIT_NO_EMPTY);
if (count($options) > 0) {
$tmp = array();
foreach ($options as $o) {
$tmp[trim($o)] = trim($o);
}
$field = new CheckboxSetField($key, htmlspecialchars($el->FieldName), $tmp);
}
} else {
if ($el->FieldType == 'Radio') {
$options = preg_split('/\\n\\r?/', $el->FieldOptions, -1, PREG_SPLIT_NO_EMPTY);
if (count($options) > 0) {
$tmp = array();
foreach ($options as $o) {
$tmp[trim($o)] = trim($o);
}
$field = new OptionsetField($key, htmlspecialchars($el->FieldName), $tmp);
}
} else {
if ($el->FieldType == 'Header') {
if ($el->FieldOptions) {
$field = new LiteralField(htmlspecialchars($el->FieldName), '<h4>' . htmlspecialchars($el->FieldName) . '</h4>
<p class="note">' . nl2br(htmlspecialchars($el->FieldOptions)) . '</p>');
} else {
$field = new HeaderField(htmlspecialchars($el->FieldName), 4);
}
} else {
if ($el->FieldType == 'Note') {
if ($el->FieldOptions) {
$field = new LiteralField(htmlspecialchars($el->FieldName), '<p class="note">' . nl2br(htmlspecialchars($el->FieldOptions)) . '</p>');
} else {
$field = new LiteralField(htmlspecialchars($el->FieldName), '<p class="note">' . htmlspecialchars($el->FieldName) . '</p>');
}
}
}
}
}
}
}
}
if ($field) {
if ($el->RequiredField == 1) {
$field->addExtraClass('required');
/* Add "Required" next to field" */
$validator->addRequiredField($key);
$jsValidator[$key] = $el->FieldType;
}
if ($el->PlaceholderText) {
$field->setAttribute('placeholder', $el->PlaceholderText);
}
$fields->push($field);
}
}
if ($this->AddCaptcha) {
$label = $this->CaptchaLabel;
$field = new CaptchaField('CaptchaImage', $label);
$field->addExtraClass('required');
//.........这里部分代码省略.........