Forum

Thread tagged as: Field-Types

Require valid URL on text field type?

Is there any way to validate a text field type against a regex pattern on the CMS control panel? I would like to make input fool proof for my client.

Josh Worden

Josh Worden 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using type="url" ?

Yes, but it allows anything (not just URLs) to be entered and saved. Am I doing something wrong? Here's the simple template I'm using for multiple entries:

<div class="artist"> <a href='<perch:content id="link" type="url" label="Artist Website" required="true" order="3" />' target='_blank'><img src="<perch:content type="image" id="image" label="Artist Image" required="true" order="1" bucket="Artist Images" />" alt="<perch:content type="text" id="name" label="Artist Name" />" /><span><perch:content type="text" id="name" label="Artist Name" required="true" order="2" /></span></a> </div>

So, it seems like the type="url" is only for inputs on the front-end (am I correct)? That's why I was originally just using a text field-type. Is there any way to achieve what I'm trying to do? Seems like a pretty simple process.

Drew McLellan

Drew McLellan 2638 points
Perch Support

For a form in the control panel you'd need to build a field type to do custom validation.

For public facing forms, URLs are validated both client- and server-side.

Ok, that was what I kinda suspected. Is there a boiler-plate custom field-type to build from?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Actually, simpler than that, you could add your own validation helper.

<perch:content id="url" type="url" helper="JoshValidators::url" />

and then define a class (maybe just include it in your config.php file):

class JoshValidators 
{
    public static function url($val)
    {
        return filter_var($val, FILTER_VALIDATE_URL);
    }
}

Basically, you defined a static method which gets passed the value of the field. You return true if it's valid, false if it's not.

Simon Clay

Simon Clay 127 points

Hi Josh, I realise this isn't validation but I would do it this way to make it a bit more fool proof.

<div class="artist"> <a href='https://<perch:content id="link" type="text" label="Artist Website" required="true" order="3" replace="https://|,https://|" />' target='_blank'>

This automatically adds in the required https:// if the editor didn't include it.

Hmm, I tried defining the class as you suggested and adding the helper tag but it doesn't seem to be doing anything. I can still set the value of the field to a non-url and it saves successfully and does not display an error message. Is there something else I should be doing to make sure that it validates and displays an error if it's not valid?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, I've given you bad advice there - validation helpers do only apply to front end forms, not control panel forms. Apologies.

I can't actually think of a good way to do this server-side as things stand. I'll have a think about it and see what I can come up with, but it may need to wait until Perch 2.9.

Thanks for the quick reply. I'll look forward to 2.9.

In the meantime, would it be possible for me to hack together a custom field-type that would trigger an error and prevent the user from saving the field data if it doesn't pass validation? I don't mind coding it as long as the functionality is there.

Simon Clay

Simon Clay 127 points

Josh, just in case you didn't see my code above... it's not validation, but it's what I use for url fields.

Thanks Simon! Def a good start, but I'm looking for something completely fool proof (as a non-tech person will be maintaining the site).

Simon Clay

Simon Clay 127 points

Understood :)

Bumping this. Never received a reply: "In the meantime, would it be possible for me to hack together a custom field-type that would trigger an error and prevent the user from saving the field data if it doesn't pass validation?"

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, as I said, I don't think there's a good way to do this server-side as things stand.

Ok. Looking forward to a solution in 2.9.

Hello all,

Did we get a solution to this in Perch 3.0?