Silverstripe form validation template




















All forms have some basic validation built in — email fields will only let the user enter email addresses, number fields will only accept numbers, and so on. Sometimes you need more complicated validation, so you can define your own validation by extending the Validator class. The framework comes with a predefined validator called RequiredFields , which performs the common task of making sure particular fields are filled out.

Below is the code to add validation to a contact form:. We've created a RequiredFields object, passing the name of the fields we want to be required. The validator we have created is then passed as the fifth argument of the form constructor. If we now try to submit the form without filling out the required fields, JavaScript validation will kick in, and the user will be presented with a message about the missing fields. If the user has JavaScript disabled, PHP validation will kick in when the form is submitted, and the user will be redirected back to the Form with messages about their missing fields.

How to make a simple contact form In this how-to, we'll explain how to set up a specific page type holding a contact form, which submits a message via email. Caution: This form is prone to abuse by spammers, since it doesn't enforce a rate limitation, or checks for bots. Edit on Github. Thanks for the reply.

But it didn't work. I still get this instead of the field name in the error messages. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 1. Let's now follow through with that. While we're in here, let's carve up the template and add a loop for all the comments. There are no comments right now, but there will be shortly. Now that we have our data models set up, we can start using them in our form handler.

Looking at our form method, the name of the handler we've specified is handleComment. Let's create that method, right below the form creation method. In the handler, we optimistically create the ArticleComment object as a first operation. You may not always want to do this. You might have some logic that determines otherwise based on the values provided, but let's just keep it simple for now. That's an easy step to forget. All the properties of a page Title , Content , ID , etc.

What's happening here? We'll get to the error in just a moment, but right now, it's important to understand where we are and what we're looking at. Take a look at the URL. The first two segments of that are easily identifiable. It's the URL of our current article page. The last part, CommentForm is what's called a controller action. By default, the URL part that immediately follows the page URL will tell the controller to invoke a method by that name. In this case, we want the CommentForm method on our controller to execute, because it creates the Form object, which is then passed along to our form submission handler.

This, right here, is where most of the magic of forms happens in SilverStripe. They actually submit to a URL that recreates them as they were rendered to the user. You may have noticed that I casually mentioned an an alarming detail of request handling in SilverStripe -- you can invoke arbitrary methods in the URL.

In fact, that's precisely why we're seeing this error. We can't just execute arbitrary controller methods from the URL. Let's do that now. We made a change to a static variable, so we have to run? Go back to the article page i. We don't want to do this in the middle of a form submission. Let's take this a step further. We've talked about how forms are first-class citizens in SilverStripe. Part of that is being model-aware. Looking at our handler method, we see that all the form parameters are named exactly the same as the ArticleComment database fields.

This is ideal, because it means we can take advantage of a massive time-saving method of the form class known as saveInto. Let's modify our function to call saveInto instead of manually assigning all the form values. Notice that we still have to manually assign the ArticlePageID field, as that is not present in the form data.

We could have passed it via a hidden input, which would eliminate that line of code. What's great about this method is that it can actually respond to the needs of a specific model. 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.



0コメント

  • 1000 / 1000