Code Monkey home page Code Monkey logo

Comments (1)

brainthinks avatar brainthinks commented on August 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.