How to customise error message to add ID in Yii? - php

Currently I am using $form->error($user_model,'password') this function to show error message which gives me output as <div class="errorMessage">Please enter current password</div> but I want to add id in same div. What changes I have to do for that?

To customize error message you have to go to user model and change/add your message to function rules() array.
function rules() {
return [
['password', 'required', 'message' => 'Your custom message'],
];
}
Check CRequiredValidator class for more information.
To apply some custom ID:
$form->error($error_model, 'password', ['id' => 'My-super-custom-id']);
just it will break your error messages output when using yii-js methods.

Related

Creating a custom validation error message in CodeIgniter4

How can i create a custom error message for a custom validation. I'm using codeIgniter4
Okay guys so I'm a bit of a newbie with CI4 and I have created a custom validation file using the spark command ./spark make:validation and it works but the problem is I still don't know how to customize the error message too for instance when I try to validate the date 05-06-2022 the message is Validation.isWeekday, I want to let it say something meaningful like date is not a weekday.
This is how my validation looks like
namespace App\Validation;
class CustomDateValidation
{
public function isWeekday(string $date): bool
{
return date("N", strtotime($date)) < 6;
}
}
And my controller function looks a bit like this
if($this-validate(['date'=>'required|isWeekday'])){
...
}
You can pass a options array for each field you want validate instead of just the rules string:
if($this-validate([
'date'=> [
'rules' => 'required|isWeekday',
'errors' => [
'required' => 'The date field is required',
'isWeekday' => 'The date must be a weekday'
],
])){
...
}

Yii2 ActiveForm encodeErrorSummary property... what is it intended for?

I was trying to use the Yii2 ActiveForm encodeErrorSummary property because I wanted to put line-breaks on Yii2 validation error messages:
Example code snippet in MODEL file
public function rules()
{
return [['username', 'required', 'message' => 'long message first line here<br> long message last line here']];
}
Example code snippet in VIEW file
$form = ActiveForm::begin(['id' => 'myform',
'encodeErrorSummary' => false
]);
...
echo $form->field($model, 'username');
...
ActiveForm::end();
Official Yii2 Documentation describes encodeErrorSummary property as:
Whether to perform encoding on the error summary.
but it seemed not suitable for that in my case... Maybe it's me misunderstanding something (...error summary)?
So... what is it intended for, then?
Thank you!
It seems like you need to configure the $fieldConfig property like this:
ActiveForm::begin([
'fieldConfig' => [
'errorOptions' => ['encode' => false],
],
]);
for your requirement. The errorSummary is the summary that you echo with
<?= $form->errorSummary($model) ?>
before or after the form. What you want is a behavior at the field level, while this is an option to disable the encoding at the summary level.

Laravel 5 Form Request manually specify field name to be displayed

In Laravel's form request, how can I manually assign the field name to be displayed on error?
This is my blade.php file
<input type="text" name="txtConfigKey" id="txtConfigKey" class="form-control" placeholder="Config Key" />
Then on my request file:
public function rules()
{
return [
'txtConfigKey' => 'required'
];
}
Then the output is: "The txt config key field is required."
Notice that it converted the "txtConfigKey" with spaces. Is there a way for me to manually specify what would the field be?
In CodeIgniter, I can do something like this:
$this->form_validation->set_rules('txtConfigKey', 'Config Key', 'required');
Wherein the first parameter is the name of the field and the second parameter is the name of the field that I want to be displayed on the error message.
If you are using form request then there is a method called messages().
You have to override it with your custom error messages like:
public function messages()
{
return [
'txtConfigKey.required'=>'The Config Key is required'
];
}

Laravel 4 custom validation error message

so I was trying to add a custom validation rules like this:
public function validateDateAfterField($attribute, $value, $params)
{
return strtotime($value) > strtotime($this->data[$params[0]]);
}
It takes the value and validate with another field in array of data being validate
I wanted it to produce an error message like this:
Event End Date must be a date after Event Start Date
Here is my custom validation message:
":attribute must be a date after :before"
I've added these custom validation attributes:
'attributes' => array(
'event_start'=>'Event Start Date',
'event_end' => 'Event End Date',
)
But when I cannot access it in my custom validation class, I've tried something like this:
return str_ireplace(":before", $this->attributes[$parameters[0]], $message);
but got an error Undefined property:CustomerValidator::attributes
Anyone knows how to do this properly?
Edit:
Extended it in my global.php
Validator::extend('date_after_field', 'CustomValidator#DateAfterField');
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
I've found the answer after reading the documentation of the validator class.
By using $this->getAttribute($parameters[0]), I manage to get the custom attribute name.

Changing error 'cannot be blank' in Yii

Regarding handling error message in Yii: Commonly, Yii has configured form's validation, and when one of the required field isn't filled, then the error message will be shown as
"Blablabla cannot be blank."
How can I customize that error message? For example, I'd like to change into this:
"Blablabla tidak boleh kosong."
Its not clear what you are trying to do exactly, but
assuming you want to make a completely other message use :
public function rules()
{
return array(
array('title, content', 'required',
'message'=>'Please enter a value for {attribute}.'),
// ... other rules
);
}
if you, on the other hand, are looking for translations, the best way to do it is by setting your language in the config
'language'=>'de',
'components'=>array(
'coreMessages'=>array(
'basePath'=>null,
),
......
),
if your language is not defined, copy framework/messages/en/yii.php to protected/messages/{yourlanguage}/yii.php and start translating, even if you wonna add messages, put the them in protected/messages/{yourlanguage}/ and never translate them in the framwork, so you can update without a fuss.
I hope this is what you look for:
url: Yii docs
class Post extends CActiveRecord
{
public function rules()
{
return array(
array('title, content', 'required',
'message'=>'Please enter a value for {attribute}.'),
// ... other rules
);
}
}

Categories