Jump to:

10843 Posts in 2514 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » Userforms adapted?

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 2801 Views
  • Nauri
    Avatar
    Community Member
    11 Posts

    Userforms adapted? Link to this post

    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

  • MCK
    Avatar
    Community Member
    8 Posts

    Re: Userforms adapted? Link to this post

    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.

  • Nauri
    Avatar
    Community Member
    11 Posts

    Re: Userforms adapted? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Userforms adapted? Link to this post

    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)

  • Nauri
    Avatar
    Community Member
    11 Posts

    Re: Userforms adapted? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Userforms adapted? Link to this post

    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;

  • Nauri
    Avatar
    Community Member
    11 Posts

    Re: Userforms adapted? Link to this post

    thank you,

    I'll try it

  • Max Bradbury
    Avatar
    Community Member
    7 Posts

    Re: Userforms adapted? Link to this post

    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

    2801 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.