<?php
/**
 * class user_create_form
 *
 * Description for class user_create_form
 *
 * @author:
*/
namespace form;

use core\form;

class user_create_form extends form {
	public $name = "";
	public $email = "";
	public $role = "";
	public $password = "";
	public $repassword = "";
	public $active = "";

	public function init() {
		$this->addElement("name", array("required"));
		$this->addElement("email", array("required","email"));
		$this->addElement("role", array("required"));
		$this->addElement("password", array("required"));
		$this->addElement("repassword", array("required"));
		$this->addElement("active", array());
	}

	public function valid() {
		if(!$this->send) {
			return false;
		}

		$valid = true;

		$this->name = $this->getElement("name")->getValue();
		if(empty($this->name)) {
			$this->elements["name"]->setError("Pole wymagane");
			$valid = false;
		}

		$this->email = $this->getElement("email")->getValue();
		if(empty($this->email)) {
			$this->elements["email"]->setError("Pole wymagane");
			$valid = false;
		} elseif(!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
			$this->elements["email"]->setError("Podaj poprawny adres e-mail");
			$valid = false;
		}

		$this->role = $this->getElement("role")->getValue();
		if(empty($this->role) || ($this->role != "administrator" && $this->role != "user")) {
			$this->elements["role"]->setError("Pole wymagane");
			$valid = false;
		}

		$this->password = $this->getElement("password")->getValue();
		if(empty($this->password)) {
			$this->elements["password"]->setError("Pole wymagane");
			$valid = false;
		}

		$this->repassword = $this->getElement("repassword")->getValue();
		if(empty($this->repassword)) {
			$this->elements["repassword"]->setError("Pole wymagane");
			$valid = false;
		} elseif($this->password != $this->repassword) {
			$this->elements["repassword"]->setError("Powtórz poprawnie hasło");
			$valid = false;
		}
		
		return $valid;
	}
}
?>