<?php

namespace ModulesGarden\Servers\OpenXchange\App\Validators;


use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Validators\BaseValidator;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\Validators\boolen;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 10.10.19
 * Time: 11:21
 * Class RepeatPasswordValidator
 */
class RepeatPasswordValidator extends BaseValidator
{

    /**
     * return true if data is valid, false if not,
     * add error messages to $errorsList
     *
     * @param $data mixed
     * @param $additionalData mixed
     * @return boolen
     */
    protected function validate($data, $additionalData = null)
    {
        $formData =  $additionalData->get('formData');
        
        if (isset($formData['repeatPasswordToReset']) && $formData['resetPassword'] != '')
        {
            if($data !== $formData['resetPassword'])
            {
                $this->addValidationError('passwordsIsNotTheSame');
                return false;
            }
        }

        if (!isset($formData['resetPassword']))
        {
            if($data !== $formData['password'])
            {
                $this->addValidationError('passwordsIsNotTheSame');
                return false;
            }
        }

        return true;
    }
}
