Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
86f4c03e
Unverified
Commit
86f4c03e
authored
Oct 09, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating new validation rule to extend alpha dash with spaces
parent
b29d1347
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
+89
-0
AlphaDashWithSpacesTest.php
Modules/Media/Tests/AlphaDashWithSpacesTest.php
+57
-0
AlphaDashWithSpaces.php
Modules/Media/Validators/AlphaDashWithSpaces.php
+32
-0
No files found.
Modules/Media/Tests/AlphaDashWithSpacesTest.php
0 → 100644
View file @
86f4c03e
<?php
namespace
Modules\Media\Tests
;
use
Illuminate\Container\Container
;
use
Illuminate\Filesystem\Filesystem
;
use
Illuminate\Translation\FileLoader
;
use
Illuminate\Translation\Translator
;
use
Illuminate\Validation\Factory
as
Validator
;
use
Modules\Media\Validators\AlphaDashWithSpaces
;
final
class
AlphaDashWithSpacesTest
extends
MediaTestCase
{
public
function
test_it_creates_instance_of_validator
()
{
$obj
=
new
AlphaDashWithSpaces
();
$this
->
assertInstanceOf
(
AlphaDashWithSpaces
::
class
,
$obj
);
}
/** @test */
public
function
it_validates_rule_is_valid
()
{
$this
->
assertTrue
(
$this
->
buildValidator
(
'My-Folder'
)
->
passes
());
$this
->
assertTrue
(
$this
->
buildValidator
(
'My Folder'
)
->
passes
());
$this
->
assertTrue
(
$this
->
buildValidator
(
'My Folder-isCool'
)
->
passes
());
}
/** @test */
public
function
it_validates_invalid_rule
()
{
$this
->
assertFalse
(
$this
->
buildValidator
(
'My-Folder @email'
)
->
passes
());
$this
->
assertFalse
(
$this
->
buildValidator
(
'My-Folder @email|}{'
)
->
passes
());
}
/** @test */
public
function
it_has_correct_error_message
()
{
$message
=
$this
->
buildValidator
(
'My-Folder @email|}{'
)
->
getMessageBag
();
$this
->
assertEquals
(
'The name may only contain letters, numbers, dashes and spaces.'
,
$message
->
get
(
'name'
)[
0
]
);
}
public
function
buildValidator
(
$folderName
)
{
$app
=
new
Container
();
$app
->
singleton
(
'app'
,
'Illuminate\Container\Container'
);
$translator
=
new
Translator
(
new
FileLoader
(
new
Filesystem
(),
null
),
'en'
);
$validator
=
(
new
Validator
(
$translator
))
->
make
([
'name'
=>
$folderName
],
[
'name'
=>
[
'required'
,
new
AlphaDashWithSpaces
()],
]);
return
$validator
;
}
}
Modules/Media/Validators/AlphaDashWithSpaces.php
0 → 100644
View file @
86f4c03e
<?php
namespace
Modules\Media\Validators
;
use
Illuminate\Contracts\Validation\Rule
;
class
AlphaDashWithSpaces
implements
Rule
{
/**
* Determine if the validation rule passes.
* @param string $attribute
* @param mixed $value
* @return bool
*/
public
function
passes
(
$attribute
,
$value
)
{
if
(
!
is_string
(
$value
)
&&
!
is_numeric
(
$value
))
{
return
false
;
}
return
preg_match
(
'/^[\pL\pM\pN_\s-]+$/u'
,
$value
)
>
0
;
}
/**
* Get the validation error message.
* @return string
*/
public
function
message
()
{
return
'The :attribute may only contain letters, numbers, dashes and spaces.'
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment