Welcome Guest, Not a member yet? Register   Sign In
required_with Validation Rule works on inserts but not updates
#2

(This post was last modified: 10-11-2020, 04:02 PM by mlurie.)

(10-10-2020, 08:27 PM)mlurie Wrote: I have a form where a select menu is disabled by default.  It's enabled when a when a checkbox is checked and disabled when the checkbox is uncheck using a bit of javascript.  I would like this select menu to be required only when the box is checked  The required_with validation rule is perfect for this and it works great on inserts.  However, it does not work on updates.  It's like the required_with rule somehow vanished with the update query.  The validation is taking place in the model before insert and update queries.  Both insert and update controller methods use the same view, so the HTML form is essentially the same.  I inspected the POST data when both inserting and updating and it is exactly the same.  The only difference is whether the data is being inserted or updated in the database.  Am I doing something wrong?  Is this a bug?

I finally figured it out.

It it's not the required_with rule (of course).  I have a helper function that I wrote to insert an option tag at the top of select menus with no value to be used a a de facto label.

PHP Code:
function form_dropdown_labeled(string $labelstring $form_dropdown) {
    list($segment1$segment2) = explode('>'$form_dropdown2);
    $labeled_dropdown $segment1 '>' "\n";
    if(strstr($segment2'selected="selected"'))
        $labeled_dropdown .= '<option value="" disabled="disabled">' $label '</option>';
    else
        $labled_dropdown .= '<option value="" selected="selected  disabled="disabled">' $label '</option>';
    $labeled_dropdown .= $segment2;
    return $labeled_dropdown;
 } 

When I removed the "selected" and "disabled" attributes from the inserted option tag...

PHP Code:
$labeled_dropdown .= '<option value="">' $label '</option>'


...the required_with validation rule began to work as expected.  I guess the disabled, selected option was throwing a false negative so to speak.  I'm still not sure why required_with worked on inserts at all.  One would think that the rule would fail to throw an error on both inserts and updates, not just updates.  This is what I get for getting too fancy with my code. I wanted the content of the option tag to be grayed out. Blush
Reply


Messages In This Thread
RE: required_with Validation Rule works on inserts but not updates - by mlurie - 10-11-2020, 03:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB