Welcome Guest, Not a member yet? Register   Sign In
validating an "edit" form
#11

[eluser]jleequeen[/eluser]
One solution I guess would be to use a hidden field that always has a value, and run the callback off that field, but that is kind of a ugly solution to the problem.
#12

[eluser]xwero[/eluser]
Related fields are a big gap in the vailidation library but it's very difficult to have solution that fits almost all scenarios.

The easiest solution would be to add a key to the POST global and check which field is filled in and assign that value to the new key.
Code:
if($_POST['field1'] != '')
{
    $_POST['joined_field] = $_POST['field1'];
}

if($_POST['field2'] != '' && $_POST['joined_field] == '')
{
    $_POST['joined_field] = $_POST['field2'];
}

if($_POST['field3'] != '' && $_POST['joined_field] == '')
{
    $_POST['joined_field] = $_POST['field3'];
}

$rules['joined_field'] = 'required'; // change to required_one (see below)
This gives you a bit of trouble with the error message as it is meant for one field and setting a custom message will change it for all other fields that use the required rule too. To circumvent this you can add to the MY_Validation.php file a method named required_one which as following code
Code:
function required_one($str)
{
    return $this->required($str);
}
So it's actually an alias of the required method. Now the only thing you have to do is add a
Code:
$lang['required_one'] = "Filling in one of these fields, %s, is required.";
to the language file and you are good to go.
#13

[eluser]jleequeen[/eluser]
@xwero

That is a good solution to the problem. I agree there is a gap that maybe could some day be filled for this situation in the validation class. It also makes me look at my actual form itself and determine if there is a better way for me to lay it out and what I do and don't require that might be more user-friendly for my users.




Theme © iAndrew 2016 - Forum software by © MyBB