Welcome Guest, Not a member yet? Register   Sign In
Validation with nested input
#1

(This post was last modified: 05-25-2020, 06:22 AM by troturier.)

Hi, I have some difficulties using certain validation rules with nested input, for example:

Let's say I have this kind of form :

PHP Code:
<form id="userForm" accept-charset="utf-8" method="POST" >
        <div class="form-group">
            <label>Adresse Email</label>
            <div class="input-group">
                <div class="input-group-prepend">
                    <div class="input-group-text bg-primary"> <class="fa fa-user text-white"></i> </div>
                </div>
                <input id='userEmail' name="user[email]" class="<?php if (isset($errors['user.email'])) echo "is-invalid";?> form-control" placeholder="email" type="text" value="<?php echo $userEmail ?? set_value('user[email]'); ?>">
                <?php if (isset($errors['user.email']))echo "<div class=\"text-danger\" role=\"alert\">".$errors['user.email']."</div>"?>
            </div>
        </div>


        <div class="form-group">
            <label>Mot de passe</label>
            <div class="input-group">
                <div class="input-group-prepend">
                    <div class="input-group-text bg-primary"> <i class="fa fa-lock text-white"></i> </div>
                </div>
                <input name="user[password]" class="<?php if (isset($errors['user.password']))echo "is-invalid";?> form-control" placeholder="******" type="password">
                <?php if (isset($errors['user.password'])) echo "<div class=\"text-danger\" role=\"alert\">".$errors['user.password']."</div>"?>
            </div>
        </div>

        <div class="form-group">
            <label>Confirmer le mot de passe</label>
            <div class="input-group">
                <div class="input-group-prepend">
                    <div class="input-group-text bg-primary"> <i class="fas fa-unlock text-white"></i> </div>
                </div>
                <input name="user[passConfirm]" class="<?php if (isset($errors['user.passConfirm']))echo "is-invalid";?> form-control" placeholder="******" type="password">
                <?php if (isset($errors['user.passConfirm'])) echo "<div class=\"text-danger\" role=\"alert\">".$errors['user.passConfirm']."</div>"?>
            </div>
        </div>

   
    <div class="form-group">
        <button type="submit" class="btn btn-primary btn-block"><?php echo $actionButton;?></button>
    </div>
</form> 

And this set of rules : 

PHP Code:
<?php

    
// Rules used with the registration form
    public $registrationRules = [
        'user.email'     => [
            'label' => 'Email',
            'rules' => 'required',
            'errors' => ['required' => 'Vous devez renseigner une adresse email.']
        ],
        'user.password'      => [
            'label'     => 'Mot de passe',
            'rules'     => 'required',
            'errors'    => ['required' => 'Vous devez renseigner un mot de passe.']
        ],
        'user.passConfirm' => [
            'label'     => 'Mot de passe de confirmation',
            'rules'     => 'required|matches[user.password]',
            'errors'    => [
                'required'  => 'Vous devez confirmer votre mot de passe.',
                'matches'   => 'Votre mot de passe de confirmation ne correspond pas.'
            ]
        ]
    ]; 

The "matches" rule seems to not be compatible with this kind of input. Am I missing something here?  Huh

One workaround would be to retrieve the "user" array and to validate its data without using nested input. But I was wondering if I was just using the wrong syntax or something.

Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB