Completed
Push — master ( 1a0349...c60a32 )
by Ben
09:05
created
src/Laravel/File.php 1 patch
Indentation   +121 added lines, -121 removed lines patch added patch discarded remove patch
@@ -7,128 +7,128 @@
 block discarded – undo
7 7
 
8 8
 class File
9 9
 {
10
-	private static $mimes = array(
11
-		'ai'    => 'application/postscript',
12
-		'aif'   => 'audio/x-aiff',
13
-		'aifc'  => 'audio/x-aiff',
14
-		'aiff'  => 'audio/x-aiff',
15
-		'avi'   => 'video/x-msvideo',
16
-		'bin'   => 'application/macbinary',
17
-		'bmp'   => 'image/bmp',
18
-		'class' => 'application/octet-stream',
19
-		'cpt'   => 'application/mac-compactpro',
20
-		'css'   => 'text/css',
21
-		'csv'   => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'),
22
-		'dcr'   => 'application/x-director',
23
-		'dir'   => 'application/x-director',
24
-		'dll'   => 'application/octet-stream',
25
-		'dms'   => 'application/octet-stream',
26
-		'doc'   => 'application/msword',
27
-		'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
28
-		'dvi'   => 'application/x-dvi',
29
-		'dxr'   => 'application/x-director',
30
-		'eml'   => 'message/rfc822',
31
-		'eps'   => 'application/postscript',
32
-		'exe'   => array('application/octet-stream', 'application/x-msdownload'),
33
-		'gif'   => 'image/gif',
34
-		'gtar'  => 'application/x-gtar',
35
-		'gz'    => 'application/x-gzip',
36
-		'hqx'   => 'application/mac-binhex40',
37
-		'htm'   => 'text/html',
38
-		'html'  => 'text/html',
39
-		'jpe'   => array('image/jpeg', 'image/pjpeg'),
40
-		'jpeg'  => array('image/jpeg', 'image/pjpeg'),
41
-		'jpg'   => array('image/jpeg', 'image/pjpeg'),
42
-		'js'    => 'application/x-javascript',
43
-		'json'  => array('application/json', 'text/json'),
44
-		'lha'   => 'application/octet-stream',
45
-		'log'   => array('text/plain', 'text/x-log'),
46
-		'lzh'   => 'application/octet-stream',
47
-		'mid'   => 'audio/midi',
48
-		'midi'  => 'audio/midi',
49
-		'mif'   => 'application/vnd.mif',
50
-		'mov'   => 'video/quicktime',
51
-		'movie' => 'video/x-sgi-movie',
52
-		'mp2'   => 'audio/mpeg',
53
-		'mp3'   => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
54
-		'mpe'   => 'video/mpeg',
55
-		'mpeg'  => 'video/mpeg',
56
-		'mpg'   => 'video/mpeg',
57
-		'mpga'  => 'audio/mpeg',
58
-		'oda'   => 'application/oda',
59
-		'odp'   => 'application/vnd.oasis.opendocument.presentation',
60
-		'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
61
-		'odt'   => 'application/vnd.oasis.opendocument.text',
62
-		'pdf'   => array('application/pdf', 'application/x-download'),
63
-		'php'   => array('application/x-httpd-php', 'text/x-php'),
64
-		'php3'  => 'application/x-httpd-php',
65
-		'php4'  => 'application/x-httpd-php',
66
-		'phps'  => 'application/x-httpd-php-source',
67
-		'phtml' => 'application/x-httpd-php',
68
-		'png'   => 'image/png',
69
-		'pps'   => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'),
70
-		'ppsx'  => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
71
-		'ppt'   => array('application/vnd.ms-powerpoint', 'application/powerpoint'),
72
-		'pptx'  => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
73
-		'ps'    => 'application/postscript',
74
-		'psd'   => 'application/x-photoshop',
75
-		'qt'    => 'video/quicktime',
76
-		'ra'    => 'audio/x-realaudio',
77
-		'ram'   => 'audio/x-pn-realaudio',
78
-		'rm'    => 'audio/x-pn-realaudio',
79
-		'rpm'   => 'audio/x-pn-realaudio-plugin',
80
-		'rtf'   => array('application/rtf', 'text/rtf'),
81
-		'rtx'   => 'text/richtext',
82
-		'rv'    => 'video/vnd.rn-realvideo',
83
-		'sea'   => 'application/octet-stream',
84
-		'shtml' => 'text/html',
85
-		'sit'   => 'application/x-stuffit',
86
-		'smi'   => 'application/smil',
87
-		'smil'  => 'application/smil',
88
-		'so'    => 'application/octet-stream',
89
-		'swf'   => 'application/x-shockwave-flash',
90
-		'tar'   => 'application/x-tar',
91
-		'text'  => 'text/plain',
92
-		'tgz'   => array('application/x-tar', 'application/x-gzip-compressed'),
93
-		'tif'   => 'image/tiff',
94
-		'tiff'  => 'image/tiff',
95
-		'txt'   => 'text/plain',
96
-		'wav'   => 'audio/x-wav',
97
-		'wbxml' => 'application/wbxml',
98
-		'wmlc'  => 'application/wmlc',
99
-		'word'  => array('application/msword', 'application/octet-stream'),
100
-		'xht'   => 'application/xhtml+xml',
101
-		'xhtml' => 'application/xhtml+xml',
102
-		'xl'    => 'application/excel',
103
-		'xls'   => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'),
104
-		'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
105
-		'xml'   => 'text/xml',
106
-		'xsl'   => 'text/xml',
107
-		'zip'   => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
108
-	);
10
+    private static $mimes = array(
11
+        'ai'    => 'application/postscript',
12
+        'aif'   => 'audio/x-aiff',
13
+        'aifc'  => 'audio/x-aiff',
14
+        'aiff'  => 'audio/x-aiff',
15
+        'avi'   => 'video/x-msvideo',
16
+        'bin'   => 'application/macbinary',
17
+        'bmp'   => 'image/bmp',
18
+        'class' => 'application/octet-stream',
19
+        'cpt'   => 'application/mac-compactpro',
20
+        'css'   => 'text/css',
21
+        'csv'   => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'),
22
+        'dcr'   => 'application/x-director',
23
+        'dir'   => 'application/x-director',
24
+        'dll'   => 'application/octet-stream',
25
+        'dms'   => 'application/octet-stream',
26
+        'doc'   => 'application/msword',
27
+        'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
28
+        'dvi'   => 'application/x-dvi',
29
+        'dxr'   => 'application/x-director',
30
+        'eml'   => 'message/rfc822',
31
+        'eps'   => 'application/postscript',
32
+        'exe'   => array('application/octet-stream', 'application/x-msdownload'),
33
+        'gif'   => 'image/gif',
34
+        'gtar'  => 'application/x-gtar',
35
+        'gz'    => 'application/x-gzip',
36
+        'hqx'   => 'application/mac-binhex40',
37
+        'htm'   => 'text/html',
38
+        'html'  => 'text/html',
39
+        'jpe'   => array('image/jpeg', 'image/pjpeg'),
40
+        'jpeg'  => array('image/jpeg', 'image/pjpeg'),
41
+        'jpg'   => array('image/jpeg', 'image/pjpeg'),
42
+        'js'    => 'application/x-javascript',
43
+        'json'  => array('application/json', 'text/json'),
44
+        'lha'   => 'application/octet-stream',
45
+        'log'   => array('text/plain', 'text/x-log'),
46
+        'lzh'   => 'application/octet-stream',
47
+        'mid'   => 'audio/midi',
48
+        'midi'  => 'audio/midi',
49
+        'mif'   => 'application/vnd.mif',
50
+        'mov'   => 'video/quicktime',
51
+        'movie' => 'video/x-sgi-movie',
52
+        'mp2'   => 'audio/mpeg',
53
+        'mp3'   => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'),
54
+        'mpe'   => 'video/mpeg',
55
+        'mpeg'  => 'video/mpeg',
56
+        'mpg'   => 'video/mpeg',
57
+        'mpga'  => 'audio/mpeg',
58
+        'oda'   => 'application/oda',
59
+        'odp'   => 'application/vnd.oasis.opendocument.presentation',
60
+        'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
61
+        'odt'   => 'application/vnd.oasis.opendocument.text',
62
+        'pdf'   => array('application/pdf', 'application/x-download'),
63
+        'php'   => array('application/x-httpd-php', 'text/x-php'),
64
+        'php3'  => 'application/x-httpd-php',
65
+        'php4'  => 'application/x-httpd-php',
66
+        'phps'  => 'application/x-httpd-php-source',
67
+        'phtml' => 'application/x-httpd-php',
68
+        'png'   => 'image/png',
69
+        'pps'   => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'),
70
+        'ppsx'  => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
71
+        'ppt'   => array('application/vnd.ms-powerpoint', 'application/powerpoint'),
72
+        'pptx'  => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
73
+        'ps'    => 'application/postscript',
74
+        'psd'   => 'application/x-photoshop',
75
+        'qt'    => 'video/quicktime',
76
+        'ra'    => 'audio/x-realaudio',
77
+        'ram'   => 'audio/x-pn-realaudio',
78
+        'rm'    => 'audio/x-pn-realaudio',
79
+        'rpm'   => 'audio/x-pn-realaudio-plugin',
80
+        'rtf'   => array('application/rtf', 'text/rtf'),
81
+        'rtx'   => 'text/richtext',
82
+        'rv'    => 'video/vnd.rn-realvideo',
83
+        'sea'   => 'application/octet-stream',
84
+        'shtml' => 'text/html',
85
+        'sit'   => 'application/x-stuffit',
86
+        'smi'   => 'application/smil',
87
+        'smil'  => 'application/smil',
88
+        'so'    => 'application/octet-stream',
89
+        'swf'   => 'application/x-shockwave-flash',
90
+        'tar'   => 'application/x-tar',
91
+        'text'  => 'text/plain',
92
+        'tgz'   => array('application/x-tar', 'application/x-gzip-compressed'),
93
+        'tif'   => 'image/tiff',
94
+        'tiff'  => 'image/tiff',
95
+        'txt'   => 'text/plain',
96
+        'wav'   => 'audio/x-wav',
97
+        'wbxml' => 'application/wbxml',
98
+        'wmlc'  => 'application/wmlc',
99
+        'word'  => array('application/msword', 'application/octet-stream'),
100
+        'xht'   => 'application/xhtml+xml',
101
+        'xhtml' => 'application/xhtml+xml',
102
+        'xl'    => 'application/excel',
103
+        'xls'   => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'),
104
+        'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
105
+        'xml'   => 'text/xml',
106
+        'xsl'   => 'text/xml',
107
+        'zip'   => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
108
+    );
109 109
 
110
-	/**
111
-	 * Get a file MIME type by extension.
112
-	 * <code>
113
-	 *    // Determine the MIME type for the .tar extension
114
-	 *    $mime = File::mime('tar');
115
-	 *    // Return a default value if the MIME can't be determined
116
-	 *    $mime = File::mime('ext', 'application/octet-stream');
117
-	 * </code>
118
-	 *
119
-	 * @param  string $extension
120
-	 * @param  string $default
121
-	 *
122
-	 * @return string
123
-	 */
124
-	public static function mime($extension, $default = 'application/octet-stream')
125
-	{
126
-		$mimes = self::$mimes;
110
+    /**
111
+     * Get a file MIME type by extension.
112
+     * <code>
113
+     *    // Determine the MIME type for the .tar extension
114
+     *    $mime = File::mime('tar');
115
+     *    // Return a default value if the MIME can't be determined
116
+     *    $mime = File::mime('ext', 'application/octet-stream');
117
+     * </code>
118
+     *
119
+     * @param  string $extension
120
+     * @param  string $default
121
+     *
122
+     * @return string
123
+     */
124
+    public static function mime($extension, $default = 'application/octet-stream')
125
+    {
126
+        $mimes = self::$mimes;
127 127
 
128
-		if (!array_key_exists($extension, $mimes)) {
129
-			return $default;
130
-		}
128
+        if (!array_key_exists($extension, $mimes)) {
129
+            return $default;
130
+        }
131 131
 
132
-		return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
133
-	}
132
+        return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
133
+    }
134 134
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/File.php 1 patch
Indentation   +123 added lines, -124 removed lines patch added patch discarded remove patch
@@ -12,128 +12,127 @@
 block discarded – undo
12 12
 class File extends Field
13 13
 {
14 14
 
15
-	/**
16
-	 * The maximum file size
17
-	 *
18
-	 * @var integer
19
-	 */
20
-	private $maxSize;
21
-
22
-	/**
23
-	 * An array of mime groups to use as shortcuts
24
-	 *
25
-	 * @var array
26
-	 */
27
-	private $mimeGroups = array('audio', 'video', 'image');
28
-
29
-	/**
30
-	 * A list of properties to be injected in the attributes
31
-	 *
32
-	 * @var array
33
-	 */
34
-	protected $injectedProperties = array('type', 'name');
35
-
36
-	////////////////////////////////////////////////////////////////////
37
-	/////////////////////////// CORE METHODS ///////////////////////////
38
-	////////////////////////////////////////////////////////////////////
39
-
40
-	/**
41
-	 * Easier arguments order for hidden fields
42
-	 *
43
-	 * @param Container $app        The Illuminate Container
44
-	 * @param string    $type       file
45
-	 * @param string    $name       Field name
46
-	 * @param string    $label      Its label
47
-	 * @param string    $value      Its value
48
-	 * @param array     $attributes Attributes
49
-	 */
50
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
51
-	{
52
-		// Multiple files field
53
-		if ($type == 'files') {
54
-			$attributes['multiple'] = 'true';
55
-			$type                   = 'file';
56
-			$name                   = $name.'[]';
57
-		}
58
-
59
-		parent::__construct($app, $type, $name, $label, $value, $attributes);
60
-	}
61
-
62
-	/**
63
-	 * Prints out the current tag
64
-	 *
65
-	 * @return string An input file tag
66
-	 */
67
-	public function render()
68
-	{
69
-		// Maximum file size
70
-		$hidden = $this->maxSize
71
-			? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize)
72
-			: null;
73
-
74
-		return $hidden.parent::render();
75
-	}
76
-
77
-	////////////////////////////////////////////////////////////////////
78
-	////////////////////////// FIELD METHODS ///////////////////////////
79
-	////////////////////////////////////////////////////////////////////
80
-
81
-	/**
82
-	 * Set which types of files are accepted by the file input
83
-
84
-	 */
85
-	public function accept()
86
-	{
87
-		$mimes = array();
88
-
89
-		// Transform all extensions/groups to mime types
90
-		foreach (func_get_args() as $mime) {
91
-
92
-			// Shortcuts and extensions
93
-			if (in_array($mime, $this->mimeGroups)) {
94
-				$mime .= '/*';
95
-			}
96
-			$mime = LaravelFile::mime($mime, $mime);
97
-
98
-			$mimes[] = $mime;
99
-		}
100
-
101
-		// Add accept attribute by concatenating the mimes
102
-		$this->attributes['accept'] = implode(',', $mimes);
103
-
104
-		return $this;
105
-	}
106
-
107
-	/**
108
-	 * Set a maximum size for files
109
-	 *
110
-	 * @param integer $size  A maximum size
111
-	 * @param string  $units The size's unit
112
-	 */
113
-	public function max($size, $units = 'KB')
114
-	{
115
-		// Bytes or bits ?
116
-		$unit = substr($units, -1);
117
-		$base = 1024;
118
-		if ($unit == 'b') {
119
-			$size = $size / 8;
120
-		}
121
-
122
-		// Convert
123
-		switch ($units[0]) {
124
-			case 'K':
125
-				$size = $size * $base;
126
-				break;
127
-			case 'M':
128
-				$size = $size * pow($base, 2);
129
-				break;
130
-			case 'G':
131
-				$size = $size * pow($base, 3);
132
-				break;
133
-		}
134
-
135
-		$this->maxSize = (int) $size;
136
-
137
-		return $this;
138
-	}
15
+    /**
16
+     * The maximum file size
17
+     *
18
+     * @var integer
19
+     */
20
+    private $maxSize;
21
+
22
+    /**
23
+     * An array of mime groups to use as shortcuts
24
+     *
25
+     * @var array
26
+     */
27
+    private $mimeGroups = array('audio', 'video', 'image');
28
+
29
+    /**
30
+     * A list of properties to be injected in the attributes
31
+     *
32
+     * @var array
33
+     */
34
+    protected $injectedProperties = array('type', 'name');
35
+
36
+    ////////////////////////////////////////////////////////////////////
37
+    /////////////////////////// CORE METHODS ///////////////////////////
38
+    ////////////////////////////////////////////////////////////////////
39
+
40
+    /**
41
+     * Easier arguments order for hidden fields
42
+     *
43
+     * @param Container $app        The Illuminate Container
44
+     * @param string    $type       file
45
+     * @param string    $name       Field name
46
+     * @param string    $label      Its label
47
+     * @param string    $value      Its value
48
+     * @param array     $attributes Attributes
49
+     */
50
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
51
+    {
52
+        // Multiple files field
53
+        if ($type == 'files') {
54
+            $attributes['multiple'] = 'true';
55
+            $type                   = 'file';
56
+            $name                   = $name.'[]';
57
+        }
58
+
59
+        parent::__construct($app, $type, $name, $label, $value, $attributes);
60
+    }
61
+
62
+    /**
63
+     * Prints out the current tag
64
+     *
65
+     * @return string An input file tag
66
+     */
67
+    public function render()
68
+    {
69
+        // Maximum file size
70
+        $hidden = $this->maxSize
71
+            ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize)
72
+            : null;
73
+
74
+        return $hidden.parent::render();
75
+    }
76
+
77
+    ////////////////////////////////////////////////////////////////////
78
+    ////////////////////////// FIELD METHODS ///////////////////////////
79
+    ////////////////////////////////////////////////////////////////////
80
+
81
+    /**
82
+     * Set which types of files are accepted by the file input
83
+     */
84
+    public function accept()
85
+    {
86
+        $mimes = array();
87
+
88
+        // Transform all extensions/groups to mime types
89
+        foreach (func_get_args() as $mime) {
90
+
91
+            // Shortcuts and extensions
92
+            if (in_array($mime, $this->mimeGroups)) {
93
+                $mime .= '/*';
94
+            }
95
+            $mime = LaravelFile::mime($mime, $mime);
96
+
97
+            $mimes[] = $mime;
98
+        }
99
+
100
+        // Add accept attribute by concatenating the mimes
101
+        $this->attributes['accept'] = implode(',', $mimes);
102
+
103
+        return $this;
104
+    }
105
+
106
+    /**
107
+     * Set a maximum size for files
108
+     *
109
+     * @param integer $size  A maximum size
110
+     * @param string  $units The size's unit
111
+     */
112
+    public function max($size, $units = 'KB')
113
+    {
114
+        // Bytes or bits ?
115
+        $unit = substr($units, -1);
116
+        $base = 1024;
117
+        if ($unit == 'b') {
118
+            $size = $size / 8;
119
+        }
120
+
121
+        // Convert
122
+        switch ($units[0]) {
123
+            case 'K':
124
+                $size = $size * $base;
125
+                break;
126
+            case 'M':
127
+                $size = $size * pow($base, 2);
128
+                break;
129
+            case 'G':
130
+                $size = $size * pow($base, 3);
131
+                break;
132
+        }
133
+
134
+        $this->maxSize = (int) $size;
135
+
136
+        return $this;
137
+    }
139 138
 }
Please login to merge, or discard this patch.
src/Former/Form/Elements.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -12,106 +12,106 @@
 block discarded – undo
12 12
  */
13 13
 class Elements
14 14
 {
15
-	/**
16
-	 * The Container
17
-	 *
18
-	 * @var Container
19
-	 */
20
-	protected $app;
21
-
22
-	/**
23
-	 * The Session instance
24
-	 *
25
-	 * @var Session
26
-	 */
27
-	protected $session;
28
-
29
-	/**
30
-	 * Build a new Element
31
-	 *
32
-	 * @param Container $app
33
-	 */
34
-	public function __construct(Container $app, $session)
35
-	{
36
-		$this->app     = $app;
37
-		$this->session = $session;
38
-	}
39
-
40
-	/**
41
-	 * Generate a hidden field containing the current CSRF token.
42
-	 *
43
-	 * @return string
44
-	 */
45
-	public function token()
46
-	{
47
-		$csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token();
48
-
49
-		return (string) $this->app['former']->hidden('_token', $csrf);
50
-	}
51
-
52
-	/**
53
-	 * Creates a label tag
54
-	 *
55
-	 * @param  string $label      The label content
56
-	 * @param  string $for        The field the label's for
57
-	 * @param  array  $attributes The label's attributes
58
-	 *
59
-	 * @return Element             A <label> tag
60
-	 */
61
-	public function label($label, $for = null, $attributes = array())
62
-	{
63
-		if (!$label instanceof Htmlable) {
64
-			$oldLabel = (string) $label;
65
-			$label    = Helpers::translate($oldLabel);
66
-
67
-			// If there was no change to the label,
68
-			// then a Laravel translation did not occur
69
-			if (lcfirst($label) == $oldLabel) {
70
-				$label = str_replace('_', ' ', $label);
71
-			}
72
-		} else {
73
-			$label = (string) $label->toHtml();
74
-		}
75
-
76
-		$attributes['for']             = $for;
77
-		$this->app['former']->labels[] = $for;
78
-
79
-		return Element::create('label', $label, $attributes);
80
-	}
81
-
82
-	/**
83
-	 * Creates a form legend
84
-	 *
85
-	 * @param  string $legend     The text
86
-	 * @param  array  $attributes Its attributes
87
-	 *
88
-	 * @return Element             A <legend> tag
89
-	 */
90
-	public function legend($legend, $attributes = array())
91
-	{
92
-		$legend = Helpers::translate($legend);
93
-
94
-		return Element::create('legend', $legend, $attributes);
95
-	}
96
-
97
-	/**
98
-	 * Close a field group
99
-	 *
100
-	 * @return string
101
-	 */
102
-	public function closeGroup()
103
-	{
104
-		$closing = '';
105
-		if (Group::$opened && isset(Group::$openGroup)) {
106
-			$closing = Group::$openGroup->close();
107
-		}
108
-
109
-		// Close custom group
110
-		Group::$opened = false;
111
-
112
-		// Reset custom group reference
113
-		Group::$openGroup = null;
114
-
115
-		return $closing;
116
-	}
15
+    /**
16
+     * The Container
17
+     *
18
+     * @var Container
19
+     */
20
+    protected $app;
21
+
22
+    /**
23
+     * The Session instance
24
+     *
25
+     * @var Session
26
+     */
27
+    protected $session;
28
+
29
+    /**
30
+     * Build a new Element
31
+     *
32
+     * @param Container $app
33
+     */
34
+    public function __construct(Container $app, $session)
35
+    {
36
+        $this->app     = $app;
37
+        $this->session = $session;
38
+    }
39
+
40
+    /**
41
+     * Generate a hidden field containing the current CSRF token.
42
+     *
43
+     * @return string
44
+     */
45
+    public function token()
46
+    {
47
+        $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token();
48
+
49
+        return (string) $this->app['former']->hidden('_token', $csrf);
50
+    }
51
+
52
+    /**
53
+     * Creates a label tag
54
+     *
55
+     * @param  string $label      The label content
56
+     * @param  string $for        The field the label's for
57
+     * @param  array  $attributes The label's attributes
58
+     *
59
+     * @return Element             A <label> tag
60
+     */
61
+    public function label($label, $for = null, $attributes = array())
62
+    {
63
+        if (!$label instanceof Htmlable) {
64
+            $oldLabel = (string) $label;
65
+            $label    = Helpers::translate($oldLabel);
66
+
67
+            // If there was no change to the label,
68
+            // then a Laravel translation did not occur
69
+            if (lcfirst($label) == $oldLabel) {
70
+                $label = str_replace('_', ' ', $label);
71
+            }
72
+        } else {
73
+            $label = (string) $label->toHtml();
74
+        }
75
+
76
+        $attributes['for']             = $for;
77
+        $this->app['former']->labels[] = $for;
78
+
79
+        return Element::create('label', $label, $attributes);
80
+    }
81
+
82
+    /**
83
+     * Creates a form legend
84
+     *
85
+     * @param  string $legend     The text
86
+     * @param  array  $attributes Its attributes
87
+     *
88
+     * @return Element             A <legend> tag
89
+     */
90
+    public function legend($legend, $attributes = array())
91
+    {
92
+        $legend = Helpers::translate($legend);
93
+
94
+        return Element::create('legend', $legend, $attributes);
95
+    }
96
+
97
+    /**
98
+     * Close a field group
99
+     *
100
+     * @return string
101
+     */
102
+    public function closeGroup()
103
+    {
104
+        $closing = '';
105
+        if (Group::$opened && isset(Group::$openGroup)) {
106
+            $closing = Group::$openGroup->close();
107
+        }
108
+
109
+        // Close custom group
110
+        Group::$opened = false;
111
+
112
+        // Reset custom group reference
113
+        Group::$openGroup = null;
114
+
115
+        return $closing;
116
+    }
117 117
 }
Please login to merge, or discard this patch.
src/Former/Traits/Field.php 2 patches
Indentation   +406 added lines, -406 removed lines patch added patch discarded remove patch
@@ -16,264 +16,264 @@  discard block
 block discarded – undo
16 16
  */
17 17
 abstract class Field extends FormerObject implements FieldInterface
18 18
 {
19
-	/**
20
-	 * The IoC Container
21
-	 *
22
-	 * @var Container
23
-	 */
24
-	protected $app;
25
-
26
-	/**
27
-	 * The Form instance
28
-	 *
29
-	 * @var Former\Form
30
-	 */
31
-	protected $form;
32
-
33
-	/**
34
-	 * A label for the field (if not using Bootstrap)
35
-	 *
36
-	 * @var string
37
-	 */
38
-	protected $label;
39
-
40
-	/**
41
-	 * The field's group
42
-	 *
43
-	 * @var Group
44
-	 */
45
-	protected $group;
46
-
47
-	/**
48
-	 * The field's default element
49
-	 *
50
-	 * @var string
51
-	 */
52
-	protected $element = 'input';
53
-
54
-	/**
55
-	 * Whether the Field is self-closing or not
56
-	 *
57
-	 * @var boolean
58
-	 */
59
-	protected $isSelfClosing = true;
60
-
61
-	/**
62
-	 * The field's bind destination
63
-	 *
64
-	 * @var string
65
-	 */
66
-	protected $bind;
67
-
68
-	/**
69
-	 * Get the current framework instance
70
-	 *
71
-	 * @return Framework
72
-	 */
73
-	protected function currentFramework()
74
-	{
75
-		if ($this->app->bound('former.form.framework')) {
76
-			return $this->app['former.form.framework'];
77
-		}
78
-
79
-		return $this->app['former.framework'];
80
-	}
81
-
82
-	////////////////////////////////////////////////////////////////////
83
-	///////////////////////////// INTERFACE ////////////////////////////
84
-	////////////////////////////////////////////////////////////////////
85
-
86
-	/**
87
-	 * Set up a Field instance
88
-	 *
89
-	 * @param string $type A field type
90
-	 */
91
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
92
-	{
93
-		// Set base parameters
94
-		$this->app   = $app;
95
-		$this->type  = $type;
96
-		$this->value = $value;
97
-		$this->setAttributes($attributes);
98
-		$this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null;
99
-
100
-		// Compute and translate label
101
-		$this->automaticLabels($name, $label);
102
-
103
-		// Repopulate field
104
-		if ($type != 'password' && $name !== '_token') {
105
-			$this->value = $this->repopulate();
106
-		}
107
-
108
-		// Apply Live validation rules
109
-		if ($this->app['former']->getOption('live_validation')) {
110
-			$rules = new LiveValidation($this);
111
-			$rules->apply($this->getRules());
112
-		}
113
-
114
-		// Bind the Group class
115
-		$groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group';
116
-		$groupClass = Former::FORMSPACE.$groupClass;
117
-
118
-		$this->group = new $groupClass($this->app, $this->label);
119
-	}
120
-
121
-	/**
122
-	 * Redirect calls to the group if necessary
123
-	 *
124
-	 * @param string $method
125
-	 */
126
-	public function __call($method, $parameters)
127
-	{
128
-		// Translate attributes
129
-		$translatable = $this->app['former']->getOption('translatable', array());
130
-		if (in_array($method, $translatable) and isset($parameters[0])) {
131
-			$parameters[0] = Helpers::translate($parameters[0]);
132
-		}
133
-
134
-		// Redirect calls to the Control Group
135
-		if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) {
136
-			$method = str_replace('onGroup', '', $method);
137
-			$method = lcfirst($method);
138
-
139
-			call_user_func_array(array($this->group, $method), $parameters);
140
-
141
-			return $this;
142
-		}
143
-
144
-		return parent::__call($method, $parameters);
145
-	}
146
-
147
-	/**
148
-	 * Prints out the field, wrapped in its group
149
-	 *
150
-	 * @return string
151
-	 */
152
-	public function wrapAndRender()
153
-	{
154
-		// Dry syntax (hidden fields, plain fields)
155
-		if ($this->isUnwrappable()) {
156
-			$html = $this->render();
157
-			// Control group syntax
158
-		} elseif (Form::hasInstanceOpened()) {
159
-			$html = $this->group->wrapField($this);
160
-			// Classic syntax
161
-		} else {
162
-			$html = $this->currentFramework()->createLabelOf($this);
163
-			$html .= $this->render();
164
-		}
165
-
166
-		return $html;
167
-	}
168
-
169
-	/**
170
-	 * Prints out the field
171
-	 *
172
-	 * @return string
173
-	 */
174
-	public function __toString()
175
-	{
176
-		return $this->wrapAndRender();
177
-	}
178
-
179
-	////////////////////////////////////////////////////////////////////
180
-	////////////////////////// PUBLIC INTERFACE ////////////////////////
181
-	////////////////////////////////////////////////////////////////////
182
-
183
-	/**
184
-	 * Whether the current field is required or not
185
-	 *
186
-	 * @return boolean
187
-	 */
188
-	public function isRequired()
189
-	{
190
-		return isset($this->attributes['required']);
191
-	}
192
-
193
-	/**
194
-	 * Set required live validation attribute
195
-	 *
196
-	 * @param boolean $isRequired
197
-	 * @return $this
198
-	 */
199
-	public function required($isRequired=true)
200
-	{
201
-		if ($isRequired) {
202
-			$this->attributes['required'] = true;
203
-		} else {
204
-			unset($this->attributes['required']);
205
-		}
206
-
207
-		return $this;
208
-	}
209
-
210
-	/**
211
-	 * Check if a field is unwrappable (no label)
212
-	 *
213
-	 * @return boolean
214
-	 */
215
-	public function isUnwrappable()
216
-	{
217
-		return
218
-			($this->form and $this->currentFramework()->is('Nude')) or
219
-			($this->form and $this->isOfType('inline')) or
220
-			$this->isButton() or
221
-			$this->isOfType('hidden') or
222
-			\Former\Form\Group::$opened or
223
-			$this->group and $this->group->isRaw();
224
-	}
225
-
226
-	/**
227
-	 * Check if field is a checkbox or a radio
228
-	 *
229
-	 * @return boolean
230
-	 */
231
-	public function isCheckable()
232
-	{
233
-		return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios');
234
-	}
235
-
236
-	/**
237
-	 * Check if the field is a button
238
-	 *
239
-	 * @return boolean
240
-	 */
241
-	public function isButton()
242
-	{
243
-		return false;
244
-	}
245
-
246
-	/**
247
-	 * Get the rules applied to the current field
248
-	 *
249
-	 * @return array An array of rules
250
-	 */
251
-	public function getRules()
252
-	{
253
-		return $this->app['former']->getRules($this->name);
254
-	}
255
-
256
-	////////////////////////////////////////////////////////////////////
257
-	//////////////////////// SETTERS AND GETTERS ///////////////////////
258
-	////////////////////////////////////////////////////////////////////
259
-
260
-	/**
261
-	 * Apply a Live Validation rule by chaining
262
-	 *
263
-	 * @param string $rule The rule
264
-	 */
265
-	public function rule($rule)
266
-	{
267
-		$parameters = func_get_args();
268
-		array_shift($parameters);
269
-
270
-		$live = new LiveValidation($this);
271
-		$live->apply(array(
272
-			$rule => $parameters,
273
-		));
274
-
275
-		return $this;
276
-	}
19
+    /**
20
+     * The IoC Container
21
+     *
22
+     * @var Container
23
+     */
24
+    protected $app;
25
+
26
+    /**
27
+     * The Form instance
28
+     *
29
+     * @var Former\Form
30
+     */
31
+    protected $form;
32
+
33
+    /**
34
+     * A label for the field (if not using Bootstrap)
35
+     *
36
+     * @var string
37
+     */
38
+    protected $label;
39
+
40
+    /**
41
+     * The field's group
42
+     *
43
+     * @var Group
44
+     */
45
+    protected $group;
46
+
47
+    /**
48
+     * The field's default element
49
+     *
50
+     * @var string
51
+     */
52
+    protected $element = 'input';
53
+
54
+    /**
55
+     * Whether the Field is self-closing or not
56
+     *
57
+     * @var boolean
58
+     */
59
+    protected $isSelfClosing = true;
60
+
61
+    /**
62
+     * The field's bind destination
63
+     *
64
+     * @var string
65
+     */
66
+    protected $bind;
67
+
68
+    /**
69
+     * Get the current framework instance
70
+     *
71
+     * @return Framework
72
+     */
73
+    protected function currentFramework()
74
+    {
75
+        if ($this->app->bound('former.form.framework')) {
76
+            return $this->app['former.form.framework'];
77
+        }
78
+
79
+        return $this->app['former.framework'];
80
+    }
81
+
82
+    ////////////////////////////////////////////////////////////////////
83
+    ///////////////////////////// INTERFACE ////////////////////////////
84
+    ////////////////////////////////////////////////////////////////////
85
+
86
+    /**
87
+     * Set up a Field instance
88
+     *
89
+     * @param string $type A field type
90
+     */
91
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
92
+    {
93
+        // Set base parameters
94
+        $this->app   = $app;
95
+        $this->type  = $type;
96
+        $this->value = $value;
97
+        $this->setAttributes($attributes);
98
+        $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null;
99
+
100
+        // Compute and translate label
101
+        $this->automaticLabels($name, $label);
102
+
103
+        // Repopulate field
104
+        if ($type != 'password' && $name !== '_token') {
105
+            $this->value = $this->repopulate();
106
+        }
107
+
108
+        // Apply Live validation rules
109
+        if ($this->app['former']->getOption('live_validation')) {
110
+            $rules = new LiveValidation($this);
111
+            $rules->apply($this->getRules());
112
+        }
113
+
114
+        // Bind the Group class
115
+        $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group';
116
+        $groupClass = Former::FORMSPACE.$groupClass;
117
+
118
+        $this->group = new $groupClass($this->app, $this->label);
119
+    }
120
+
121
+    /**
122
+     * Redirect calls to the group if necessary
123
+     *
124
+     * @param string $method
125
+     */
126
+    public function __call($method, $parameters)
127
+    {
128
+        // Translate attributes
129
+        $translatable = $this->app['former']->getOption('translatable', array());
130
+        if (in_array($method, $translatable) and isset($parameters[0])) {
131
+            $parameters[0] = Helpers::translate($parameters[0]);
132
+        }
133
+
134
+        // Redirect calls to the Control Group
135
+        if (method_exists($this->group, $method) or Str::startsWith($method, 'onGroup')) {
136
+            $method = str_replace('onGroup', '', $method);
137
+            $method = lcfirst($method);
138
+
139
+            call_user_func_array(array($this->group, $method), $parameters);
140
+
141
+            return $this;
142
+        }
143
+
144
+        return parent::__call($method, $parameters);
145
+    }
146
+
147
+    /**
148
+     * Prints out the field, wrapped in its group
149
+     *
150
+     * @return string
151
+     */
152
+    public function wrapAndRender()
153
+    {
154
+        // Dry syntax (hidden fields, plain fields)
155
+        if ($this->isUnwrappable()) {
156
+            $html = $this->render();
157
+            // Control group syntax
158
+        } elseif (Form::hasInstanceOpened()) {
159
+            $html = $this->group->wrapField($this);
160
+            // Classic syntax
161
+        } else {
162
+            $html = $this->currentFramework()->createLabelOf($this);
163
+            $html .= $this->render();
164
+        }
165
+
166
+        return $html;
167
+    }
168
+
169
+    /**
170
+     * Prints out the field
171
+     *
172
+     * @return string
173
+     */
174
+    public function __toString()
175
+    {
176
+        return $this->wrapAndRender();
177
+    }
178
+
179
+    ////////////////////////////////////////////////////////////////////
180
+    ////////////////////////// PUBLIC INTERFACE ////////////////////////
181
+    ////////////////////////////////////////////////////////////////////
182
+
183
+    /**
184
+     * Whether the current field is required or not
185
+     *
186
+     * @return boolean
187
+     */
188
+    public function isRequired()
189
+    {
190
+        return isset($this->attributes['required']);
191
+    }
192
+
193
+    /**
194
+     * Set required live validation attribute
195
+     *
196
+     * @param boolean $isRequired
197
+     * @return $this
198
+     */
199
+    public function required($isRequired=true)
200
+    {
201
+        if ($isRequired) {
202
+            $this->attributes['required'] = true;
203
+        } else {
204
+            unset($this->attributes['required']);
205
+        }
206
+
207
+        return $this;
208
+    }
209
+
210
+    /**
211
+     * Check if a field is unwrappable (no label)
212
+     *
213
+     * @return boolean
214
+     */
215
+    public function isUnwrappable()
216
+    {
217
+        return
218
+            ($this->form and $this->currentFramework()->is('Nude')) or
219
+            ($this->form and $this->isOfType('inline')) or
220
+            $this->isButton() or
221
+            $this->isOfType('hidden') or
222
+            \Former\Form\Group::$opened or
223
+            $this->group and $this->group->isRaw();
224
+    }
225
+
226
+    /**
227
+     * Check if field is a checkbox or a radio
228
+     *
229
+     * @return boolean
230
+     */
231
+    public function isCheckable()
232
+    {
233
+        return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios');
234
+    }
235
+
236
+    /**
237
+     * Check if the field is a button
238
+     *
239
+     * @return boolean
240
+     */
241
+    public function isButton()
242
+    {
243
+        return false;
244
+    }
245
+
246
+    /**
247
+     * Get the rules applied to the current field
248
+     *
249
+     * @return array An array of rules
250
+     */
251
+    public function getRules()
252
+    {
253
+        return $this->app['former']->getRules($this->name);
254
+    }
255
+
256
+    ////////////////////////////////////////////////////////////////////
257
+    //////////////////////// SETTERS AND GETTERS ///////////////////////
258
+    ////////////////////////////////////////////////////////////////////
259
+
260
+    /**
261
+     * Apply a Live Validation rule by chaining
262
+     *
263
+     * @param string $rule The rule
264
+     */
265
+    public function rule($rule)
266
+    {
267
+        $parameters = func_get_args();
268
+        array_shift($parameters);
269
+
270
+        $live = new LiveValidation($this);
271
+        $live->apply(array(
272
+            $rule => $parameters,
273
+        ));
274
+
275
+        return $this;
276
+    }
277 277
 
278 278
     /**
279 279
      * Apply multiple rules passed as a string.
@@ -295,9 +295,9 @@  discard block
 block discarded – undo
295 295
                 $rulename = substr($rule, 0, $colon) ;
296 296
 
297 297
                 /**
298
-                * Regular expressions may contain commas and should not be divided by str_getcsv.
299
-                * For regular expressions we are just using the complete expression as a parameter.
300
-                */
298
+                 * Regular expressions may contain commas and should not be divided by str_getcsv.
299
+                 * For regular expressions we are just using the complete expression as a parameter.
300
+                 */
301 301
                 if ($rulename !== 'regex') {
302 302
                     $parameters = str_getcsv(substr($rule, $colon + 1));
303 303
                 } else {
@@ -319,149 +319,149 @@  discard block
 block discarded – undo
319 319
         return $this;
320 320
     }
321 321
 
322
-	/**
323
-	 * Adds a label to the group/field
324
-	 *
325
-	 * @param  string $text       A label
326
-	 * @param  array  $attributes The label's attributes
327
-	 *
328
-	 * @return Field              A field
329
-	 */
330
-	public function label($text, $attributes = array())
331
-	{
332
-		// Create the Label element
333
-		$for   = $this->id ?: $this->name;
334
-		$label = $this->app['former']->label($text, $for, $attributes);
335
-
336
-		// Set label
337
-		$this->label = $label;
338
-		if ($this->group) {
339
-			$this->group->setLabel($label);
340
-		}
341
-
342
-		return $this;
343
-	}
344
-
345
-	/**
346
-	 * Set the Field value no matter what
347
-	 *
348
-	 * @param string $value A new value
349
-	 */
350
-	public function forceValue($value)
351
-	{
352
-		$this->value = $value;
353
-
354
-		return $this;
355
-	}
356
-
357
-	/**
358
-	 * Classic setting of attribute, won't overwrite any populate() attempt
359
-	 *
360
-	 * @param  string $value A new value
361
-	 */
362
-	public function value($value)
363
-	{
364
-		// Check if we already have a value stored for this field or in POST data
365
-		$already = $this->repopulate();
366
-
367
-		if (!$already) {
368
-			$this->value = $value;
369
-		}
370
-
371
-		return $this;
372
-	}
373
-
374
-	/**
375
-	 * Change the field's name
376
-	 *
377
-	 * @param  string $name The new name
378
-	 */
379
-	public function name($name)
380
-	{
381
-		$this->name = $name;
382
-
383
-		// Also relink the label to the new name
384
-		$this->label($name);
385
-
386
-		return $this;
387
-	}
388
-
389
-	/**
390
-	 * Get the field's labels
391
-	 *
392
-	 * @return string
393
-	 */
394
-	public function getLabel()
395
-	{
396
-		return $this->label;
397
-	}
398
-
399
-	/**
400
-	 * Change the field's bind destination
401
-	 *
402
-	 * @param $destination
403
-	 */
404
-	public function bind($destination) {
405
-		$this->bind = $destination;
406
-		if ($this->type != 'password') {
407
-			$this->value = $this->repopulate();
408
-		}
409
-
410
-		return $this;
411
-	}
412
-
413
-	////////////////////////////////////////////////////////////////////
414
-	//////////////////////////////// HELPERS ///////////////////////////
415
-	////////////////////////////////////////////////////////////////////
416
-
417
-	/**
418
-	 * Use values stored in Former to populate the current field
419
-	 */
420
-	private function repopulate($fallback = null)
421
-	{
422
-		// Get values from POST, populated, and manually set value
423
-		$post      = $this->app['former']->getPost($this->name);
424
-		$populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator'];
425
-		$populate  = $populator->get($this->bind ?: $this->name);
426
-
427
-		// Assign a priority to each
428
-		if (!is_null($post)) {
429
-			return $post;
430
-		}
431
-		if (!is_null($populate)) {
432
-			return $populate;
433
-		}
434
-
435
-		return $fallback ?: $this->value;
436
-	}
437
-
438
-	/**
439
-	 * Ponders a label and a field name, and tries to get the best out of it
440
-	 *
441
-	 * @param  string $label A label
442
-	 * @param  string $name  A field name
443
-	 *
444
-	 * @return false|null         A label and a field name
445
-	 */
446
-	private function automaticLabels($name, $label)
447
-	{
448
-		// Disabled automatic labels
449
-		if (!$this->app['former']->getOption('automatic_label')) {
450
-			$this->name = $name;
451
-			$this->label($label);
452
-
453
-			return false;
454
-		}
455
-
456
-		// Check for the two possibilities
457
-		if ($label and is_null($name)) {
458
-			$name = Str::slug($label);
459
-		} elseif (is_null($label) and $name) {
460
-			$label = preg_replace('/\[\]$/', '', $name);
461
-		}
462
-
463
-		// Save values
464
-		$this->name = $name;
465
-		$this->label($label);
466
-	}
322
+    /**
323
+     * Adds a label to the group/field
324
+     *
325
+     * @param  string $text       A label
326
+     * @param  array  $attributes The label's attributes
327
+     *
328
+     * @return Field              A field
329
+     */
330
+    public function label($text, $attributes = array())
331
+    {
332
+        // Create the Label element
333
+        $for   = $this->id ?: $this->name;
334
+        $label = $this->app['former']->label($text, $for, $attributes);
335
+
336
+        // Set label
337
+        $this->label = $label;
338
+        if ($this->group) {
339
+            $this->group->setLabel($label);
340
+        }
341
+
342
+        return $this;
343
+    }
344
+
345
+    /**
346
+     * Set the Field value no matter what
347
+     *
348
+     * @param string $value A new value
349
+     */
350
+    public function forceValue($value)
351
+    {
352
+        $this->value = $value;
353
+
354
+        return $this;
355
+    }
356
+
357
+    /**
358
+     * Classic setting of attribute, won't overwrite any populate() attempt
359
+     *
360
+     * @param  string $value A new value
361
+     */
362
+    public function value($value)
363
+    {
364
+        // Check if we already have a value stored for this field or in POST data
365
+        $already = $this->repopulate();
366
+
367
+        if (!$already) {
368
+            $this->value = $value;
369
+        }
370
+
371
+        return $this;
372
+    }
373
+
374
+    /**
375
+     * Change the field's name
376
+     *
377
+     * @param  string $name The new name
378
+     */
379
+    public function name($name)
380
+    {
381
+        $this->name = $name;
382
+
383
+        // Also relink the label to the new name
384
+        $this->label($name);
385
+
386
+        return $this;
387
+    }
388
+
389
+    /**
390
+     * Get the field's labels
391
+     *
392
+     * @return string
393
+     */
394
+    public function getLabel()
395
+    {
396
+        return $this->label;
397
+    }
398
+
399
+    /**
400
+     * Change the field's bind destination
401
+     *
402
+     * @param $destination
403
+     */
404
+    public function bind($destination) {
405
+        $this->bind = $destination;
406
+        if ($this->type != 'password') {
407
+            $this->value = $this->repopulate();
408
+        }
409
+
410
+        return $this;
411
+    }
412
+
413
+    ////////////////////////////////////////////////////////////////////
414
+    //////////////////////////////// HELPERS ///////////////////////////
415
+    ////////////////////////////////////////////////////////////////////
416
+
417
+    /**
418
+     * Use values stored in Former to populate the current field
419
+     */
420
+    private function repopulate($fallback = null)
421
+    {
422
+        // Get values from POST, populated, and manually set value
423
+        $post      = $this->app['former']->getPost($this->name);
424
+        $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator'];
425
+        $populate  = $populator->get($this->bind ?: $this->name);
426
+
427
+        // Assign a priority to each
428
+        if (!is_null($post)) {
429
+            return $post;
430
+        }
431
+        if (!is_null($populate)) {
432
+            return $populate;
433
+        }
434
+
435
+        return $fallback ?: $this->value;
436
+    }
437
+
438
+    /**
439
+     * Ponders a label and a field name, and tries to get the best out of it
440
+     *
441
+     * @param  string $label A label
442
+     * @param  string $name  A field name
443
+     *
444
+     * @return false|null         A label and a field name
445
+     */
446
+    private function automaticLabels($name, $label)
447
+    {
448
+        // Disabled automatic labels
449
+        if (!$this->app['former']->getOption('automatic_label')) {
450
+            $this->name = $name;
451
+            $this->label($label);
452
+
453
+            return false;
454
+        }
455
+
456
+        // Check for the two possibilities
457
+        if ($label and is_null($name)) {
458
+            $name = Str::slug($label);
459
+        } elseif (is_null($label) and $name) {
460
+            $label = preg_replace('/\[\]$/', '', $name);
461
+        }
462
+
463
+        // Save values
464
+        $this->name = $name;
465
+        $this->label($label);
466
+    }
467 467
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
 	 * @param boolean $isRequired
