Jump to:

11002 Posts in 2734 Topics by 1823 members

All other Modules

SilverStripe Forums » All other Modules » Adding attributes to a form

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1395 Views
  • px
    Avatar
    Community Member
    10 Posts

    Adding attributes to a form Link to this post

    Hi! I'm a newbie in silverstripe and I would just like to ask if how can i add an attribute in a form? For example i want to add onsubmit=''return false;" and class="myClass". Here is the function that I'm using:

    public function NewsletterForm() {
    $fields = new FieldSet(
    new EmailField('Email','')
    );

    $actions = new FieldSet(
    new FormAction('NewsletterSubmit', 'Ok!')
    );

    $validator = new RequiredFields('Email','onsubmit');
    return new Form($this, 'NewsletterSubmit', $fields, $actions,$validator);
    }

    /**
    * Inserts a submitted email to database
    * @param <type> $form
    */
    public function NewsletterSubmit($form) {
    $newsletter = new Newsletter();
    $newsletter->Email=$_POST['Email'];
    $newsletter->write();
    return Director::redirectBack();

    //Director::redirect(Director::currentURLSegment()."?msg=Thank+you+for+signing+up!");
    }

    Any help will be much appreciated. Thanks!

  • px
    Avatar
    Community Member
    10 Posts

    Re: Adding attributes to a form Link to this post

    hello?is there anyone who knows the answer to my simple question?

    it's like having this in form tag:

    <form name="frmName" method="post" action="home/newsletterSubmit" class="myClass" onsubmit="return false;">

    i just want to add class and onsubmit attributes in this form.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Adding attributes to a form Link to this post

    You should never mix javascript and HTML. Assign your event handlers unobtrusively in a javascript file.

    $('#my-form').submit(function() {
    // do stuff
    return false;
    });

  • UncleCheese
    Avatar
    4085 Posts

    Re: Adding attributes to a form Link to this post

    If you need to customize your form, there's nothing forcing you to use the default form markup. That's just for quick rendering and prototyping, IMO.

    <% control MyForm %>
    <form $FormAttributes>
    <div class="customStuff">
    <% control Fields %>
    $FieldHolder
    <% end_control %>
    </div>

    <% control Actions %>
    $FormHolder
    <% end_control %>
    </form>

    1395 Views
Page: 1
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.