1

I have an Ardent (Laravel 4) model with validation rules as below:

public static $rules = array(
    'title'      => 'required|alpha_num|min:4',
    'friendly'   => 'required|alpha_num|url'
);

When I try this:

$page = Page::find($id);
$page->menu=1;
$page->save();

It fails, because of the validation rules of other fields. My question is how can I update only one field as above?

Laurence
  • 58,936
  • 21
  • 171
  • 212
Yunus Is
  • 11
  • 3

2 Answers2

1

Force save without validation in Laravel

If you want to force save your model without validation, simply use the forceSave() method instead of save().

$model->forceSave()
Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
  • This seems more like a workaround though? We should be able to validate a single field, the one being updated, while ignoring ones that aren't affected? – PeterG Dec 19 '14 at 18:46
0

Adding "sometimes" worked in my case. Equivalent code for you would be:

public static $rules = array(
    'title'      => 'sometimes|required|alpha_num|min:4',
    'friendly'   => 'sometimes|required|alpha_num|url'
);

See: http://laravel.com/docs/4.2/validation#conditionally-adding-rules

PeterG
  • 772
  • 2
  • 11
  • 28