I have a callback within kohana validation. I'm sending error message this way:
public function way(Validate $array, $field)
{
if (something)
{
$array->error($field, 'super error message', array($array[$field]));
}
}
It works but when I print out the message
echo $errors['field'])
it returns formName.field super error message
How to get rid of formName.field?
These are messages configured in Kohana Core or in Modules or Application.
You can change them in messages folder
e.g. default validation message are in System -> messages -> validation.php
you an copy this file in your application and remove :field from them it will get rid of field name.
'not_empty' => ':field must not be empty',
change it to
'not_empty' => 'must not be empty',
Related
I'm working on a form_validation. I have set the error messages, but it only shows the first error I've set. Other than that, it shows the default message.
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[user.email]',
array(
'required'=>'Empty email',
'is_unique[user.email]'=>'Email has been registered')
);
If I don't fill the email form, it shows 'Empty email'. If I write the email the same as in database, it shows the default error of CI : 'The Email field must contain a unique value.'
How I can show 'Email has been registered'?
Try:
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[user.email]',
array(
'required'=>'Empty email',
'is_unique'=>'Email has been registered')
);
DOCS: https://www.codeigniter.com/userguide3/libraries/form_validation.html#cascading-rules
I suspect you aren't getting the right error message because you shouldn't repeat user.email for the errors array.
In Laravel you can make a custom messages for validators. But I found that the prepared messages are little bit wrong. With before and after validation rules, the parameter is converted with strtotime like that said in the documentation.
So if I set rule 'expires' => 'before:+1 year' the rule is working and correct. But if the user inputs a wrong value, Laravel prints the message like that:
The expires must be a date before +1 year
This message is very unclear for the average client. I was expected that the message will be converted with the strtotime also.
There is a clean way to print more clear error message?
You can override the validation messages with a custom ones if you want to.
In the Form Request class, add the following method and change the messages like so:
public function messages()
{
return [
// 'fieldname.rulename' => 'Custom message goes here.'
'email.required' => 'Er, you forgot your email address!',
'email.unique' => 'Email already taken m8',
];
}
Update:
If you want to change the global messages, you can open the validation.php file inside resources/lang/en/validation.php and edit the message there.
You can user AppServiceProvider.php
and you can write your new validation rule there and set your error message in validation.php file
/app/Providers/AppServiceProvider.php
/resources/lang/en/validation.php
Ex:
Validator::extend('before_equal', function ($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
and the message is
'before_equal' => 'The :attribute field is only accept date format before or equal to end date',
Please try this and you can alter this to based on your require.
How to solve above issue? I want to keep test even after a validation failure in the submission. But after session error message is passed all the entered data will be gone.
To re-fill the form with the input data again, check the input validation then if validation fails, redirect the user back along with his input data and validation errors.
$validator = Validator::make($request->all(), [
// your validation rules.
'name' => 'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
// Continue your app logic.
You'll find more information in Laravel validation docs
You should send back the input also to the view.
Like Sameh Salama already mentioned, use the following code:
function () {
return redirect()->back()->withErrors($validator)->withInput();
}
Notice the withInput() function, it returns the old input.
Use it in the View as
<input type="something" value="{{$input->first_something}}" />
I am making a REST api for my application. It almost works, but I want it to return the validation errors when they occurs.
With CakePHP 2.x I see there was an invalidFields method, but it's not there anymore with Cake3. I see, instead, that there is an errors method in the Form class, but I don't know how can I use it.
This is what I have in my Users/json/add.ctp file:
<?php
$echo = [
'errors' => $this->Form->errors(),
'user' => $user
];
echo json_encode($echo);
As I said it doesn't work (I know that probably Form is used out of its scope here, but I don't even know if I am on the right path)...
This is the result:
{
"message": "Missing field name for FormHelper::errors",
"url": "\/fatturazione\/api\/users.json",
"code": 500
}
What is the correct way to display all the form errors when they occour?
This is what I would do, check for validation-errors in your controller and send them to the view serialized
public function add() {
$this->request->allowMethod('post');
$data = $this->Model->newEntity($this->request->data);
if($data->errors()) {
$this->set([
'errors' => $data->errors(),
'_serialize' => ['errors']]);
return;
}
//if no validation errors then save here
}
I have a list of User records that I'm saving at one time (the form is populated from an uploaded CSV).
Everything seems to be working fine, and my validation rules are being honoured, but one thing that's eluding me are the specific validation error messages.
I get the general validation error (if say, a field that is supposed to be unique is not), but I'm not getting an error on the specific field to let the user know what the actual problem is.
Is this a bug, or is there something wrong with my Code? Here's the action from my Controller (fwiw I'm passing the CSV data to this action from another action, hence the snippet at the beginning):
public function finalizeCsv() {
if ( isset($this->request->params['named']['csvData']) ) {
$csvData = unserialize( $this->request->params['named']['csvData'] );
} else {
$csvData = $this->request->data;
}
$this->set('users', $csvData);
if ($this->request->is('get')) {
$this->request->data = $csvData;
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->saveAll($this->request->data)) {
$this->Session->setFlash('Users added!', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('There were errors with your data.');
}
}
}
For unique fields add a validation rule
'field_name' => array(
'rule' => 'isUnique',
'required' => 'create'
),
Next - when you get a error you will need to review $this->User->validationErrors. There will be many arrays inside - 1 for each created record. Loop through them, collect errors and send them to $this->Session->setFlash().
Also you better wrap $this->User->saveAll($this->request->data) into transaction if you're using InnoDB or other engine that supports transactions. On error - do rollback and try again.
$ds = $this->User->getDataSource();
$ds->begin();
// > saving here < //
$ds->commit(); //< on success
$ds->rollback(); //< on error