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.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Userforms adapted?


Go to End
Reply


13 Posts   3229 Views

Avatar
Nauri

Community Member, 11 Posts

1 March 2011 at 2:00am

Hi! I need your help!

I am a beginner in Silverstripe.

I use the module-user forms. How can I extend the form with fields for address (postal code, city, street, country) and telephone as required field?

Thank you.

Nauri

Avatar
MCK

Community Member, 9 Posts

1 March 2011 at 12:12pm

Hi Nauri,

Assuming you have created a page with the "User Defined Form" type, go to the "Form" tab in the CMS and add text fields. You can add fields, name them as you like (address, postal code, etc.) and under "Show Options" for the field tick "Is this field required" under Validation. I have attached a screen image to show you what I mean.

Avatar
Nauri

Community Member, 11 Posts

2 March 2011 at 9:34pm

Hi, MCK,

thanks for the quick reply.

Unfortunately, I need another solution.

If I create the text field, I can only check whether something is entered in the field. For example, I can enter in phone field or postal code field letters. And that's wrong.

I think I need a nummeric field. Unfortunately I do not know how I can realize that.

Can someone help me?

Thank you.

Nauri

Avatar
Willr

Forum Moderator, 5513 Posts

2 March 2011 at 11:03pm

The module doesn't currently have a 'numeric' field. You can see the list of available form fields in https://github.com/silverstripe/silverstripe-userforms/tree/master/code/editor.

