Comments (1)
I ran into this same problem and I was able to patch the repo to get it working. To state the problem more generally, when a form is submitted and validation fails, the selected radio options are not displayed when the form is re-rendered.
The problem (in my opinion) lies in the default radios view - /classes/views/WP_Form_View_Radios.php
The radios element is re-rendered using the WP_Form_View_Radios->radios() method. In this method, there is no logic that will mark a specific radio button as checked. Here is how I fixed the problem in my environment:
Change this:
$output .= $this->radio( $key, $label, $attributes );
To this:
if ($element->get_value() == $key) {
$output .= $this->radio( $key, $label, $attributes + array('checked' => 'checked') );
}
else {
$output .= $this->radio( $key, $label, $attributes );
}
And it should work as you expect. This will retrieve the value that the user set in the radios and set that individual radio as checked.
Note that I suspect that this default radios view will also not render any value or default value up front (meaning when the form is originally rendered rather than when it is re-rendered upon validation failure), though I did not test this. In that case, I think the fix would be:
if ($element->get_value() == $key) {
$output .= $this->radio( $key, $label, $attributes + array('checked' => 'checked') );
}
elseif (empty($element->get_value()) && $element->get_default_value() == $key) {
$output .= $this->radio( $key, $label, $attributes + array('checked' => 'checked') );
}
else {
$output .= $this->radio( $key, $label, $attributes );
}
from wp-forms.
Related Issues (10)
- Field name conflict with custom post type HOT 3
- Esc textarea content
- Documentation - Redirect to same form post submission HOT 1
- Add an example folder with sample forms HOT 1
- Support FieldSet elements HOT 2
- Support form pagination
- Call to a member function get_validators() on a non-object HOT 2
- wp_form_htmltag_default - add an extra attribute to the element to support this customisation
- How to add selected attribute to specific options in a checkbox group. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wp-forms.