[Plugin: Custom Content Type Manager] Formatting a Text Field for numbers
I built a form that has a price field – I used the custom text field but I would like to enforce proper syntax on it, something like:
value='<?php echo ( number_format( float, 2, '.', '' ) : '70.00'); ?>'.
How would I do that so it doesn’t break the form?
Do you want to enforce that in the manager? So basically you want data validation in the manager? That’s not implemented yet. See http://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=6
If you want to do this in your templates, then just use the PHP, just like you outlined. Just get the value using the get_custom_field() function and then have your way with the value it returns.
Enforcing it in the manager, or more specifically, on the form itself, seems the prudent route. I’m new to CCTM so I don’t fully understand how you have implemented the admin form. Are the form inputs saved in a database table, called when a user clicks the ‘Add New’ button? Or is an actual form built and saved as a php template?
I looked for the custom form I created but found no page with that name. So if these are database entries could I use a query to alter the value without breaking the output?
The CCTM uses built-in WP functionality and built-in WP tables, so it’s not doing anything different than WP here — it’d be the exact same behavior if you did not activate CCTM and you manually added custom fields to a post. WP does not standardize the custom fields, and it only offers support for text fields, so CCTM offers a lot of improvements there. WP unfortunately has no events that allow for real validation of the submitted forms — the closest you can come is to check the submitted data and force the status of the page to “draft” if the input didn’t validate (as outlined in Issue 6 above). This is one area where you can see the limits of the underlying WP architecture.
To your question, the way that the form is generated is this:
1. the custom fields meta box is handled by the CCTM (I’m replacing the default WP meta box here and forcing the custom fields to be standardized and support all those different input types).
2. The html in that meta box is generated by the various form element classes (located in the CCTM’s folder: includes/elements). These are PHP files that follow a pattern: each time the form element is printed into the meta box, it is calling the respective PHP class’s “get_edit_field_instance” function. E.g. a colorselector field would generate the HTML for the colorselector input via the get_edit_field_instance() function inside of the includes/elements/colorselector.php file.
The HTML that is used by that function can be customized without editing the core, but I haven’t documented that yet (I’m still tweaking it for 0.9.5), but it will let you customize the HTML that shows up in the manager. Issue 6 will incorporate other PHP classes that handle the custom validation, but that probably won’t be implemented until 0.9.6 or 0.9.7… still a couple months off, but the idea is that I’ll ship it with a set of common validation functions, and tech-savvy users can add their own PHP classes to implement their own validation functions (like the one you outlined).
But right now, custom validation or input formats is not possible, and after looking at the dilapidated shape of the underlying WP code, I know that implementing this functionality will be an uphill battle. If you wanted to fund the development of that feature, I could send you a quote, otherwise maybe we could start up something with http://www.kickstarter.com/ and get enough people in on it to get me to prioritize it.
Another option is to use your own custom field — what’s required there is that you copy one of the existing field PHP classes and you would just have to implement the necessary functions. I’ve got this working in 0.9.5 (which will be available soon — you can try the dev version), but basically the CCTM will look in your own custom directory for any PHP classes, and you’ll be able to create your own “currency” field (as an example). There’s another function that fires to filter the data as it gets saved, so you could implement your filtering controls there in PHP.
Otherwise, I’d really recommend MODX (http://modx.com/) for this type of thing. It’s architecture is so much cleaner, and all of this stuff is built into the core very cleanly. The CCTM plugin is sorta just crumbs from MODx’s table.
- The topic ‘[Plugin: Custom Content Type Manager] Formatting a Text Field for numbers’ is closed to new replies.