Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question - Radio elements and post-validation state #10

Open
emeraldjava opened this issue Mar 14, 2014 · 2 comments
Open

Question - Radio elements and post-validation state #10

emeraldjava opened this issue Mar 14, 2014 · 2 comments

Comments

@emeraldjava
Copy link

Hey,

I've a simple form with three radio elements. My issue is that if validation fails on any of the elements in the form, the state of the radio elements is lost. This does not happen for the text elements. I'm wondering is there something i'm expected as the form developer to handle this case or should the framework be expected to handle this.

My code

        $gender_drop_down = WP_Form_Element::create('radios')->set_name('bhaa_gender')
            ->set_label('Gender')->set_classes(array('radio-inline'))
            ->add_option('M','M')
            ->add_option('W','W');

and a screen shot of the form

image

Note - The gender field was selected before this form was submitted. I just want to avoid have to re-check the three radio buttons each time.

@brainthinks
Copy link

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 );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@emeraldjava @brainthinks and others