Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add displayabled values support #4037

Merged
merged 1 commit into from
May 2, 2014
Merged

add displayabled values support #4037

merged 1 commit into from
May 2, 2014

Conversation

overtrue
Copy link
Contributor

@overtrue overtrue commented Apr 4, 2014

some times we need custom displayable values, especially when using multi-byte encoded text scenes. for example:

HTML:

<label>banner type</label>
<select name="content_type" >
    <option value="1" data-type="image">Image</option>
    <option value="3" data-type="video">video</option>
    <option value="4" data-type="web">Web</option>
</select>
...
<div>
    <label>banner image</label>
    <input type="url" name="image" value="" placeholder="the url of banner image.">
</div>
<div>
    <label>banner video</label>
    <input type="url" name="video" value="" placeholder="the url of banner video.">
</div>
...

Rule:

    ...
    $rules = array(
              'content_type' => 'required|in:1,3,4',
              'image'        => 'required_if:content_type,1',
              'video'        => 'required_if:content_type,3',
             ),
    ...

app/lang/en/validation.php:

    ...
    'attributes' => array(
                     'content_type' => 'banner type',
                     'image'        => 'banner image url',
                     'video'        => 'banner video url',
                    ),
    ...

Request:

array(
 'content_type' => 1,
 'image'        => '',
);

Errors:

The banner image url field is required when content type is 1.

Difficult for users to understand what the number 1 is

We expect that this:

The banner image url field is required when banner type is Image.

Now we can add the values ​field to the app/lang/en/validation.php to achieve it:

    ...
    'values' => array(
                 'content_type' => array(
                                    '1' => 'Image',
                                    '3' => 'Video',
                                    '4' => 'Web'
                                   ),
                ),
    ...

Thanks!

@vandas
Copy link

vandas commented Apr 4, 2014

thanks It is what I want

@ghost
Copy link

ghost commented Apr 4, 2014

I also met this demand, thanks

@qufo
Copy link

qufo commented Apr 4, 2014

That is great. the error message will be simple and easier to read.

@botherbox
Copy link

Nice work!

@iarchean
Copy link

iarchean commented Apr 4, 2014

Great work. Helped me a lot.

@crny
Copy link

crny commented Apr 4, 2014

能说中文不?

@overtrue
Copy link
Contributor Author

overtrue commented Apr 4, 2014

可以用中文,不过作者可能看不懂啊,英文才是通用语言

@overtrue
Copy link
Contributor Author

overtrue commented Apr 4, 2014

@iarchean @Alexskywin @qufo @vandas Thanks!

@jasonlewis
Copy link
Contributor

I'd just usually have my option values set to "web", " image", etc.
On 4 Apr 2014 16:19, "Joy" notifications@github.com wrote:

@iarchean https://github.com/iarchean Thanks!

Reply to this email directly or view it on GitHubhttps://github.com//pull/4037#issuecomment-39532703
.

@overtrue
Copy link
Contributor Author

overtrue commented Apr 4, 2014

@jasonlewis yes! However, the use of multi-byte encoding characters such as China or Japan, it is not convenient.

@yinmingming
Copy link

good job

@taylorotwell
Copy link
Member

I would suggest just extend the Validator to do what you want. You can easily do so using the Validate::resolver method.

@overtrue
Copy link
Contributor Author

ok,Thanks

@taylorotwell taylorotwell reopened this Apr 14, 2014
@taylorotwell
Copy link
Member

Want to re-open this to consider it further.

@overtrue
Copy link
Contributor Author

@taylorotwell Thanks, in fact, in a non-English speaking countries is still a lot of demand.

@taylorotwell taylorotwell merged commit c06e8b7 into laravel:master May 2, 2014
@overtrue
Copy link
Contributor Author

overtrue commented May 2, 2014

@taylorotwell Thanks!

@jinchun
Copy link

jinchun commented May 3, 2014

Admiration arises despite my incomprehension of what you said!

*/
public function getDisplayableValue($attribute, $value)
{
if (isset($this->customValues[$attribute][$value]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no method to set $customValues like there is to set $customAttributes (unless one extends Validator class):

Custom attributes may be set either in constructor or using setAttributeNames(array $attributes).
I think this cool functionality deserves similar API.

To explain: I'm not using the default key validation.values because my models attributes overlap.

@yopop666
Copy link

May anyone help me to translate value from required_if validation rule?
More details in issue 19725

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.