197 197
 	 * @return $this
198 198
 	 */
199
-	public function required($isRequired=true)
199
+	public function required($isRequired = true)
200 200
 	{
201 201
 		if ($isRequired) {
202 202
 			$this->attributes['required'] = true;
@@ -292,7 +292,7 @@  discard block
 block discarded – undo
292 292
 
293 293
             // If we have a rule with a value
294 294
             if (($colon = strpos($rule, ':')) !== false) {
295
-                $rulename = substr($rule, 0, $colon) ;
295
+                $rulename = substr($rule, 0, $colon);
296 296
 
297 297
                 /**
298 298
                 * Regular expressions may contain commas and should not be divided by str_getcsv.
Please login to merge, or discard this patch.
src/Former/Framework/TwitterBootstrap4.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@
 block discarded – undo
139 139
 		$classes = array_intersect($classes, $this->fields);
140 140
 
141 141
 		// Prepend field type
142
-		$classes = array_map(function ($class) {
142
+		$classes = array_map(function($class) {
143 143
 			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
144 144
 		}, $classes);
145 145
 
Please login to merge, or discard this patch.
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -345,7 +345,7 @@
 block discarded – undo
345 345
 	 * @param string $text
346 346
 	 * @param array  $attributes
347 347
 	 *
348
-	 * @return string
348
+	 * @return Element
349 349
 	 */
350 350
 	public function createValidationError($text, $attributes = array())
351 351
 	{
Please login to merge, or discard this patch.
Indentation   +455 added lines, -455 removed lines patch added patch discarded remove patch
@@ -13,459 +13,459 @@
 block discarded – undo
13 13
  */
14 14
 class TwitterBootstrap4 extends Framework implements FrameworkInterface
15 15
 {
16
-	/**
17
-	 * Form types that trigger special styling for this Framework
18
-	 *
19
-	 * @var array
20
-	 */
21
-	protected $availableTypes = array('horizontal', 'vertical', 'inline');
22
-
23
-	/**
24
-	 * The button types available
25
-	 *
26
-	 * @var array
27
-	 */
28
-	private $buttons = array(
29
-		'lg',
30
-		'sm',
31
-		'xs',
32
-		'block',
33
-		'link',
34
-		'primary',
35
-		'secondary',
36
-		'warning',
37
-		'danger',
38
-		'success',
39
-		'info',
40
-		'light',
41
-		'dark',
42
-	);
43
-
44
-	/**
45
-	 * The field sizes available
46
-	 *
47
-	 * @var array
48
-	 */
49
-	private $fields = array(
50
-		'lg',
51
-		'sm',
52
-		// 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
53
-		// 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
54
-		// 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
55
-		// 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
56
-		// 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
57
-		// 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
58
-		// 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
59
-		// 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
60
-	);
61
-
62
-	/**
63
-	 * The field states available
64
-	 *
65
-	 * @var array
66
-	 */
67
-	protected $states = array(
68
-		'is-invalid',
69
-	);
70
-
71
-	/**
72
-	 * The default HTML tag used for icons
73
-	 *
74
-	 * @var string
75
-	 */
76
-	protected $iconTag = 'i';
77
-
78
-	/**
79
-	 * The default set for icon fonts
80
-	 * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome
81
-	 *
82
-	 * @var string
83
-	 */
84
-	protected $iconSet = 'fa';
85
-
86
-	/**
87
-	 * The default prefix icon names
88
-	 * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular
89
-	 *
90
-	 * @var string
91
-	 */
92
-	protected $iconPrefix = 'fa';
93
-
94
-	/**
95
-	 * Create a new TwitterBootstrap instance
96
-	 *
97
-	 * @param \Illuminate\Container\Container $app
98
-	 */
99
-	public function __construct(Container $app)
100
-	{
101
-		$this->app = $app;
102
-		$this->setFrameworkDefaults();
103
-	}
104
-
105
-	////////////////////////////////////////////////////////////////////
106
-	/////////////////////////// FILTER ARRAYS //////////////////////////
107
-	////////////////////////////////////////////////////////////////////
108
-
109
-	/**
110
-	 * Filter buttons classes
111
-	 *
112
-	 * @param  array $classes An array of classes
113
-	 *
114
-	 * @return string[] A filtered array
115
-	 */
116
-	public function filterButtonClasses($classes)
117
-	{
118
-		// Filter classes
119
-		// $classes = array_intersect($classes, $this->buttons);
120
-
121
-		// Prepend button type
122
-		$classes   = $this->prependWith($classes, 'btn-');
123
-		$classes[] = 'btn';
124
-
125
-		return $classes;
126
-	}
127
-
128
-	/**
129
-	 * Filter field classes
130
-	 *
131
-	 * @param  array $classes An array of classes
132
-	 *
133
-	 * @return array A filtered array
134
-	 */
135
-	public function filterFieldClasses($classes)
136
-	{
137
-		// Filter classes
138
-		$classes = array_intersect($classes, $this->fields);
139
-
140
-		// Prepend field type
141
-		$classes = array_map(function ($class) {
142
-			return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
143
-		}, $classes);
144
-
145
-		return $classes;
146
-	}
147
-
148
-	////////////////////////////////////////////////////////////////////
149
-	///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
150
-	////////////////////////////////////////////////////////////////////
151
-
152
-	/**
153
-	 * Framework error state
154
-	 *
155
-	 * @return string
156
-	 */
157
-	public function errorState()
158
-	{
159
-		return 'is-invalid';
160
-	}
161
-
162
-	/**
163
-	 * Returns corresponding inline class of a field
164
-	 *
165
-	 * @param Field $field
166
-	 *
167
-	 * @return string
168
-	 */
169
-	public function getInlineLabelClass($field)
170
-	{
171
-		$inlineClass = parent::getInlineLabelClass($field);
172
-		if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) {
173
-			$inlineClass = 'form-check-label';
174
-		}
175
-
176
-		return $inlineClass;
177
-	}
178
-
179
-	/**
180
-	 * Set the fields width from a label width
181
-	 *
182
-	 * @param array $labelWidths
183
-	 */
184
-	protected function setFieldWidths($labelWidths)
185
-	{
186
-		$labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
187
-
188
-		$viewports = $this->getFrameworkOption('viewports');
189
-		foreach ($labelWidths as $viewport => $columns) {
190
-			if ($viewport) {
191
-				$labelWidthClass .= " col-$viewports[$viewport]-$columns";
192
-				$fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
193
-				$fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
194
-			}
195
-		}
196
-
197
-		$this->labelWidth  = ltrim($labelWidthClass);
198
-		$this->fieldWidth  = ltrim($fieldWidthClass);
199
-		$this->fieldOffset = ltrim($fieldOffsetClass);
200
-	}
201
-
202
-	////////////////////////////////////////////////////////////////////
203
-	///////////////////////////// ADD CLASSES //////////////////////////
204
-	////////////////////////////////////////////////////////////////////
205
-
206
-	/**
207
-	 * Add classes to a field
208
-	 *
209
-	 * @param Field $field
210
-	 * @param array $classes The possible classes to add
211
-	 *
212
-	 * @return Field
213
-	 */
214
-	public function getFieldClasses(Field $field, $classes)
215
-	{
216
-		// Add inline class for checkables
217
-		if ($field->isCheckable()) {
218
-			$classes[] = 'form-check';
219
-
220
-			if (in_array('inline', $classes)) {
221
-				$field->inline();
222
-			}
223
-		}
224
-
225
-		// Filter classes according to field type
226
-		if ($field->isButton()) {
227
-			$classes = $this->filterButtonClasses($classes);
228
-		} else {
229
-			$classes = $this->filterFieldClasses($classes);
230
-		}
231
-
232
-		// Add form-control class for text-type, textarea and select fields
233
-		// As text-type is open-ended we instead exclude those that shouldn't receive the class
234
-		if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
235
-					'file',
236
-					'plaintext',
237
-				)) and !in_array('form-control', $classes)
238
-		) {
239
-			$classes[] = 'form-control';
240
-		}
241
-
242
-		if ($this->app['former']->getErrors($field->getName())) {
243
-			$classes[] = $this->errorState();
244
-		}
245
-
246
-		return $this->addClassesToField($field, $classes);
247
-	}
248
-
249
-	/**
250
-	 * Add group classes
251
-	 *
252
-	 * @return string A list of group classes
253
-	 */
254
-	public function getGroupClasses()
255
-	{
256
-		if ($this->app['former.form']->isOfType('horizontal')) {
257
-			return 'form-group row';
258
-		} else {
259
-			return 'form-group';
260
-		}
261
-	}
262
-
263
-	/**
264
-	 * Add label classes
265
-	 *
266
-	 * @return string[] An array of attributes with the label class
267
-	 */
268
-	public function getLabelClasses()
269
-	{
270
-		if ($this->app['former.form']->isOfType('horizontal')) {
271
-			return array('control-label', $this->labelWidth);
272
-		} elseif ($this->app['former.form']->isOfType('inline')) {
273
-			return array('sr-only');
274
-		} else {
275
-			return array('control-label');
276
-		}
277
-	}
278
-
279
-	/**
280
-	 * Add uneditable field classes
281
-	 *
282
-	 * @return string An array of attributes with the uneditable class
283
-	 */
284
-	public function getUneditableClasses()
285
-	{
286
-		return '';
287
-	}
288
-
289
-	/**
290
-	 * Add plain text field classes
291
-	 *
292
-	 * @return string An array of attributes with the plain text class
293
-	 */
294
-	public function getPlainTextClasses()
295
-	{
296
-		return 'form-control-static';
297
-	}
298
-
299
-	/**
300
-	 * Add form class
301
-	 *
302
-	 * @param  string $type The type of form to add
303
-	 *
304
-	 * @return string|null
305
-	 */
306
-	public function getFormClasses($type)
307
-	{
308
-		return $type ? 'form-'.$type : null;
309
-	}
310
-
311
-	/**
312
-	 * Add actions block class
313
-	 *
314
-	 * @return string|null
315
-	 */
316
-	public function getActionClasses()
317
-	{
318
-		if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
319
-			return 'form-group row';
320
-		}
321
-
322
-		return null;
323
-	}
324
-
325
-	////////////////////////////////////////////////////////////////////
326
-	//////////////////////////// RENDER BLOCKS /////////////////////////
327
-	////////////////////////////////////////////////////////////////////
328
-
329
-	/**
330
-	 * Render an help text
331
-	 *
332
-	 * @param string $text
333
-	 * @param array  $attributes
334
-	 *
335
-	 * @return Element
336
-	 */
337
-	public function createHelp($text, $attributes = array())
338
-	{
339
-		return Element::create('small', $text, $attributes)->addClass('text-muted');
340
-	}
341
-
342
-	/**
343
-	 * Render an validation error text
344
-	 *
345
-	 * @param string $text
346
-	 * @param array  $attributes
347
-	 *
348
-	 * @return string
349
-	 */
350
-	public function createValidationError($text, $attributes = array())
351
-	{
352
-		return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
353
-	}
354
-
355
-	/**
356
-	 * Render an help text
357
-	 *
358
-	 * @param string $text
359
-	 * @param array  $attributes
360
-	 *
361
-	 * @return Element
362
-	 */
363
-	public function createBlockHelp($text, $attributes = array())
364
-	{
365
-		return Element::create('small', $text, $attributes)->addClass('form-text text-muted');
366
-	}
367
-
368
-	/**
369
-	 * Render a disabled field
370
-	 *
371
-	 * @param Field $field
372
-	 *
373
-	 * @return Element
374
-	 */
375
-	public function createDisabledField(Field $field)
376
-	{
377
-		return Element::create('span', $field->getValue(), $field->getAttributes());
378
-	}
379
-
380
-	/**
381
-	 * Render a plain text field
382
-	 *
383
-	 * @param Field $field
384
-	 *
385
-	 * @return Element
386
-	 */
387
-	public function createPlainTextField(Field $field)
388
-	{
389
-		$label = $field->getLabel();
390
-		if ($label) {
391
-			$label->for('');
392
-		}
393
-
394
-		return Element::create('div', $field->getValue(), $field->getAttributes());
395
-	}
396
-
397
-	////////////////////////////////////////////////////////////////////
398
-	//////////////////////////// WRAP BLOCKS ///////////////////////////
399
-	////////////////////////////////////////////////////////////////////
400
-
401
-	/**
402
-	 * Wrap an item to be prepended or appended to the current field
403
-	 *
404
-	 * @return Element A wrapped item
405
-	 */
406
-	public function placeAround($item)
407
-	{
408
-		// Render object
409
-		if (is_object($item) and method_exists($item, '__toString')) {
410
-			$item = $item->__toString();
411
-		}
412
-
413
-		// Get class to use
414
-		$class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
415
-
416
-		return Element::create('span', $item)->addClass('input-group-'.$class);
417
-	}
418
-
419
-	/**
420
-	 * Wrap a field with prepended and appended items
421
-	 *
422
-	 * @param  Field $field
423
-	 * @param  array $prepend
424
-	 * @param  array $append
425
-	 *
426
-	 * @return string A field concatented with prepended and/or appended items
427
-	 */
428
-	public function prependAppend($field, $prepend, $append)
429
-	{
430
-		$return = '<div class="input-group">';
431
-		$return .= join(null, $prepend);
432
-		$return .= $field->render();
433
-		$return .= join(null, $append);
434
-		$return .= '</div>';
435
-
436
-		return $return;
437
-	}
438
-
439
-	/**
440
-	 * Wrap a field with potential additional tags
441
-	 *
442
-	 * @param  Field $field
443
-	 *
444
-	 * @return Element A wrapped field
445
-	 */
446
-	public function wrapField($field)
447
-	{
448
-		if ($this->app['former.form']->isOfType('horizontal')) {
449
-			return Element::create('div', $field)->addClass($this->fieldWidth);
450
-		}
451
-
452
-		return $field;
453
-	}
454
-
455
-	/**
456
-	 * Wrap actions block with potential additional tags
457
-	 *
458
-	 * @param  Actions $actions
459
-	 *
460
-	 * @return string A wrapped actions block
461
-	 */
462
-	public function wrapActions($actions)
463
-	{
464
-		// For horizontal forms, we wrap the actions in a div
465
-		if ($this->app['former.form']->isOfType('horizontal')) {
466
-			return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
467
-		}
468
-
469
-		return $actions;
470
-	}
16
+    /**
17
+     * Form types that trigger special styling for this Framework
18
+     *
19
+     * @var array
20
+     */
21
+    protected $availableTypes = array('horizontal', 'vertical', 'inline');
22
+
23
+    /**
24
+     * The button types available
25
+     *
26
+     * @var array
27
+     */
28
+    private $buttons = array(
29
+        'lg',
30
+        'sm',
31
+        'xs',
32
+        'block',
33
+        'link',
34
+        'primary',
35
+        'secondary',
36
+        'warning',
37
+        'danger',
38
+        'success',
39
+        'info',
40
+        'light',
41
+        'dark',
42
+    );
43
+
44
+    /**
45
+     * The field sizes available
46
+     *
47
+     * @var array
48
+     */
49
+    private $fields = array(
50
+        'lg',
51
+        'sm',
52
+        // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6',
53
+        // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12',
54
+        // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6',
55
+        // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12',
56
+        // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6',
57
+        // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12',
58
+        // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6',
59
+        // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12',
60
+    );
61
+
62
+    /**
63
+     * The field states available
64
+     *
65
+     * @var array
66
+     */
67
+    protected $states = array(
68
+        'is-invalid',
69
+    );
70
+
71
+    /**
72
+     * The default HTML tag used for icons
73
+     *
74
+     * @var string
75
+     */
76
+    protected $iconTag = 'i';
77
+
78
+    /**
79
+     * The default set for icon fonts
80
+     * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome
81
+     *
82
+     * @var string
83
+     */
84
+    protected $iconSet = 'fa';
85
+
86
+    /**
87
+     * The default prefix icon names
88
+     * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular
89
+     *
90
+     * @var string
91
+     */
92
+    protected $iconPrefix = 'fa';
93
+
94
+    /**
95
+     * Create a new TwitterBootstrap instance
96
+     *
97
+     * @param \Illuminate\Container\Container $app
98
+     */
99
+    public function __construct(Container $app)
100
+    {
101
+        $this->app = $app;
102
+        $this->setFrameworkDefaults();
103
+    }
104
+
105
+    ////////////////////////////////////////////////////////////////////
106
+    /////////////////////////// FILTER ARRAYS //////////////////////////
107
+    ////////////////////////////////////////////////////////////////////
108
+
109
+    /**
110
+     * Filter buttons classes
111
+     *
112
+     * @param  array $classes An array of classes
113
+     *
114
+     * @return string[] A filtered array
115
+     */
116
+    public function filterButtonClasses($classes)
117
+    {
118
+        // Filter classes
119
+        // $classes = array_intersect($classes, $this->buttons);
120
+
121
+        // Prepend button type
122
+        $classes   = $this->prependWith($classes, 'btn-');
123
+        $classes[] = 'btn';
124
+
125
+        return $classes;
126
+    }
127
+
128
+    /**
129
+     * Filter field classes
130
+     *
131
+     * @param  array $classes An array of classes
132
+     *
133
+     * @return array A filtered array
134
+     */
135
+    public function filterFieldClasses($classes)
136
+    {
137
+        // Filter classes
138
+        $classes = array_intersect($classes, $this->fields);
139
+
140
+        // Prepend field type
141
+        $classes = array_map(function ($class) {
142
+            return Str::startsWith($class, 'col') ? $class : 'input-'.$class;
143
+        }, $classes);
144
+
145
+        return $classes;
146
+    }
147
+
148
+    ////////////////////////////////////////////////////////////////////
149
+    ///////////////////// EXPOSE FRAMEWORK SPECIFICS ///////////////////
150
+    ////////////////////////////////////////////////////////////////////
151
+
152
+    /**
153
+     * Framework error state
154
+     *
155
+     * @return string
156
+     */
157
+    public function errorState()
158
+    {
159
+        return 'is-invalid';
160
+    }
161
+
162
+    /**
163
+     * Returns corresponding inline class of a field
164
+     *
165
+     * @param Field $field
166
+     *
167
+     * @return string
168
+     */
169
+    public function getInlineLabelClass($field)
170
+    {
171
+        $inlineClass = parent::getInlineLabelClass($field);
172
+        if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) {
173
+            $inlineClass = 'form-check-label';
174
+        }
175
+
176
+        return $inlineClass;
177
+    }
178
+
179
+    /**
180
+     * Set the fields width from a label width
181
+     *
182
+     * @param array $labelWidths
183
+     */
184
+    protected function setFieldWidths($labelWidths)
185
+    {
186
+        $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = '';
187
+
188
+        $viewports = $this->getFrameworkOption('viewports');
189
+        foreach ($labelWidths as $viewport => $columns) {
190
+            if ($viewport) {
191
+                $labelWidthClass .= " col-$viewports[$viewport]-$columns";
192
+                $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns);
193
+                $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns";
194
+            }
195
+        }
196
+
197
+        $this->labelWidth  = ltrim($labelWidthClass);
198
+        $this->fieldWidth  = ltrim($fieldWidthClass);
199
+        $this->fieldOffset = ltrim($fieldOffsetClass);
200
+    }
201
+
202
+    ////////////////////////////////////////////////////////////////////
203
+    ///////////////////////////// ADD CLASSES //////////////////////////
204
+    ////////////////////////////////////////////////////////////////////
205
+
206
+    /**
207
+     * Add classes to a field
208
+     *
209
+     * @param Field $field
210
+     * @param array $classes The possible classes to add
211
+     *
212
+     * @return Field
213
+     */
214
+    public function getFieldClasses(Field $field, $classes)
215
+    {
216
+        // Add inline class for checkables
217
+        if ($field->isCheckable()) {
218
+            $classes[] = 'form-check';
219
+
220
+            if (in_array('inline', $classes)) {
221
+                $field->inline();
222
+            }
223
+        }
224
+
225
+        // Filter classes according to field type
226
+        if ($field->isButton()) {
227
+            $classes = $this->filterButtonClasses($classes);
228
+        } else {
229
+            $classes = $this->filterFieldClasses($classes);
230
+        }
231
+
232
+        // Add form-control class for text-type, textarea and select fields
233
+        // As text-type is open-ended we instead exclude those that shouldn't receive the class
234
+        if (!$field->isCheckable() and !$field->isButton() and !in_array($field->getType(), array(
235
+                    'file',
236
+                    'plaintext',
237
+                )) and !in_array('form-control', $classes)
238
+        ) {
239
+            $classes[] = 'form-control';
240
+        }
241
+
242
+        if ($this->app['former']->getErrors($field->getName())) {
243
+            $classes[] = $this->errorState();
244
+        }
245
+
246
+        return $this->addClassesToField($field, $classes);
247
+    }
248
+
249
+    /**
250
+     * Add group classes
251
+     *
252
+     * @return string A list of group classes
253
+     */
254
+    public function getGroupClasses()
255
+    {
256
+        if ($this->app['former.form']->isOfType('horizontal')) {
257
+            return 'form-group row';
258
+        } else {
259
+            return 'form-group';
260
+        }
261
+    }
262
+
263
+    /**
264
+     * Add label classes
265
+     *
266
+     * @return string[] An array of attributes with the label class
267
+     */
268
+    public function getLabelClasses()
269
+    {
270
+        if ($this->app['former.form']->isOfType('horizontal')) {
271
+            return array('control-label', $this->labelWidth);
272
+        } elseif ($this->app['former.form']->isOfType('inline')) {
273
+            return array('sr-only');
274
+        } else {
275
+            return array('control-label');
276
+        }
277
+    }
278
+
279
+    /**
280
+     * Add uneditable field classes
281
+     *
282
+     * @return string An array of attributes with the uneditable class
283
+     */
284
+    public function getUneditableClasses()
285
+    {
286
+        return '';
287
+    }
288
+
289
+    /**
290
+     * Add plain text field classes
291
+     *
292
+     * @return string An array of attributes with the plain text class
293
+     */
294
+    public function getPlainTextClasses()
295
+    {
296
+        return 'form-control-static';
297
+    }
298
+
299
+    /**
300
+     * Add form class
301
+     *
302
+     * @param  string $type The type of form to add
303
+     *
304
+     * @return string|null
305
+     */
306
+    public function getFormClasses($type)
307
+    {
308
+        return $type ? 'form-'.$type : null;
309
+    }
310
+
311
+    /**
312
+     * Add actions block class
313
+     *
314
+     * @return string|null
315
+     */
316
+    public function getActionClasses()
317
+    {
318
+        if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) {
319
+            return 'form-group row';
320
+        }
321
+
322
+        return null;
323
+    }
324
+
325
+    ////////////////////////////////////////////////////////////////////
326
+    //////////////////////////// RENDER BLOCKS /////////////////////////
327
+    ////////////////////////////////////////////////////////////////////
328
+
329
+    /**
330
+     * Render an help text
331
+     *
332
+     * @param string $text
333
+     * @param array  $attributes
334
+     *
335
+     * @return Element
336
+     */
337
+    public function createHelp($text, $attributes = array())
338
+    {
339
+        return Element::create('small', $text, $attributes)->addClass('text-muted');
340
+    }
341
+
342
+    /**
343
+     * Render an validation error text
344
+     *
345
+     * @param string $text
346
+     * @param array  $attributes
347
+     *
348
+     * @return string
349
+     */
350
+    public function createValidationError($text, $attributes = array())
351
+    {
352
+        return Element::create('div', $text, $attributes)->addClass('invalid-feedback');
353
+    }
354
+
355
+    /**
356
+     * Render an help text
357
+     *
358
+     * @param string $text
359
+     * @param array  $attributes
360
+     *
361
+     * @return Element
362
+     */
363
+    public function createBlockHelp($text, $attributes = array())
364
+    {
365
+        return Element::create('small', $text, $attributes)->addClass('form-text text-muted');
366
+    }
367
+
368
+    /**
369
+     * Render a disabled field
370
+     *
371
+     * @param Field $field
372
+     *
373
+     * @return Element
374
+     */
375
+    public function createDisabledField(Field $field)
376
+    {
377
+        return Element::create('span', $field->getValue(), $field->getAttributes());
378
+    }
379
+
380
+    /**
381
+     * Render a plain text field
382
+     *
383
+     * @param Field $field
384
+     *
385
+     * @return Element
386
+     */
387
+    public function createPlainTextField(Field $field)
388
+    {
389
+        $label = $field->getLabel();
390
+        if ($label) {
391
+            $label->for('');
392
+        }
393
+
394
+        return Element::create('div', $field->getValue(), $field->getAttributes());
395
+    }
396
+
397
+    ////////////////////////////////////////////////////////////////////
398
+    //////////////////////////// WRAP BLOCKS ///////////////////////////
399
+    ////////////////////////////////////////////////////////////////////
400
+
401
+    /**
402
+     * Wrap an item to be prepended or appended to the current field
403
+     *
404
+     * @return Element A wrapped item
405
+     */
406
+    public function placeAround($item)
407
+    {
408
+        // Render object
409
+        if (is_object($item) and method_exists($item, '__toString')) {
410
+            $item = $item->__toString();
411
+        }
412
+
413
+        // Get class to use
414
+        $class = (strpos($item, '<button') !== false) ? 'btn' : 'addon';
415
+
416
+        return Element::create('span', $item)->addClass('input-group-'.$class);
417
+    }
418
+
419
+    /**
420
+     * Wrap a field with prepended and appended items
421
+     *
422
+     * @param  Field $field
423
+     * @param  array $prepend
424
+     * @param  array $append
425
+     *
426
+     * @return string A field concatented with prepended and/or appended items
427
+     */
428
+    public function prependAppend($field, $prepend, $append)
429
+    {
430
+        $return = '<div class="input-group">';
431
+        $return .= join(null, $prepend);
432
+        $return .= $field->render();
433
+        $return .= join(null, $append);
434
+        $return .= '</div>';
435
+
436
+        return $return;
437
+    }
438
+
439
+    /**
440
+     * Wrap a field with potential additional tags
441
+     *
442
+     * @param  Field $field
443
+     *
444
+     * @return Element A wrapped field
445
+     */
446
+    public function wrapField($field)
447
+    {
448
+        if ($this->app['former.form']->isOfType('horizontal')) {
449
+            return Element::create('div', $field)->addClass($this->fieldWidth);
450
+        }
451
+
452
+        return $field;
453
+    }
454
+
455
+    /**
456
+     * Wrap actions block with potential additional tags
457
+     *
458
+     * @param  Actions $actions
459
+     *
460
+     * @return string A wrapped actions block
461
+     */
462
+    public function wrapActions($actions)
463
+    {
464
+        // For horizontal forms, we wrap the actions in a div
465
+        if ($this->app['former.form']->isOfType('horizontal')) {
466
+            return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth));
467
+        }
468
+
469
+        return $actions;
470
+    }
471 471
 }
Please login to merge, or discard this patch.
src/Former/Traits/Checkable.php 2 patches
Indentation   +553 added lines, -553 removed lines patch added patch discarded remove patch
@@ -12,558 +12,558 @@
 block discarded – undo
12 12
  */
13 13
 abstract class Checkable extends Field
14 14
 {
15
-	/**
16
-	 * Renders the checkables as inline
17
-	 *
18
-	 * @var boolean
19
-	 */
20
-	protected $inline = false;
21
-
22
-	/**
23
-	 * Add a text to a single element
24
-	 *
25
-	 * @var string
26
-	 */
27
-	protected $text = null;
28
-
29
-	/**
30
-	 * Renders the checkables as grouped
31
-	 *
32
-	 * @var boolean
33
-	 */
34
-	protected $grouped = false;
35
-
36
-	/**
37
-	 * The checkable items currently stored
38
-	 *
39
-	 * @var array
40
-	 */
41
-	protected $items = array();
42
-
43
-	/**
44
-	 * The type of checkable item
45
-	 *
46
-	 * @var string
47
-	 */
48
-	protected $checkable = null;
49
-
50
-	/**
51
-	 * An array of checked items
52
-	 *
53
-	 * @var array
54
-	 */
55
-	protected $checked = array();
56
-
57
-	/**
58
-	 * The checkable currently being focused on
59
-	 *
60
-	 * @var integer
61
-	 */
62
-	protected $focus = null;
63
-
64
-	/**
65
-	 * Whether this particular checkable is to be pushed
66
-	 *
67
-	 * @var boolean
68
-	 */
69
-	protected $isPushed = null;
70
-
71
-	////////////////////////////////////////////////////////////////////
72
-	//////////////////////////// CORE METHODS //////////////////////////
73
-	////////////////////////////////////////////////////////////////////
74
-
75
-	/**
76
-	 * Build a new checkable
77
-	 *
78
-	 * @param Container $app
79
-	 * @param string    $type
80
-	 * @param array     $name
81
-	 * @param           $label
82
-	 * @param           $value
83
-	 * @param           $attributes
84
-	 */
85
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
86
-	{
87
-		// Unify auto and chained methods of grouping checkboxes
88
-		if (ends_with($name, '[]')) {
89
-			$name = substr($name, 0, -2);
90
-			$this->grouped();
91
-		}
92
-		parent::__construct($app, $type, $name, $label, $value, $attributes);
93
-
94
-		if (is_array($this->value)) {
95
-			$this->items($this->value);
96
-		}
97
-	}
98
-
99
-	/**
100
-	 * Apply methods to focused checkable
101
-	 *
102
-	 * @param string $method
103
-	 * @param array  $parameters
104
-	 *
105
-	 * @return $this
106
-	 */
107
-	public function __call($method, $parameters)
108
-	{
109
-		$focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0));
110
-		if ($focused) {
111
-			return $this;
112
-		}
113
-
114
-		return parent::__call($method, $parameters);
115
-	}
116
-
117
-	/**
118
-	 * Prints out the currently stored checkables
119
-	 */
120
-	public function render()
121
-	{
122
-		$html = null;
123
-
124
-		// Multiple items
125
-		if ($this->items) {
126
-			unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]);
127
-			foreach ($this->items as $key => $item) {
128
-				$value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key;
129
-				$html .= $this->createCheckable($item, $value);
130
-			}
131
-
132
-			return $html;
133
-		}
134
-
135
-		// Single item
136
-		return $this->createCheckable(array(
137
-			'name'  => $this->name,
138
-			'label' => $this->text,
139
-			'value' => $this->value,
140
-			'attributes' => $this->attributes,
141
-		));
142
-	}
143
-
144
-	////////////////////////////////////////////////////////////////////
145
-	////////////////////////// FIELD METHODS ///////////////////////////
146
-	////////////////////////////////////////////////////////////////////
147
-
148
-	/**
149
-	 * Focus on a particular checkable
150
-	 *
151
-	 * @param integer $on The checkable to focus on
152
-	 *
153
-	 * @return $this
154
-	 */
155
-	public function on($on)
156
-	{
157
-		if (!isset($this->items[$on])) {
158
-			return $this;
159
-		} else {
160
-			$this->focus = $on;
161
-		}
162
-
163
-		return $this;
164
-	}
165
-
166
-	/**
167
-	 * Set the checkables as inline
168
-	 */
169
-	public function inline()
170
-	{
171
-		$this->inline = true;
172
-
173
-		return $this;
174
-	}
175
-
176
-	/**
177
-	 * Set the checkables as stacked
178
-	 */
179
-	public function stacked()
180
-	{
181
-		$this->inline = false;
182
-
183
-		return $this;
184
-	}
185
-
186
-	/**
187
-	 * Set the checkables as grouped
188
-	 */
189
-	public function grouped()
190
-	{
191
-		$this->grouped = true;
192
-
193
-		return $this;
194
-	}
195
-
196
-	/**
197
-	 * Add text to a single checkable
198
-	 *
199
-	 * @param  string $text The checkable label
200
-	 *
201
-	 * @return $this
202
-	 */
203
-	public function text($text)
204
-	{
205
-		// Translate and format
206
-		$text = Helpers::translate($text);
207
-
208
-		// Apply on focused if any
209
-		$focused = $this->setOnFocused('label', $text);
210
-		if ($focused) {
211
-			return $this;
212
-		}
213
-
214
-		$this->text = $text;
215
-
216
-		return $this;
217
-	}
218
-
219
-	/**
220
-	 * Push this particular checkbox
221
-	 *
222
-	 * @param boolean $pushed
223
-	 *
224
-	 * @return $this
225
-	 */
226
-	public function push($pushed = true)
227
-	{
228
-		$this->isPushed = $pushed;
229
-
230
-		return $this;
231
-	}
232
-
233
-	/**
234
-	 * Check a specific item
235
-	 *
236
-	 * @param bool|string $checked The checkable to check, or an array of checked items
237
-	 *
238
-	 * @return $this
239
-	 */
240
-	public function check($checked = true)
241
-	{
242
-		// If we're setting all the checked items at once
243
-		if (is_array($checked)) {
244
-			$this->checked = $checked;
245
-			// Checking an item in particular
246
-		} elseif (is_string($checked) or is_int($checked)) {
247
-			$this->checked[$checked] = true;
248
-			// Only setting a single item
249
-		} else {
250
-			$this->checked[$this->name] = (bool) $checked;
251
-		}
252
-
253
-		return $this;
254
-	}
255
-
256
-	////////////////////////////////////////////////////////////////////
257
-	////////////////////////// INTERNAL METHODS ////////////////////////
258
-	////////////////////////////////////////////////////////////////////
259
-
260
-	/**
261
-	 * Creates a series of checkable items
262
-	 *
263
-	 * @param array $_items Items to create
264
-	 */
265
-	protected function items($_items)
266
-	{
267
-		// If passing an array
268
-		if (sizeof($_items) == 1 and
269
-			isset($_items[0]) and
270
-			is_array($_items[0])
271
-		) {
272
-			$_items = $_items[0];
273
-		}
274
-
275
-		// Fetch models if that's what we were passed
276
-		if (isset($_items[0]) and is_object($_items[0])) {
277
-			$_items = Helpers::queryToArray($_items);
278
-			$_items = array_flip($_items);
279
-		}
280
-
281
-		// Iterate through items, assign a name and a label to each
282
-		$count = 0;
283
-		foreach ($_items as $label => $name) {
284
-
285
-			// Define a fallback name in case none is found
286
-			$fallback = $this->isCheckbox()
287
-				? $this->name.'_'.$count
288
-				: $this->name;
289
-
290
-			// Grouped fields
291
-			if ($this->isGrouped()) {
292
-				$attributes['id'] = str_replace('[]', null, $fallback);
293
-				$fallback         = str_replace('[]', null, $this->name).'[]';
294
-			}
295
-
296
-			// If we haven't any name defined for the checkable, try to compute some
297
-			if (!is_string($label) and !is_array($name)) {
298
-				$label = $name;
299
-				$name  = $fallback;
300
-			}
301
-
302
-			// If we gave custom information on the item, add them
303
-			if (is_array($name)) {
304
-				$attributes = $name;
305
-				$name       = array_get($attributes, 'name', $fallback);
306
-				unset($attributes['name']);
307
-			}
308
-
309
-			// Store all informations we have in an array
310
-			$item = array(
311
-				'name'  => $name,
312
-				'label' => Helpers::translate($label),
313
-				'count' => $count,
314
-			);
315
-			if (isset($attributes)) {
316
-				$item['attributes'] = $attributes;
317
-			}
318
-
319
-			$this->items[] = $item;
320
-			$count++;
321
-		}
322
-	}
323
-
324
-	/**
325
-	 * Renders a checkable
326
-	 *
327
-	 * @param string|array $item          A checkable item
328
-	 * @param integer      $fallbackValue A fallback value if none is set
329
-	 *
330
-	 * @return string
331
-	 */
332
-	protected function createCheckable($item, $fallbackValue = 1)
333
-	{
334
-		// Extract informations
335
-		extract($item);
336
-
337
-		// Set default values
338
-		if (!isset($attributes)) {
339
-			$attributes = array();
340
-		}
341
-		if (isset($attributes['value'])) {
342
-			$value = $attributes['value'];
343
-		}
344
-		if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) {
345
-			$value = $fallbackValue;
346
-		}
347
-
348
-		// If inline items, add class
349
-		$isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null;
350
-
351
-		// In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if
352
-		// rendering inline.
353
-		$class =  ($this->app['former']->framework() == 'TwitterBootstrap3' ||
354
-			$this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline;
355
-
356
-		// Merge custom attributes with global attributes
357
-		$attributes = array_merge($this->attributes, $attributes);
358
-		if (!isset($attributes['id'])) {
359
-			$attributes['id'] = $name.$this->unique($name);
360
-		}
361
-
362
-		// Create field
363
-		$field = Input::create($this->checkable, $name, $value, $attributes);
364
-		if ($this->isChecked($item, $value)) {
365
-			$field->checked('checked');
366
-		}
367
-
368
-		// Add hidden checkbox if requested
369
-		if ($this->isOfType('checkbox', 'checkboxes')) {
370
-			if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) {
371
-				$field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render();
372
-
373
-				// app['former.field'] was overwritten by Former::hidden() call in the line above, so here
374
-				// we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object
375
-				$this->app->instance('former.field', $this);
376
-			}
377
-		}
378
-
379
-		// If no label to wrap, return plain checkable
380
-		if (!$label) {
381
-			$element = (is_object($field)) ? $field->render() : $field;
382
-		} elseif ($this->app['former']->framework() == 'TwitterBootstrap4') {
383
-			// Revised for Bootstrap 4, move the 'input' outside of the 'label'
384
-			$element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render();
385
-
386
-			$wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check';
15
+    /**
16
+     * Renders the checkables as inline
17
+     *
18
+     * @var boolean
19
+     */
20
+    protected $inline = false;
21
+
22
+    /**
23
+     * Add a text to a single element
24
+     *
25
+     * @var string
26
+     */
27
+    protected $text = null;
28
+
29
+    /**
30
+     * Renders the checkables as grouped
31
+     *
32
+     * @var boolean
33
+     */
34
+    protected $grouped = false;
35
+
36
+    /**
37
+     * The checkable items currently stored
38
+     *
39
+     * @var array
40
+     */
41
+    protected $items = array();
42
+
43
+    /**
44
+     * The type of checkable item
45
+     *
46
+     * @var string
47
+     */
48
+    protected $checkable = null;
49
+
50
+    /**
51
+     * An array of checked items
52
+     *
53
+     * @var array
54
+     */
55
+    protected $checked = array();
56
+
57
+    /**
58
+     * The checkable currently being focused on
59
+     *
60
+     * @var integer
61
+     */
62
+    protected $focus = null;
63
+
64
+    /**
65
+     * Whether this particular checkable is to be pushed
66
+     *
67
+     * @var boolean
68
+     */
69
+    protected $isPushed = null;
70
+
71
+    ////////////////////////////////////////////////////////////////////
72
+    //////////////////////////// CORE METHODS //////////////////////////
73
+    ////////////////////////////////////////////////////////////////////
74
+
75
+    /**
76
+     * Build a new checkable
77
+     *
78
+     * @param Container $app
79
+     * @param string    $type
80
+     * @param array     $name
81
+     * @param           $label
82
+     * @param           $value
83
+     * @param           $attributes
84
+     */
85
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
86
+    {
87
+        // Unify auto and chained methods of grouping checkboxes
88
+        if (ends_with($name, '[]')) {
89
+            $name = substr($name, 0, -2);
90
+            $this->grouped();
91
+        }
92
+        parent::__construct($app, $type, $name, $label, $value, $attributes);
93
+
94
+        if (is_array($this->value)) {
95
+            $this->items($this->value);
96
+        }
97
+    }
98
+
99
+    /**
100
+     * Apply methods to focused checkable
101
+     *
102
+     * @param string $method
103
+     * @param array  $parameters
104
+     *
105
+     * @return $this
106
+     */
107
+    public function __call($method, $parameters)
108
+    {
109
+        $focused = $this->setOnFocused('attributes.'.$method, array_get($parameters, 0));
110
+        if ($focused) {
111
+            return $this;
112
+        }
113
+
114
+        return parent::__call($method, $parameters);
115
+    }
116
+
117
+    /**
118
+     * Prints out the currently stored checkables
119
+     */
120
+    public function render()
121
+    {
122
+        $html = null;
123
+
124
+        // Multiple items
125
+        if ($this->items) {
126
+            unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]);
127
+            foreach ($this->items as $key => $item) {
128
+                $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key;
129
+                $html .= $this->createCheckable($item, $value);
130
+            }
131
+
132
+            return $html;
133
+        }
134
+
135
+        // Single item
136
+        return $this->createCheckable(array(
137
+            'name'  => $this->name,
138
+            'label' => $this->text,
139
+            'value' => $this->value,
140
+            'attributes' => $this->attributes,
141
+        ));
142
+    }
143
+
144
+    ////////////////////////////////////////////////////////////////////
145
+    ////////////////////////// FIELD METHODS ///////////////////////////
146
+    ////////////////////////////////////////////////////////////////////
147
+
148
+    /**
149
+     * Focus on a particular checkable
150
+     *
151
+     * @param integer $on The checkable to focus on
152
+     *
153
+     * @return $this
154
+     */
155
+    public function on($on)
156
+    {
157
+        if (!isset($this->items[$on])) {
158
+            return $this;
159
+        } else {
160
+            $this->focus = $on;
161
+        }
162
+
163
+        return $this;
164
+    }
165
+
166
+    /**
167
+     * Set the checkables as inline
168
+     */
169
+    public function inline()
170
+    {
171
+        $this->inline = true;
172
+
173
+        return $this;
174
+    }
175
+
176
+    /**
177
+     * Set the checkables as stacked
178
+     */
179
+    public function stacked()
180
+    {
181
+        $this->inline = false;
182
+
183
+        return $this;
184
+    }
185
+
186
+    /**
187
+     * Set the checkables as grouped
188
+     */
189
+    public function grouped()
190
+    {
191
+        $this->grouped = true;
192
+
193
+        return $this;
194
+    }
195
+
196
+    /**
197
+     * Add text to a single checkable
198
+     *
199
+     * @param  string $text The checkable label
200
+     *
201
+     * @return $this
202
+     */
203
+    public function text($text)
204
+    {
205
+        // Translate and format
206
+        $text = Helpers::translate($text);
207
+
208
+        // Apply on focused if any
209
+        $focused = $this->setOnFocused('label', $text);
210
+        if ($focused) {
211
+            return $this;
212
+        }
213
+
214
+        $this->text = $text;
215
+
216
+        return $this;
217
+    }
218
+
219
+    /**
220
+     * Push this particular checkbox
221
+     *
222
+     * @param boolean $pushed
223
+     *
224
+     * @return $this
225
+     */
226
+    public function push($pushed = true)
227
+    {
228
+        $this->isPushed = $pushed;
229
+
230
+        return $this;
231
+    }
232
+
233
+    /**
234
+     * Check a specific item
235
+     *
236
+     * @param bool|string $checked The checkable to check, or an array of checked items
237
+     *
238
+     * @return $this
239
+     */
240
+    public function check($checked = true)
241
+    {
242
+        // If we're setting all the checked items at once
243
+        if (is_array($checked)) {
244
+            $this->checked = $checked;
245
+            // Checking an item in particular
246
+        } elseif (is_string($checked) or is_int($checked)) {
247
+            $this->checked[$checked] = true;
248
+            // Only setting a single item
249
+        } else {
250
+            $this->checked[$this->name] = (bool) $checked;
251
+        }
252
+
253
+        return $this;
254
+    }
255
+
256
+    ////////////////////////////////////////////////////////////////////
257
+    ////////////////////////// INTERNAL METHODS ////////////////////////
258
+    ////////////////////////////////////////////////////////////////////
259
+
260
+    /**
261
+     * Creates a series of checkable items
262
+     *
263
+     * @param array $_items Items to create
264
+     */
265
+    protected function items($_items)
266
+    {
267
+        // If passing an array
268
+        if (sizeof($_items) == 1 and
269
+            isset($_items[0]) and
270
+            is_array($_items[0])
271
+        ) {
272
+            $_items = $_items[0];
273
+        }
274
+
275
+        // Fetch models if that's what we were passed
276
+        if (isset($_items[0]) and is_object($_items[0])) {
277
+            $_items = Helpers::queryToArray($_items);
278
+            $_items = array_flip($_items);
279
+        }
280
+
281
+        // Iterate through items, assign a name and a label to each
282
+        $count = 0;
283
+        foreach ($_items as $label => $name) {
284
+
285
+            // Define a fallback name in case none is found
286
+            $fallback = $this->isCheckbox()
287
+                ? $this->name.'_'.$count
288
+                : $this->name;
289
+
290
+            // Grouped fields
291
+            if ($this->isGrouped()) {
292
+                $attributes['id'] = str_replace('[]', null, $fallback);
293
+                $fallback         = str_replace('[]', null, $this->name).'[]';
294
+            }
295
+
296
+            // If we haven't any name defined for the checkable, try to compute some
297
+            if (!is_string($label) and !is_array($name)) {
298
+                $label = $name;
299
+                $name  = $fallback;
300
+            }
301
+
302
+            // If we gave custom information on the item, add them
303
+            if (is_array($name)) {
304
+                $attributes = $name;
305
+                $name       = array_get($attributes, 'name', $fallback);
306
+                unset($attributes['name']);
307
+            }
308
+
309
+            // Store all informations we have in an array
310
+            $item = array(
311
+                'name'  => $name,
312
+                'label' => Helpers::translate($label),
313
+                'count' => $count,
314
+            );
315
+            if (isset($attributes)) {
316
+                $item['attributes'] = $attributes;
317
+            }
318
+
319
+            $this->items[] = $item;
320
+            $count++;
321
+        }
322
+    }
323
+
324
+    /**
325
+     * Renders a checkable
326
+     *
327
+     * @param string|array $item          A checkable item
328
+     * @param integer      $fallbackValue A fallback value if none is set
329
+     *
330
+     * @return string
331
+     */
332
+    protected function createCheckable($item, $fallbackValue = 1)
333
+    {
334
+        // Extract informations
335
+        extract($item);
336
+
337
+        // Set default values
338
+        if (!isset($attributes)) {
339
+            $attributes = array();
340
+        }
341
+        if (isset($attributes['value'])) {
342
+            $value = $attributes['value'];
343
+        }
344
+        if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) {
345
+            $value = $fallbackValue;
346
+        }
347
+
348
+        // If inline items, add class
349
+        $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null;
350
+
351
+        // In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if
352
+        // rendering inline.
353
+        $class =  ($this->app['former']->framework() == 'TwitterBootstrap3' ||
354
+            $this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline;
355
+
356
+        // Merge custom attributes with global attributes
357
+        $attributes = array_merge($this->attributes, $attributes);
358
+        if (!isset($attributes['id'])) {
359
+            $attributes['id'] = $name.$this->unique($name);
360
+        }
361
+
362
+        // Create field
363
+        $field = Input::create($this->checkable, $name, $value, $attributes);
364
+        if ($this->isChecked($item, $value)) {
365
+            $field->checked('checked');
366
+        }
367
+
368
+        // Add hidden checkbox if requested
369
+        if ($this->isOfType('checkbox', 'checkboxes')) {
370
+            if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) {
371
+                $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render();
372
+
373
+                // app['former.field'] was overwritten by Former::hidden() call in the line above, so here
374
+                // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object
375
+                $this->app->instance('former.field', $this);
376
+            }
377
+        }
378
+
379
+        // If no label to wrap, return plain checkable
380
+        if (!$label) {
381
+            $element = (is_object($field)) ? $field->render() : $field;
382
+        } elseif ($this->app['former']->framework() == 'TwitterBootstrap4') {
383
+            // Revised for Bootstrap 4, move the 'input' outside of the 'label'
384
+            $element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render();
385
+
386
+            $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check';
387 387
 			
388
-			$element = Element::create('div', $element)->class($wrapper_class)->render();
389
-
390
-		} else {
391
-			// Original way is to add the 'input' inside the 'label'
392
-			$element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render();
393
-		}
394
-
395
-		// If BS3, if checkables are stacked, wrap them in a div with the checkable type
396
-		if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') {
397
-			$wrapper = Element::create('div', $element)->class($this->checkable);
398
-			if ($this->getAttribute('disabled')) {
399
-				$wrapper->addClass('disabled');
400
-			}
401
-			$element = $wrapper->render();
402
-		}
403
-
404
-		// Return the field
405
-		return $element;
406
-	}
407
-
408
-	////////////////////////////////////////////////////////////////////
409
-	///////////////////////////// HELPERS //////////////////////////////
410
-	////////////////////////////////////////////////////////////////////
411
-
412
-	/**
413
-	 * Generate an unique ID for a field
414
-	 *
415
-	 * @param string $name The field's name
416
-	 *
417
-	 * @return string A field number to use
418
-	 */
419
-	protected function unique($name)
420
-	{
421
-		$this->app['former']->labels[] = $name;
422
-
423
-		// Count number of fields with the same ID
424
-		$where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
425
-			return $label == $name;
426
-		});
427
-		$unique = sizeof($where);
428
-
429
-		// In case the field doesn't need to be numbered
430
-		if ($unique < 2 or empty($this->items)) {
431
-			return false;
432
-		}
433
-
434
-		return $unique;
435
-	}
436
-
437
-	/**
438
-	 * Set something on the currently focused checkable
439
-	 *
440
-	 * @param string $attribute The key to set
441
-	 * @param string $value     Its value
442
-	 *
443
-	 * @return $this|bool
444
-	 */
445
-	protected function setOnFocused($attribute, $value)
446
-	{
447
-		if (is_null($this->focus)) {
448
-			return false;
449
-		}
450
-
451
-		$this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value);
452
-
453
-		return $this;
454
-	}
455
-
456
-	/**
457
-	 * Check if a checkable is checked
458
-	 *
459
-	 * @return boolean Checked or not
460
-	 */
461
-	protected function isChecked($item = null, $value = null)
462
-	{
463
-		if (isset($item['name'])) {
464
-			$name = $item['name'];
465
-		}
466
-		if (empty($name)) {
467
-			$name = $this->name;
468
-		}
469
-
470
-		// If it's a checkbox, see if we marqued that one as checked in the array
471
-		// Or if it's a single radio, simply see if we called check
472
-		if ($this->isCheckbox() or
473
-			!$this->isCheckbox() and !$this->items
474
-		) {
475
-			$checked = array_get($this->checked, $name, false);
476
-
477
-			// If there are multiple, search for the value
478
-			// as the name are the same between radios
479
-		} else {
480
-			$checked = array_get($this->checked, $value, false);
481
-		}
482
-
483
-		// Check the values and POST array
484
-		if ($this->isGrouped()) {
485
-			// The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[])
486
-			$groupIndex = self::getGroupIndexFromItem($item);
487
-
488
-			// Search using the bare name, not the individual item name
489
-			$post   = $this->app['former']->getPost($this->name);
490
-			$static = $this->app['former']->getValue($this->bind ?: $this->name);
491
-
492
-			if (isset($post[$groupIndex])) {
493
-				$post = $post[$groupIndex];
494
-			}
495
-
496
-			/**
497
-			 * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must
498
-			 * match the value in order for the checkbox to be considered checked, e.g.:
499
-			 *
500
-			 *  array(
501
-			 *    'name' = 'roles[foo]',
502
-			 *    'value' => 'foo',
503
-			 *  )
504
-			 */
505
-			if ($static instanceof Collection) {
506
-				// If the repopulate value is a collection, search for an item matching the $groupIndex
507
-				foreach ($static as $staticItem) {
508
-					$staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem;
509
-					if ($staticItemValue == $groupIndex) {
510
-						$static = $staticItemValue;
511
-						break;
512
-					}
513
-				}
514
-			} else if (isset($static[$groupIndex])) {
515
-				$static = $static[$groupIndex];
516
-			}
517
-		} else {
518
-			$post   = $this->app['former']->getPost($name);
519
-			$static = $this->app['former']->getValue($this->bind ?: $name);
520
-		}
521
-
522
-		if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) {
523
-			$isChecked = ($post == $value);
524
-		} elseif (!is_null($static)) {
525
-			$isChecked = ($static == $value);
526
-		} else {
527
-			$isChecked = $checked;
528
-		}
529
-
530
-		return $isChecked ? true : false;
531
-	}
532
-
533
-	/**
534
-	 * Check if the current element is a checkbox
535
-	 *
536
-	 * @return boolean Checkbox or radio
537
-	 */
538
-	protected function isCheckbox()
539
-	{
540
-		return $this->checkable == 'checkbox';
541
-	}
542
-
543
-	/**
544
-	 * Check if the checkables are grouped or not
545
-	 *
546
-	 * @return boolean
547
-	 */
548
-	protected function isGrouped()
549
-	{
550
-		return
551
-			$this->grouped == true or
552
-			strpos($this->name, '[]') !== false;
553
-	}
554
-
555
-	/**
556
-	 * @param array $item The item array, containing at least name and count keys.
557
-	 *
558
-	 * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[])
559
-	 */
560
-	public static function getGroupIndexFromItem($item)
561
-	{
562
-		$groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']);
563
-		if (empty($groupIndex) or $groupIndex == $item['name']) {
564
-			return $item['count'];
565
-		}
566
-
567
-		return $groupIndex;
568
-	}
388
+            $element = Element::create('div', $element)->class($wrapper_class)->render();
389
+
390
+        } else {
391
+            // Original way is to add the 'input' inside the 'label'
392
+            $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render();
393
+        }
394
+
395
+        // If BS3, if checkables are stacked, wrap them in a div with the checkable type
396
+        if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') {
397
+            $wrapper = Element::create('div', $element)->class($this->checkable);
398
+            if ($this->getAttribute('disabled')) {
399
+                $wrapper->addClass('disabled');
400
+            }
401
+            $element = $wrapper->render();
402
+        }
403
+
404
+        // Return the field
405
+        return $element;
406
+    }
407
+
408
+    ////////////////////////////////////////////////////////////////////
409
+    ///////////////////////////// HELPERS //////////////////////////////
410
+    ////////////////////////////////////////////////////////////////////
411
+
412
+    /**
413
+     * Generate an unique ID for a field
414
+     *
415
+     * @param string $name The field's name
416
+     *
417
+     * @return string A field number to use
418
+     */
419
+    protected function unique($name)
420
+    {
421
+        $this->app['former']->labels[] = $name;
422
+
423
+        // Count number of fields with the same ID
424
+        $where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
425
+            return $label == $name;
426
+        });
427
+        $unique = sizeof($where);
428
+
429
+        // In case the field doesn't need to be numbered
430
+        if ($unique < 2 or empty($this->items)) {
431
+            return false;
432
+        }
433
+
434
+        return $unique;
435
+    }
436
+
437
+    /**
438
+     * Set something on the currently focused checkable
439
+     *
440
+     * @param string $attribute The key to set
441
+     * @param string $value     Its value
442
+     *
443
+     * @return $this|bool
444
+     */
445
+    protected function setOnFocused($attribute, $value)
446
+    {
447
+        if (is_null($this->focus)) {
448
+            return false;
449
+        }
450
+
451
+        $this->items[$this->focus] = array_set($this->items[$this->focus], $attribute, $value);
452
+
453
+        return $this;
454
+    }
455
+
456
+    /**
457
+     * Check if a checkable is checked
458
+     *
459
+     * @return boolean Checked or not
460
+     */
461
+    protected function isChecked($item = null, $value = null)
462
+    {
463
+        if (isset($item['name'])) {
464
+            $name = $item['name'];
465
+        }
466
+        if (empty($name)) {
467
+            $name = $this->name;
468
+        }
469
+
470
+        // If it's a checkbox, see if we marqued that one as checked in the array
471
+        // Or if it's a single radio, simply see if we called check
472
+        if ($this->isCheckbox() or
473
+            !$this->isCheckbox() and !$this->items
474
+        ) {
475
+            $checked = array_get($this->checked, $name, false);
476
+
477
+            // If there are multiple, search for the value
478
+            // as the name are the same between radios
479
+        } else {
480
+            $checked = array_get($this->checked, $value, false);
481
+        }
482
+
483
+        // Check the values and POST array
484
+        if ($this->isGrouped()) {
485
+            // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[])
486
+            $groupIndex = self::getGroupIndexFromItem($item);
487
+
488
+            // Search using the bare name, not the individual item name
489
+            $post   = $this->app['former']->getPost($this->name);
490
+            $static = $this->app['former']->getValue($this->bind ?: $this->name);
491
+
492
+            if (isset($post[$groupIndex])) {
493
+                $post = $post[$groupIndex];
494
+            }
495
+
496
+            /**
497
+             * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must
498
+             * match the value in order for the checkbox to be considered checked, e.g.:
499
+             *
500
+             *  array(
501
+             *    'name' = 'roles[foo]',
502
+             *    'value' => 'foo',
503
+             *  )
504
+             */
505
+            if ($static instanceof Collection) {
506
+                // If the repopulate value is a collection, search for an item matching the $groupIndex
507
+                foreach ($static as $staticItem) {
508
+                    $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem;
509
+                    if ($staticItemValue == $groupIndex) {
510
+                        $static = $staticItemValue;
511
+                        break;
512
+                    }
513
+                }
514
+            } else if (isset($static[$groupIndex])) {
515
+                $static = $static[$groupIndex];
516
+            }
517
+        } else {
518
+            $post   = $this->app['former']->getPost($name);
519
+            $static = $this->app['former']->getValue($this->bind ?: $name);
520
+        }
521
+
522
+        if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) {
523
+            $isChecked = ($post == $value);
524
+        } elseif (!is_null($static)) {
525
+            $isChecked = ($static == $value);
526
+        } else {
527
+            $isChecked = $checked;
528
+        }
529
+
530
+        return $isChecked ? true : false;
531
+    }
532
+
533
+    /**
534
+     * Check if the current element is a checkbox
535
+     *
536
+     * @return boolean Checkbox or radio
537
+     */
538
+    protected function isCheckbox()
539
+    {
540
+        return $this->checkable == 'checkbox';
541
+    }
542
+
543
+    /**
544
+     * Check if the checkables are grouped or not
545
+     *
546
+     * @return boolean
547
+     */
548
+    protected function isGrouped()
549
+    {
550
+        return
551
+            $this->grouped == true or
552
+            strpos($this->name, '[]') !== false;
553
+    }
554
+
555
+    /**
556
+     * @param array $item The item array, containing at least name and count keys.
557
+     *
558
+     * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[])
559
+     */
560
+    public static function getGroupIndexFromItem($item)
561
+    {
562
+        $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']);
563
+        if (empty($groupIndex) or $groupIndex == $item['name']) {
564
+            return $item['count'];
565
+        }
566
+
567
+        return $groupIndex;
568
+    }
569 569
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
 
351 351
 		// In Bootsrap 3 or 4, don't append the the checkable type (radio/checkbox) as a class if
352 352
 		// rendering inline.
353
-		$class =  ($this->app['former']->framework() == 'TwitterBootstrap3' ||
353
+		$class = ($this->app['former']->framework() == 'TwitterBootstrap3' ||
354 354
 			$this->app['former']->framework() == 'TwitterBootstrap4') ? trim($isInline) : $this->checkable.$isInline;
355 355
 
356 356
 		// Merge custom attributes with global attributes
@@ -381,7 +381,7 @@  discard block
 block discarded – undo
381 381
 			$element = (is_object($field)) ? $field->render() : $field;
382 382
 		} elseif ($this->app['former']->framework() == 'TwitterBootstrap4') {
383 383
 			// Revised for Bootstrap 4, move the 'input' outside of the 'label'
384
-			$element = $field . Element::create('label', $label)->for($attributes['id'])->class($class)->render();
384
+			$element = $field.Element::create('label', $label)->for($attributes['id'])->class($class)->render();
385 385
 
386 386
 			$wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check';
387 387
 			
@@ -421,7 +421,7 @@  discard block
 block discarded – undo
421 421
 		$this->app['former']->labels[] = $name;
422 422
 
423 423
 		// Count number of fields with the same ID
424
-		$where  = array_filter($this->app['former']->labels, function ($label) use ($name) {
424
+		$where  = array_filter($this->app['former']->labels, function($label) use ($name) {
425 425
 			return $label == $name;
426 426
 		});
427 427
 		$unique = sizeof($where);
Please login to merge, or discard this patch.
src/config/former.php 1 patch
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -1,207 +1,207 @@
 block discarded – undo
1 1
 <?php return array(
2 2
 
3
-	// Markup
4
-	////////////////////////////////////////////////////////////////////
5
-
6
-	// Whether labels should be automatically computed from name
7
-	'automatic_label'         => true,
8
-
9
-	// The default form type
10
-	'default_form_type'       => 'horizontal',
11
-
12
-	// Validation
13
-	////////////////////////////////////////////////////////////////////
14
-
15
-	// Whether Former should fetch errors from Session
16
-	'fetch_errors'            => true,
17
-
18
-	// Whether Former should try to apply Validator rules as attributes
19
-	'live_validation'         => true,
20
-
21
-	// Whether Former should automatically fetch error messages and
22
-	// display them next to the matching fields
23
-	'error_messages'          => true,
24
-
25
-	// Checkables
26
-	////////////////////////////////////////////////////////////////////
27
-
28
-	// Whether checkboxes should always be present in the POST data,
29
-	// no matter if you checked them or not
30
-	'push_checkboxes'         => false,
31
-
32
-	// The value a checkbox will have in the POST array if unchecked
33
-	'unchecked_value'         => 0,
34
-
35
-	// Required fields
36
-	////////////////////////////////////////////////////////////////////
37
-
38
-	// The class to be added to required fields
39
-	'required_class'          => 'required',
40
-
41
-	// A facultative text to append to the labels of required fields
42
-	'required_text'           => '<sup>*</sup>',
43
-
44
-	// Translations
45
-	////////////////////////////////////////////////////////////////////
46
-
47
-	// Where Former should look for translations
48
-	'translate_from'          => 'validation.attributes',
49
-
50
-	// Whether text that comes out of the translated
51
-	// should be capitalized (ex: email => Email) automatically
52
-	'capitalize_translations' => true,
53
-
54
-	// An array of attributes to automatically translate
55
-	'translatable'            => array(
56
-		'help',
57
-		'inlineHelp',
58
-		'blockHelp',
59
-		'placeholder',
60
-		'data_placeholder',
61
-		'label',
62
-	),
63
-
64
-	// Framework
65
-	////////////////////////////////////////////////////////////////////
66
-
67
-	// The framework to be used by Former
68
-	'framework'               => 'TwitterBootstrap3',
69
-
70
-	'TwitterBootstrap4'       => array(
71
-
72
-		// Map Former-supported viewports to Bootstrap 4 equivalents
73
-		'viewports'   => array(
74
-			'large'  => 'lg',
75
-			'medium' => 'md',
76
-			'small'  => 'sm',
77
-			'mini'   => 'xs',
78
-		),
79
-		// Width of labels for horizontal forms expressed as viewport => grid columns
80
-		'labelWidths' => array(
81
-			'large' => 2,
82
-			'small' => 4,
83
-		),
84
-		// HTML markup and classes used by Bootstrap 5 for icons
85
-		'icon'        => array(
86
-			'tag'    => 'i',
87
-			'set'    => 'fa',
88
-			'prefix' => 'fa',
89
-		),
90
-
91
-	),
92
-
93
-	'TwitterBootstrap3'       => array(
94
-
95
-		// Map Former-supported viewports to Bootstrap 3 equivalents
96
-		'viewports'   => array(
97
-			'large'  => 'lg',
98
-			'medium' => 'md',
99
-			'small'  => 'sm',
100
-			'mini'   => 'xs',
101
-		),
102
-		// Width of labels for horizontal forms expressed as viewport => grid columns
103
-		'labelWidths' => array(
104
-			'large' => 2,
105
-			'small' => 4,
106
-		),
107
-		// HTML markup and classes used by Bootstrap 3 for icons
108
-		'icon'        => array(
109
-			'tag'    => 'span',
110
-			'set'    => 'glyphicon',
111
-			'prefix' => 'glyphicon',
112
-		),
113
-
114
-	),
115
-
116
-	'Nude'                    => array(  // No-framework markup
117
-		'icon' => array(
118
-			'tag'    => 'i',
119
-			'set'    => null,
120
-			'prefix' => 'icon',
121
-		),
122
-	),
123
-
124
-	'TwitterBootstrap'        => array( // Twitter Bootstrap version 2
125
-		'icon' => array(
126
-			'tag'    => 'i',
127
-			'set'    => null,
128
-			'prefix' => 'icon',
129
-		),
130
-	),
131
-
132
-	'ZurbFoundation5'         => array(
133
-		// Map Former-supported viewports to Foundation 5 equivalents
134
-		'viewports'           => array(
135
-			'large'  => 'large',
136
-			'medium' => null,
137
-			'small'  => 'small',
138
-			'mini'   => null,
139
-		),
140
-		// Width of labels for horizontal forms expressed as viewport => grid columns
141
-		'labelWidths'         => array(
142
-			'small' => 3,
143
-		),
144
-		// Classes to be applied to wrapped labels in horizontal forms
145
-		'wrappedLabelClasses' => array('right', 'inline'),
146
-		// HTML markup and classes used by Foundation 5 for icons
147
-		'icon'                => array(
148
-			'tag'    => 'i',
149
-			'set'    => null,
150
-			'prefix' => 'fi',
151
-		),
152
-		// CSS for inline validation errors
153
-		'error_classes'       => array('class' => 'error'),
154
-	),
155
-
156
-	'ZurbFoundation4'         => array(
157
-		// Foundation 4 also has an experimental "medium" breakpoint
158
-		// explained at http://foundation.zurb.com/docs/components/grid.html
159
-		'viewports'           => array(
160
-			'large'  => 'large',
161
-			'medium' => null,
162
-			'small'  => 'small',
163
-			'mini'   => null,
164
-		),
165
-		// Width of labels for horizontal forms expressed as viewport => grid columns
166
-		'labelWidths'         => array(
167
-			'small' => 3,
168
-		),
169
-		// Classes to be applied to wrapped labels in horizontal forms
170
-		'wrappedLabelClasses' => array('right', 'inline'),
171
-		// HTML markup and classes used by Foundation 4 for icons
172
-		'icon'                => array(
173
-			'tag'    => 'i',
174
-			'set'    => 'general',
175
-			'prefix' => 'foundicon',
176
-		),
177
-		// CSS for inline validation errors
178
-		'error_classes'       => array('class' => 'alert-box radius warning'),
179
-	),
180
-
181
-	'ZurbFoundation'          => array( // Foundation 3
182
-		'viewports'           => array(
183
-			'large'  => '',
184
-			'medium' => null,
185
-			'small'  => 'mobile-',
186
-			'mini'   => null,
187
-		),
188
-		// Width of labels for horizontal forms expressed as viewport => grid columns
189
-		'labelWidths'         => array(
190
-			'large' => 2,
191
-			'small' => 4,
192
-		),
193
-		// Classes to be applied to wrapped labels in horizontal forms
194
-		'wrappedLabelClasses' => array('right', 'inline'),
195
-		// HTML markup and classes used by Foundation 3 for icons
196
-		'icon'                => array(
197
-			'tag'    => 'i',
198
-			'set'    => null,
199
-			'prefix' => 'fi',
200
-		),
201
-		// CSS for inline validation errors
202
-		// should work for Zurb 2 and 3
203
-		'error_classes'       => array('class' => 'alert-box alert error'),
204
-	),
3
+    // Markup
4
+    ////////////////////////////////////////////////////////////////////
5
+
6
+    // Whether labels should be automatically computed from name
7
+    'automatic_label'         => true,
8
+
9
+    // The default form type
10
+    'default_form_type'       => 'horizontal',
11
+
12
+    // Validation
13
+    ////////////////////////////////////////////////////////////////////
14
+
15
+    // Whether Former should fetch errors from Session
16
+    'fetch_errors'            => true,
17
+
18
+    // Whether Former should try to apply Validator rules as attributes
19
+    'live_validation'         => true,
20
+
21
+    // Whether Former should automatically fetch error messages and
22
+    // display them next to the matching fields
23
+    'error_messages'          => true,
24
+
25
+    // Checkables
26
+    ////////////////////////////////////////////////////////////////////
27
+
28
+    // Whether checkboxes should always be present in the POST data,
29
+    // no matter if you checked them or not
30
+    'push_checkboxes'         => false,
31
+
32
+    // The value a checkbox will have in the POST array if unchecked
33
+    'unchecked_value'         => 0,
34
+
35
+    // Required fields
36
+    ////////////////////////////////////////////////////////////////////
37
+
38
+    // The class to be added to required fields
39
+    'required_class'          => 'required',
40
+
41
+    // A facultative text to append to the labels of required fields
42
+    'required_text'           => '<sup>*</sup>',
43
+
44
+    // Translations
45
+    ////////////////////////////////////////////////////////////////////
46
+
47
+    // Where Former should look for translations
48
+    'translate_from'          => 'validation.attributes',
49
+
50
+    // Whether text that comes out of the translated
51
+    // should be capitalized (ex: email => Email) automatically
52
+    'capitalize_translations' => true,
53
+
54
+    // An array of attributes to automatically translate
55
+    'translatable'            => array(
56
+        'help',
57
+        'inlineHelp',
58
+        'blockHelp',
59
+        'placeholder',
60
+        'data_placeholder',
61
+        'label',
62
+    ),
63
+
64
+    // Framework
65
+    ////////////////////////////////////////////////////////////////////
66
+
67
+    // The framework to be used by Former
68
+    'framework'               => 'TwitterBootstrap3',
69
+
70
+    'TwitterBootstrap4'       => array(
71
+
72
+        // Map Former-supported viewports to Bootstrap 4 equivalents
73
+        'viewports'   => array(
74
+            'large'  => 'lg',
75
+            'medium' => 'md',
76
+            'small'  => 'sm',
77
+            'mini'   => 'xs',
78
+        ),
79
+        // Width of labels for horizontal forms expressed as viewport => grid columns
80
+        'labelWidths' => array(
81
+            'large' => 2,
82
+            'small' => 4,
83
+        ),
84
+        // HTML markup and classes used by Bootstrap 5 for icons
85
+        'icon'        => array(
86
+            'tag'    => 'i',
87
+            'set'    => 'fa',
88
+            'prefix' => 'fa',
89
+        ),
90
+
91
+    ),
92
+
93
+    'TwitterBootstrap3'       => array(
94
+
95
+        // Map Former-supported viewports to Bootstrap 3 equivalents
96
+        'viewports'   => array(
97
+            'large'  => 'lg',
98
+            'medium' => 'md',
99
+            'small'  => 'sm',
100
+            'mini'   => 'xs',
101
+        ),
102
+        // Width of labels for horizontal forms expressed as viewport => grid columns
103
+        'labelWidths' => array(
104
+            'large' => 2,
105
+            'small' => 4,
106
+        ),
107
+        // HTML markup and classes used by Bootstrap 3 for icons
108
+        'icon'        => array(
109
+            'tag'    => 'span',
110
+            'set'    => 'glyphicon',
111
+            'prefix' => 'glyphicon',
112
+        ),
113
+
114
+    ),
115
+
116
+    'Nude'                    => array(  // No-framework markup
117
+        'icon' => array(
118
+            'tag'    => 'i',
119
+            'set'    => null,
120
+            'prefix' => 'icon',
121
+        ),
122
+    ),
123
+
124
+    'TwitterBootstrap'        => array( // Twitter Bootstrap version 2
125
+        'icon' => array(
126
+            'tag'    => 'i',
127
+            'set'    => null,
128
+            'prefix' => 'icon',
129
+        ),
130
+    ),
131
+
132
+    'ZurbFoundation5'         => array(
133
+        // Map Former-supported viewports to Foundation 5 equivalents
134
+        'viewports'           => array(
135
+            'large'  => 'large',
136
+            'medium' => null,
137
+            'small'  => 'small',
138
+            'mini'   => null,
139
+        ),
140
+        // Width of labels for horizontal forms expressed as viewport => grid columns
141
+        'labelWidths'         => array(
142
+            'small' => 3,
143
+        ),
144
+        // Classes to be applied to wrapped labels in horizontal forms
145
+        'wrappedLabelClasses' => array('right', 'inline'),
146
+        // HTML markup and classes used by Foundation 5 for icons
147
+        'icon'                => array(
148
+            'tag'    => 'i',
149
+            'set'    => null,
150
+            'prefix' => 'fi',
151
+        ),
152
+        // CSS for inline validation errors
153
+        'error_classes'       => array('class' => 'error'),
154
+    ),
155
+
156
+    'ZurbFoundation4'         => array(
157
+        // Foundation 4 also has an experimental "medium" breakpoint
158
+        // explained at http://foundation.zurb.com/docs/components/grid.html
159
+        'viewports'           => array(
160
+            'large'  => 'large',
161
+            'medium' => null,
162
+            'small'  => 'small',
163
+            'mini'   => null,
164
+        ),
165
+        // Width of labels for horizontal forms expressed as viewport => grid columns
166
+        'labelWidths'         => array(
167
+            'small' => 3,
168
+        ),
169
+        // Classes to be applied to wrapped labels in horizontal forms
170
+        'wrappedLabelClasses' => array('right', 'inline'),
171
+        // HTML markup and classes used by Foundation 4 for icons
172
+        'icon'                => array(
173
+            'tag'    => 'i',
174
+            'set'    => 'general',
175
+            'prefix' => 'foundicon',
176
+        ),
177
+        // CSS for inline validation errors
178
+        'error_classes'       => array('class' => 'alert-box radius warning'),
179
+    ),
180
+
181
+    'ZurbFoundation'          => array( // Foundation 3
182
+        'viewports'           => array(
183
+            'large'  => '',
184
+            'medium' => null,
185
+            'small'  => 'mobile-',
186
+            'mini'   => null,
187
+        ),
188
+        // Width of labels for horizontal forms expressed as viewport => grid columns
189
+        'labelWidths'         => array(
190
+            'large' => 2,
191
+            'small' => 4,
192
+        ),
193
+        // Classes to be applied to wrapped labels in horizontal forms
194
+        'wrappedLabelClasses' => array('right', 'inline'),
195
+        // HTML markup and classes used by Foundation 3 for icons
196
+        'icon'                => array(
197
+            'tag'    => 'i',
198
+            'set'    => null,
199
+            'prefix' => 'fi',
200
+        ),
201
+        // CSS for inline validation errors
202
+        // should work for Zurb 2 and 3
203
+        'error_classes'       => array('class' => 'alert-box alert error'),
204
+    ),
205 205
 
206 206
 
207 207
 );
Please login to merge, or discard this patch.