Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
sheychen | a8d36db23f |
|
@ -8,7 +8,7 @@
|
|||
}
|
||||
],
|
||||
"require": {
|
||||
"krutush/template": "dev-develop"
|
||||
"krutush/template": "^0.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
|
@ -16,28 +16,17 @@ class Element{
|
|||
|
||||
public function name() : string{ return $this->data['name']; }
|
||||
|
||||
public function id(string $id): self{
|
||||
$this->data['id'] = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function label(string $label, string $more = ''): self {
|
||||
$this->data['label'] = $label;
|
||||
$this->data['label.more'] = $more;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function required(bool $value = true) : self{
|
||||
$this->data['required'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function value(string $value = null) : self{
|
||||
public function value(string $value) : self{
|
||||
$this->data['value'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function get(){
|
||||
public function get() : ?string{
|
||||
return $this->data['value'];
|
||||
}
|
||||
|
||||
|
@ -46,22 +35,14 @@ class Element{
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function valid($data)/* :bool|string */{
|
||||
public function valid(mixed $data)/* :bool|string */{
|
||||
if((!isset($data) || empty($data)) && isset($this->data['required']) && $this->data['required'] == true)
|
||||
return 'requis';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getId(): string{
|
||||
return isset($this->data['id']) ? $this->data['id'] : $this->data['name'];
|
||||
}
|
||||
|
||||
protected function htmlLabel(): string{
|
||||
return isset($this->data['label']) ? '<label for="'.$this->getId().'" '.$this->data['label.more'].'>'.$this->data['label']."</label>\n" : '';
|
||||
}
|
||||
|
||||
public function html(string $more = '') : string{
|
||||
return $this->htmlLabel().'<span '.$more.'></span>';
|
||||
return '<span '.$more.'></span>';
|
||||
}
|
||||
}
|
49
src/Form.php
49
src/Form.php
|
@ -11,30 +11,15 @@ class Form {
|
|||
private $name;
|
||||
private $errors = array();
|
||||
private $set = false;
|
||||
private $csrfToken;
|
||||
public static $csrfSession = '_form_token';
|
||||
public static $csrfInput = "_token";
|
||||
|
||||
public function __construct(string $name, string $path, string $extention = null, bool $folder = true, array $sets = array()){
|
||||
public function __construct(string $name, string $path, bool $extention = true, bool $folder = true){
|
||||
$this->name = $name;
|
||||
$this->resetCsrf();
|
||||
$tpl = new Html($path, $extention, $folder);
|
||||
$tpl->set($name, $this)
|
||||
->sets($sets)
|
||||
->run('buffer');
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function resetCsrf(){
|
||||
if(session_status() == PHP_SESSION_NONE) session_start(); //TODO: create Krutsh\Session
|
||||
if(isset($_SESSION[static::$csrfSession][$this->name])){
|
||||
$this->csrfToken = $_SESSION[static::$csrfSession][$this->name];
|
||||
}else{
|
||||
$this->csrfToken = base64_encode(random_bytes(6));
|
||||
$_SESSION[static::$csrfSession][$this->name] = $this->csrfToken;
|
||||
}
|
||||
}
|
||||
|
||||
public static function sanitize(array $data) : array{
|
||||
$return = array();
|
||||
foreach($data as $key => $value){
|
||||
|
@ -47,38 +32,25 @@ class Form {
|
|||
public function valid(array $data) : bool{
|
||||
$data = static::sanitize($data);
|
||||
$this->set = true;
|
||||
if(!isset($_SESSION[static::$csrfSession][$this->name]) || !isset($data[static::$csrfInput]) || $_SESSION[static::$csrfSession][$this->name] != $data[static::$csrfInput]){
|
||||
$this->error('Formulaire expiré');
|
||||
return false;
|
||||
}
|
||||
$valid = true;
|
||||
foreach($this->elements as $element){
|
||||
$value = isset($data[$element->name()]) ? $data[$element->name()] : null;
|
||||
$return = $element->valid($value);
|
||||
if($return !== true){
|
||||
$this->error('Le champ '.$element->name().' est '.$return.'.', false);
|
||||
$this->errors[] = 'Le champ '.$element->name().' est '.$return.'.';
|
||||
$valid = false;
|
||||
}else{
|
||||
$element->value($value);
|
||||
}
|
||||
}
|
||||
if($valid)
|
||||
unset($_SESSION[static::$csrfSession][$this->name]);
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
public function error(string $error, bool $reset = true){
|
||||
$this->errors[] = $error;
|
||||
if($reset)
|
||||
$this->resetCsrf();
|
||||
}
|
||||
|
||||
public function name() : string{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function _start(string $more = '', string $method = 'post', string $url = null) : string{
|
||||
public function start(string $more = '', string $method = 'post', string $url = null) : string{
|
||||
if(!in_array($method, array('post', 'get')))
|
||||
$method = 'post';
|
||||
|
||||
|
@ -87,7 +59,6 @@ class Form {
|
|||
$this->url = $url;
|
||||
}
|
||||
$html = '<form method="'.$method.'" '.(isset($url) ? 'action="'.$url.'" ' : '').$more.'>';
|
||||
$html .= '<input type="hidden" name="'.static::$csrfInput.'" value="'.$this->csrfToken.'">';
|
||||
$html .= "
|
||||
<script type=\"text/javascript\">
|
||||
function SelectOther(source, other){
|
||||
|
@ -112,11 +83,11 @@ function SelectOther(source, other){
|
|||
return $html;
|
||||
}
|
||||
|
||||
public function _end(string $more = '') : string{
|
||||
public function end(string $more = '') : string{
|
||||
return '</form '.$more.'>';
|
||||
}
|
||||
|
||||
public function _errors(string $more = '') : string{
|
||||
public function errors(string $more = '') : string{
|
||||
if(empty($this->errors))
|
||||
return '';
|
||||
|
||||
|
@ -127,11 +98,11 @@ function SelectOther(source, other){
|
|||
return $html.'</div>';
|
||||
}
|
||||
|
||||
public function _submit(string $name = null, string $more = '') : string{
|
||||
public function submit(string $name = null, string $more = '') : string{
|
||||
return '<input type="submit" '.(isset($name) ? 'value="'.$name.'" ' : '').$more.'>';
|
||||
}
|
||||
|
||||
function _input(string $name, bool $add = true) : Element{
|
||||
function input(string $name, bool $add = true) : Element{
|
||||
if($add == false)
|
||||
return new Input($name);
|
||||
|
||||
|
@ -145,7 +116,7 @@ function SelectOther(source, other){
|
|||
return $input;
|
||||
}
|
||||
|
||||
function _select(string $name, bool $add = true) : Element{
|
||||
function select(string $name, bool $add = true) : Element{
|
||||
if($add == false)
|
||||
return new Select($name);
|
||||
if($this->set == true){
|
||||
|
@ -158,7 +129,7 @@ function SelectOther(source, other){
|
|||
return $input;
|
||||
}
|
||||
|
||||
function _textarea(string $name) : Element{
|
||||
function textarea(string $name) : Element{
|
||||
if($this->set == true){
|
||||
$input = $this->get($name);
|
||||
if(isset($input))
|
||||
|
@ -174,7 +145,7 @@ function SelectOther(source, other){
|
|||
$this->elements[] = $thing;
|
||||
}
|
||||
|
||||
public function get(string $name) : Element{
|
||||
public function get(string $name) : ?Element{
|
||||
foreach($this->elements as $element){
|
||||
if($element->name() == $name)
|
||||
return $element;
|
||||
|
|
110
src/Input.php
110
src/Input.php
|
@ -28,46 +28,6 @@ class Input extends Element{
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function number(): Input{
|
||||
$this->data['type'] = 'number';
|
||||
$this->data['number'] = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function date(): Input{
|
||||
$this->data['type'] = 'date';
|
||||
$this->data['date'] = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function time(): Input{
|
||||
$this->data['type'] = 'time';
|
||||
$this->data['time'] = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function min(string $value) : Input{
|
||||
$this->data['min'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function max(string $value) : Input{
|
||||
$this->data['max'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function password(bool $complexity = false): Input{
|
||||
$this->data['type'] = 'password';
|
||||
$this->data['password'] = true;
|
||||
if($complexity){
|
||||
$regex = '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W).{8,}$';
|
||||
$this->data['pattern'] = $regex;
|
||||
$this->data['regex'] = $regex;
|
||||
$this->data['title'] = 'Mot de passe trop simple';
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function minlength(int $value) : Input{
|
||||
$this->data['minlength'] = $value;
|
||||
return $this;
|
||||
|
@ -81,7 +41,7 @@ class Input extends Element{
|
|||
public function alpha(string $value = '') : Input{
|
||||
$this->data['type'] = 'text';
|
||||
$this->data['title'] = 'Alphabétique';
|
||||
$this->data['alpha'] = $value; //TODO: add parttern
|
||||
$this->data['alpha'] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -104,67 +64,45 @@ class Input extends Element{
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function valid($data)/*: bool|string*/{
|
||||
public function valid(mixde $data)/*: bool|string*/{
|
||||
$parent = parent::valid($data);
|
||||
|
||||
if($parent !== true || !isset($data))
|
||||
return $parent;
|
||||
|
||||
if(!empty($data)){
|
||||
if(isset($this->data['phone']) && $this->data['phone'] == true && !preg_match("#^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$#", $data))
|
||||
return 'incorrect';
|
||||
|
||||
if(isset($this->data['number']) && $this->data['number'] == true && !ctype_digit($data))
|
||||
return 'non numérique';
|
||||
|
||||
if(isset($this->data['date']) && $this->data['date'] == true){
|
||||
$d = \DateTime::createFromFormat('Y-m-d', $data);
|
||||
if(!$d || $d->format('Y-m-d') != $data)
|
||||
if(isset($this->data['phone'])){
|
||||
if($this->data['phone'] == true && !preg_match("#^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$#", $data))
|
||||
return 'incorrect';
|
||||
}else if(isset($this->data['email'])){
|
||||
if($this->data['email'] == true && !filter_var($data, FILTER_VALIDATE_EMAIL))
|
||||
return 'incorrect';
|
||||
}else if(isset($this->data['minlength'])){
|
||||
if(strlen($data) < $this->data['minlength'])
|
||||
return 'trop court';
|
||||
}else if(isset($this->data['maxlength'])){
|
||||
if(strlen($data) > $this->data['maxlength'])
|
||||
return 'trop long';
|
||||
}else if(isset($this->data['alpha'])){
|
||||
if(!preg_match('#^[\p{L}'.$this->data['alpha'].']*$#', $data))
|
||||
return 'non alphabétique';
|
||||
}else if(isset($this->data['alphanum'])){
|
||||
if(!preg_match('#^[\p{L}\p{N}'.$this->data['alphanum'].']*$#', $data))
|
||||
return 'non alphanumérique';
|
||||
}else if(isset($this->data['regex'])){
|
||||
if(!preg_match('#'.$this->data['regex'].'#', $data))
|
||||
return 'incorrect';
|
||||
}
|
||||
if(isset($this->data['time']) && $this->data['time'] == true){
|
||||
$t = \DateTime::createFromFormat('H:i', $data);
|
||||
if(!$t || $t->format('H:i') != $data)
|
||||
return 'incorrect';
|
||||
}
|
||||
if(isset($this->data['min']) && $data < $this->data['min'])
|
||||
return 'trop petit';
|
||||
|
||||
if(isset($this->data['max']) && $data > $this->data['max'])
|
||||
return 'trop grand';
|
||||
|
||||
if(isset($this->data['email']) && $this->data['email'] == true && !filter_var($data, FILTER_VALIDATE_EMAIL))
|
||||
return 'incorrect';
|
||||
|
||||
if(isset($this->data['minlength']) && strlen($data) < $this->data['minlength'])
|
||||
return 'trop court';
|
||||
|
||||
if(isset($this->data['maxlength']) && strlen($data) > $this->data['maxlength'])
|
||||
return 'trop long';
|
||||
|
||||
if(isset($this->data['alpha']) && !preg_match('#^[\p{L}\p{M}'.$this->data['alpha'].']*$#', $data))
|
||||
return 'non alphabétique';
|
||||
|
||||
if(isset($this->data['alphanum']) && !preg_match('#^[\p{L}\p{M}\p{N}'.$this->data['alphanum'].']*$#', $data))
|
||||
return 'non alphanumérique';
|
||||
|
||||
if(isset($this->data['regex']) && !preg_match('#'.$this->data['regex'].'#', $data))
|
||||
return 'incorrect';
|
||||
|
||||
}
|
||||
return $parent;
|
||||
}
|
||||
|
||||
public function html(string $more = '') : string{
|
||||
return $this->htmlLabel().
|
||||
'<input name="'.$this->data['name'].'" '.
|
||||
'id="'.$this->getId().'" '.
|
||||
(isset($this->data['value']) && !(isset($this->data['password']) && $this->data['password'] == true) ? 'value="'.$this->data['value'].'" ' : '').
|
||||
return '<input name="'.$this->data['name'].'" '.
|
||||
(isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
|
||||
(isset($this->data['type']) ? 'type="'.$this->data['type'].'" ' : '').
|
||||
(isset($this->data['title']) ? 'title="'.$this->data['title'].'" ' : '').
|
||||
(isset($this->data['pattern']) ? 'pattern="'.$this->data['pattern'].'" ' : '').
|
||||
(isset($this->data['min']) ? 'min="'.$this->data['min'].'" ' : '').
|
||||
(isset($this->data['max']) ? 'max="'.$this->data['max'].'" ' : '').
|
||||
(isset($this->data['minlength']) ? 'minlength="'.$this->data['minlength'].'" ' : '').
|
||||
(isset($this->data['maxlength']) ? 'maxlength="'.$this->data['maxlength'].'" ' : '').
|
||||
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
|
||||
|
|
|
@ -33,7 +33,7 @@ class Select extends Element{
|
|||
}
|
||||
|
||||
|
||||
public function valid($data)/*: bool|string*/{
|
||||
public function valid(mixed $data)/*: bool|string*/{
|
||||
$parent = parent::valid($data);
|
||||
if($parent !== true || !isset($data))
|
||||
return $parent;
|
||||
|
@ -62,9 +62,7 @@ class Select extends Element{
|
|||
$options .= $option['more'].'>'.$option['text'].'</option>';
|
||||
}
|
||||
|
||||
$html = $this->htmlLabel().
|
||||
'<select name="'.$this->data['name'].'" '.
|
||||
'id="'.$this->getId().'" ';
|
||||
$html = '<select name="'.$this->data['name'].'" ';
|
||||
$inputmore = '';
|
||||
if(isset($this->data['other.text'])){
|
||||
$options .= '<option value="'.$this->data['other.text'].'" '.(isset($this->data['value']) && $selected == false ? 'selected="selected" ' : '').'>'.$this->data['other.text'].'</option>';
|
||||
|
|
|
@ -3,15 +3,14 @@
|
|||
namespace Krutush\Form;
|
||||
|
||||
class TextArea extends Element{
|
||||
public function valid($data)/*: bool|string*/{
|
||||
public function valid(mixed $data)/*: bool|string*/{
|
||||
return parent::valid($data);
|
||||
}
|
||||
|
||||
public function html(string $more = '') : string{
|
||||
return $this->htmlLabel().
|
||||
'<textarea name="'.$this->data['name'].'" '.
|
||||
'id="'.$this->getId().'" '.
|
||||
return '<textarea name="'.$this->data['name'].'" '.
|
||||
(isset($this->data['value']) ? 'value="'.$this->data['value'].'" ' : '').
|
||||
(isset($this->data['required']) && $this->data['required'] == true ? 'required ' : '').
|
||||
$more.'>'.(isset($this->data['value']) ? $this->data['value'] : '').'</textarea>';
|
||||
$more.'></textarea>';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue