Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-12-13 17:00:07

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Required form fields for write tab

To write tab :
Is there a solution to make form fields required (custom fields or article image for ex.) if article has the live status ?
If the status is draft, the field is not required.

Thanks

Offline

#2 2011-12-13 20:56:45

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,549
Website GitHub Twitter

Re: Required form fields for write tab

Hi sacripan

Can javascript can do the trick? trigger save button and test if it s live or draft and check the custom field to see if it s empty!

A+

Offline

#3 2011-12-13 22:25:26

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: Required form fields for write tab

Using jQuery… (not tested)

$('#article_form').submit(function() {
 var goahead = true; /*by default submit the form*/

   /*custom javascript to check values of fields. For example:*/
 if ($('#custom-1').val()=""){
    goahead = false; /*nothing in custom field 1 so set goahead to false*/
 }

 return goahead;
});

You could use bot_write_tab_customize or jmd_admin_js to implement something like this.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#4 2011-12-14 10:23:42

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: Required form fields for write tab

@MattD,
Thank you for the snippet, I have to test if I have no better.

@All
Yes, of course, I can add an overlay Javascript.
But Textpattern is a php CMS, right ?

Validating a form with only Javascript? In this case it is a lesser evil.

Ok, I lacked precision. Is there a php solution (plug’in?), To add validation options for the write form?

Offline

#5 2011-12-14 10:42:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,947
Website GitHub

Re: Required form fields for write tab

sacripant wrote:

Is there a php solution (plug’in?), To add validation options for the write form?

Not yet, but I think it’s possible. A plugin can hook into the Write tab’s article save event/step using pre="1" so the plugin is called before Txp’s built in routine. You could then do your validation rules and (probably) bomb out if they are not met.

I’m not sure how successful it would be as I don’t know how easy it would be to bypass Txp’s save step which would come immediately after the plugin finishes. It would probably involve some code duplication as you’d have to display the Write tab yourself and put your own message in like “custom_field ‘name’ needs to be filled in”, then you’d have to terminate the code so it didn’t run Txp’s built-in save.

It might be simpler than that — maybe just making sure the txp_article.php file is present and calling the function to display the write tab manually with the given message string — but I’ve never really looked into it.

Last edited by Bloke (2011-12-14 10:44:47)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB