Support the ongoing development of Laravel.io →
posted 9 years ago
Validation
Last updated 1 year ago.
0

My original questions was actually 2 questions, so here are the 2 answers:

To work with custom messages when using the new L5 FormRequest dependency method of doing validation you can include the following override in your SignUpRequest.php file.

	public function messages() {
		return [
			'required' => ucwords(':attribute') . ' is required.'
		];
	}

OR

You can edit the custom messages are of the lang/xx/validation.php file.

	/*
	|--------------------------------------------------------------------------
	| Custom Validation Language Lines
	|--------------------------------------------------------------------------
	|
	| Here you may specify custom validation messages for attributes using the
	| convention "attribute.rule" to name the lines. This makes it quick to
	| specify a specific custom language line for a given attribute rule.
	|
	*/

	'custom' => [
		'attribute-name' => [
			'rule-name' => 'custom-message',
		],
	],

The only issue, is that the neither of these custom message areas allowed me to wrap :attribute in ucwords() (which was my second question.). So, thanks to a tweet from Taylor, what I did was just use the custom attributes section of the /lang/xx/validation.php file.

	/*
	|--------------------------------------------------------------------------
	| Custom Validation Attributes
	|--------------------------------------------------------------------------
	|
	| The following language lines are used to swap attribute place-holders
	| with something more reader friendly such as E-Mail Address instead
	| of "email". This simply helps us make messages a little cleaner.
	|
	*/

	'attributes' => [
		'first_name' => 'First Name'
	],
Last updated 9 years ago.
0

Hi chrislentz,

Are you sure

	public function messages() {
		return [
			'required' => ucwords(':attribute') . ' is required.'
		];
	}

is working? I tried, there is not working. and I replace ucwords to strtoupper, it give me The :ATTRIBUTE field is required.

I am using Laravel 5.1 LTS.

Anyone know how to set ucwords for all the attribute, instead I need to set the attribute one by one.

I did some research on the framework, I notice that in in 5.2 https://github.com/laravel/framework/pull/11337/files

it has something to do with replace word.

Is it possible done in 5.1? any way to overwrite the function? \vendor\laravel\framework\src\Illuminate\Validation\Validator.php Line 1698

  protected function doReplacements($message, $attribute, $rule, $parameters)
    {
        $message = str_replace(':attribute', $this->getAttribute($attribute), $message);

        if (isset($this->replacers[Str::snake($rule)])) {
            $message = $this->callReplacer($message, $attribute, Str::snake($rule), $parameters);
        } elseif (method_exists($this, $replacer = "replace{$rule}")) {
            $message = $this->$replacer($message, $attribute, $rule, $parameters);
        }

        return $message;
    }

Thanks!

chrislentz said:

My original questions was actually 2 questions, so here are the 2 answers:

To work with custom messages when using the new L5 FormRequest dependency method of doing validation you can include the following override in your SignUpRequest.php file.

  public function messages() {
  	return [
  		'required' => ucwords(':attribute') . ' is required.'
  	];
  }

OR

You can edit the custom messages are of the lang/xx/validation.php file.

  /*
  |--------------------------------------------------------------------------
  | Custom Validation Language Lines
  |--------------------------------------------------------------------------
  |
  | Here you may specify custom validation messages for attributes using the
  | convention "attribute.rule" to name the lines. This makes it quick to
  | specify a specific custom language line for a given attribute rule.
  |
  */

  'custom' => [
  	'attribute-name' => [
  		'rule-name' => 'custom-message',
  	],
  ],

The only issue, is that the neither of these custom message areas allowed me to wrap :attribute in ucwords() (which was my second question.). So, thanks to a tweet from Taylor, what I did was just use the custom attributes section of the /lang/xx/validation.php file.

  /*
  |--------------------------------------------------------------------------
  | Custom Validation Attributes
  |--------------------------------------------------------------------------
  |
  | The following language lines are used to swap attribute place-holders
  | with something more reader friendly such as E-Mail Address instead
  | of "email". This simply helps us make messages a little cleaner.
  |
  */

  'attributes' => [
  	'first_name' => 'First Name'
  ],
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

chrislentz chrislentz Joined 19 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.