Making a dedicated 'Numeric' form field would be relatively easy. First you would need to subclass the default EditableTextField class with your EditableNumericField then override the getFormField() (to return a numeric field) and also the getValidation method (https://github.com/silverstripe/silverstripe-userforms/blob/master/code/editor/EditableTextField.php#L58) to validate for numeric values (uses jQuery validate - http://docs.jquery.com/Plugins/Validation)

Avatar
Nauri

Community Member, 11 Posts

10 March 2011 at 10:53am

Thanks for your tip.
Unfortunately I am not progressed. I could not adapt to the script. I am still inexperienced in Silverstripe ((
I've changed. Is it correct?

<?php
/**
* EditableTextField
*
* This control represents a user-defined text field in a user defined form
*
* @package userforms
*/

class EditableNumericField extends EditableFormField {

static $singular_name = 'Numeric field';

static $plural_name = 'Numeric fields';

function getFieldConfiguration() {
$fields = parent::getFieldConfiguration();

// eventually replace hard-coded "Fields"?
$baseName = "Fields[$this->ID]";

$minLength = ($this->getSetting('MinLength')) ? $this->getSetting('MinLength') : '';
$maxLength = ($this->getSetting('MaxLength')) ? $this->getSetting('MaxLength') : '';
//$rows = ($this->getSetting('Rows')) ? $this->getSetting('Rows') : '1';

$extraFields = new FieldSet(
new FieldGroup(_t('EditableNumericField.TEXTLENGTH', 'Text length'),
new TextField($baseName . "[CustomSettings][MinLength]", "", $minLength),
new TextField($baseName . "[CustomSettings][MaxLength]", " - ", $maxLength)
),
new TextField($baseName . "[CustomSettings][Rows]", _t('EditableNumericField.NUMBERROWS', 'Number of rows'), $rows)
);

$fields->merge($extraFields);
return $fields;
}

/**
* @return EditableNumericField|TextField
*/
function getFormField() {
if($this->getSetting('Rows') && $this->getSetting('Rows') > 1) {
return new TextareaField($this->Name, $this->Title, $this->getSetting('Rows'));
}
else {
return new TextField($this->Name, $this->Title, null, $this->getSetting('MaxLength'));
}
}

/**
* Return the validation information related to this field. This is
* interrupted as a JSON object for validate plugin and used in the
* PHP.
*
* @see http://docs.jquery.com/Plugins/Validation/Methods
* @return Array
*/
public function getValidation() {
$options = array();
if($this->getSetting('MinLength')) $options['minlength'] = $this->getSetting('MinLength');
if($this->getSetting('MaxLength')) $options['maxlength'] = $this->getSetting('MaxLength');

return $options;
}
}

How do I change validator for numeric field?

Thanks for the help.

Avatar
Willr

Forum Moderator, 5513 Posts

10 March 2011 at 2:05pm

Try the jQuery validation documentation since they have all the available rules (http://docs.jquery.com/Plugins/Validation/Methods/number). I think what you'll want is 'number'. So then you would add a option to getValidation() like

...
$options['number'] = true;

return $options;

Avatar
Nauri

Community Member, 11 Posts

12 March 2011 at 6:40am

thank you,

I'll try it

Avatar
Max Bradbury

Community Member, 7 Posts

8 June 2011 at 4:07am

Edited: 08/06/2011 4:18am

Hi there, I'm attempting to add a numeric field as well, and I have a problem; the form only seems to run through the validation when the field is required. I don't want to make my number fields required, I just want to stop users from entering text into them.

The validation does seem to work when I tick "Required field" (it checks length, positive/negative etc. OK) but the error message is always "[field name] is required." rather than something relevant like "Number must be below 100"

Here's my "EditableNumericField.php":

<?php
/**
* EditableNumericField
*
* This control represents a user-defined numeric field in a user defined form
*
* @package userforms
*
* Max Bradbury 2011
*/

class EditableNumericField extends EditableFormField {

static $singular_name = 'Numeric field';
static $plural_name = 'Numeric fields';

function getFieldConfiguration() {
$fields = parent::getFieldConfiguration();

// eventually replace hard-coded "Fields"?
$baseName = "Fields[$this->ID]";

$minLength = ($this->getSetting('MinLength')) ? $this->getSetting('MinLength') : '';
$maxLength = ($this->getSetting('MaxLength')) ? $this->getSetting('MaxLength') : '';
$minValue = ($this->getSetting('MinValue')) ? $this->getSetting('MinValue') : '';
$maxValue = ($this->getSetting('MaxValue')) ? $this->getSetting('MaxValue') : '';
$positive = ($this->getSetting('Positive')) ? $this->getSetting('Positive') : true;
$negative = ($this->getSetting('Negative')) ? $this->getSetting('Negative') : false;

$extraFields = new FieldSet(
new FieldGroup(_t('EditableNumericField.NUMLENGTH', 'Number length'),
new TextField($baseName . "[CustomSettings][MinLength]", "", $minLength),
new TextField($baseName . "[CustomSettings][MaxLength]", " - ", $maxLength)
),
new FieldGroup(_t('EditableNumericField.NUMRANGE', 'Number range'),
new TextField($baseName . "[CustomSettings][MinValue]", "", $minValue),
new TextField($baseName . "[CustomSettings][MaxValue]", " - ", $maxValue)
),
new FieldGroup(_t('EditableNumericField.POSNEG', 'Allow positive/negative numbers'),
new CheckboxField($baseName . "[CustomSettings][Positive]", "Positive", $positive),
new CheckboxField($baseName . "[CustomSettings][Negative]", "Negative", $negative)
)
);

$fields->merge($extraFields);

return $fields;
}

/**
* @return TextareaField|TextField
*/
function getFormField() {
return new TextField($this->Name, $this->Title, null, $this->getSetting('MaxLength'));
}

/**
* Return the validation information related to this field. This is
* interrupted as a JSON object for validate plugin and used in the
* PHP.
*
* @see http://docs.jquery.com/Plugins/Validation/Methods
* @return Array
*/
public function getValidation() {
$options = array();

if ($this->getSetting('MinLength')) {
$options['minlength'] = $this->getSetting('MinLength');
}

if ($this->getSetting('MaxLength')) {
$options['maxlength'] = $this->getSetting('MaxLength');
}

if ($this->getSetting('MinValue') && $this->getSetting('MaxValue')) {
$options['range'] = array($this->getSetting('MinValue'), $this->getSetting('MaxValue'));
}

if (!$this->getSetting('Positive')) {
$options['max'] = 0;
}

if (!$this->getSetting('Negative')) {
$options['min'] = 0;
}

$options['number'] = true;

return $options;
}
}

Any ideas? Am I missing something?

Thanks a lot,
Max

Go to Top