Silverstripe custom form template




















If our ArticleComment object had a method called saveComment or saveName , it could save the form data in its own specific way. So it may look like a shotgun approach, but it can actually be pretty granular if you want it to be. Our form is accepting submissions and working as expected, so let's now add a bit of validation. We're already using RequiredFields , which is our primary sentinel against bad data, but what if we want to add some custom logic that goes beyond simple sanity checks?

If the logic were really complicated, we could write our own validator, which we'll cover in the future, but for simple validation, it's fine to do all of this in your form handler method. Let's run a check to make sure the user's comment has not already been added. You might think of this as really basic spam protection.

We look for a comment on this page specifically that contains the same content, and if so, we add a message to the top of the form. The value 'bad' as the second argument gives it an appropriate CSS class. To filter out false positives, we make sure the comment is at least 20 characters long. It's plausible that multiple readers might comment "Nice article" or "Good work" and we don't want to punish them.

Again, the lesson here is not about spam protection, but just how to handle form validation. Don't accept this as gospel on how to secure your blog comments. Try submitting the form again with an existing comment, and you'll see that we generate an error message. There's one usability problem here, and perhaps we shouldn't worry about it too much since we're not particularly motivated to be nice to spammers, but for the sake of teaching the concept, it would be nice if the form saved its invalid state, so that the user doesn't have to repopulate an empty form.

For this, the convention is to use Session state. If everything checks out, we clear it, so that the form renders clean on the next page load. If not, we're going to want the form to render the session data. If it does, return the form with the data loaded into it using loadDataFrom. If not, just return the form as is.

Remember, most form methods all return the Form object, so it's okay to return the result of loadDataFrom here. In this tutorial, we'll discuss one of the major building blocks of modular and reusable code in SilverStripe Framework: extensions.

We won't be writing a whole In this lesson, we'll create the Property object that will drive most of the content in our application, and add a management interface for it in In this lesson, we'll introduce the concept of a controller action, which is a URL route that executes a function on a controller.

Search Site search. Code repository. How SilverStripe handles forms This lesson is about frontend forms, which is a very important distinction to make, because we've actually been working with forms since very early on in the CMS.

Creating a simple form Let's look at a simple form constructor to better understand how this will work. The benefits to this are impressive, and we'll see some of them in a bit. ContactForm A string representing the name of the method that creates the form. Odd, right? It's a bit unconventional to be dealing with class methods as strings, for sure, but this is a really key feature. The form will submit to a URL that calls this method, which means that we get the fully configured Form object to inspect and manipulate when handling submission.

We can add error messages, set state, and even manipulate the fields based on the user's submission. This is because tabs can be nested in other tabsets, and digging through several levels to get the one tab set you want to add to would be tedious. The dot-separated string identifier allows you traverse the tab set ancestrally, where the highest level is on the left, and the tab you want to add to is all the way on the right.

The 'Root' tab is the highest level tab set, assigned to all pages. The first argument of the form field constructor is the field name.

The second argument is an optional label for the field. If left null, the label will be the name of the field, as we see in the case of our Teaser field. Notice our new fields at the bottom of the page. Try populating them, and ensure that it saves state. There are a couple usability issues we can address here. One is that the fields are all pinned to the bottom of the page, below the content editor, making it difficult for the user to find.

Fortunately, the addFieldToTab method accepts an optional argument to specify an existing field that the new field should come before.

In this case, we want these fields before the Content field. Second, the date field is a bit awkward. Every form field exposes a public API that you can use to configure the field. When using subfolders in your template structure e. The include tag can be particularly helpful for nested functionality and breaking large templates up. In this example, the include only happens if the user is logged in. Includes can't directly access the parent scope when the include is included.

However you can pass arguments to the include. This snippet loops over the children of a page, and generates an unordered list showing the Title property from each page. This demonstrates the concept of Scope. As the template has access to the list object, templates can call DataList methods. Inside the loop scope, there are many variables at your disposal to determine the current position in the list and iteration.

Sometimes you will have template tags which need to roll into one another. But inside the loop, we were in the scope of an item in the list i. The scope determines where the value comes from when you refer to a variable. Typically the outer scope of a Page. Some common examples are TextField or DropdownField.

The fields are added to the FieldList fields property on the Form and can be modified at up to the point the Form is rendered. A field can be appended to the FieldList. The default FormField object has several methods for doing common operations.

You can provide custom templates by using the setTemplate method on either the Form or FormField. For more details on providing custom templates see Form Templates.



0コメント

  • 1000 / 1000