dotxxh0998 2014-02-15 13:08
浏览 26

Laravel 4:唯一(数据库)未验证

I am creating a basic CMS to teach myself the fundamentals of Laravel and PHP.

I have a 'pages' table and I am storing a url_title. I want this URL title to be unique for obvious reasons. However, whatever I do to validate it, fails. It just saves anyway. I'm sure it is something simple. Can you spot what is wrong with this code?

I am also using Former in the view, that doesn't validate either. I have tried hard-coding a value as the last option in the unique method and it fails also.
http://anahkiasen.github.io/former/

http://laravel.com/docs/validation#rule-unique
States: unique:table,column,except,idColumn

Here is my Controller:

public function store()
{
    $validation = Pages::validate(Input::all());

    if($validation->fails()) {
      Former::withErrors($validation);
      return View::make('myview');
    } else {
        Pages::create(array(
            'title'             => Input::get('title'),
            'url_title'         => Input::get('url_title'),
            'status'            => Input::get('status'),
            'body'              => Input::get('body'),
            'seo_title'         => Input::get('seo_title'),
            'seo_description'   => Input::get('seo_description')
        ));

        //check which submit was clicked on
        if(Input::get('save')) {
            return Redirect::route('admin_pages')->with('message', 'Woo-hoo! page was created successfully!')->with('message_status', 'success');
        }
        elseif(Input::get('continue')) {
            $id = $page->id;
            return Redirect::route('admin_pages_edit', $id)->with('message', 'Woo-hoo! page was created successfully!')->with('message_status', 'success');
        }
    }

}

Here is my model:

class Pages extends Eloquent {

   protected $guarded = array('id');

   public static $rules = array(
        'id'            => 'unique:pages,url_title,{{$id}}'
   );

   public static function validate($data) {
       return Validator::make($data, static::$rules);
   }
}

I have tried the following:

 public static $rules = array(
    //      'id'=> 'unique:pages,url_title,{{$id}}'
    //      'id'=> 'unique:pages,url_title,$id'
    //      'id'=> 'unique:pages,url_title,:id'
    //      'id'=> 'unique:pages,url_title,'. {{$id}}
    //      'id'=> 'unique:pages,url_title,'. $id
 );

Any ideas? I spoke to the guy who created Former. He can't make head nor tail about it either. He suggested tracking it back to find our what query Laravel uses to check the uniqueness and try running that directly in my DB to see what happens. I can't find the query to do this. Does anyone know where to track it down?

Many thanks

  • 写回答

2条回答 默认 最新

  • dtqie02844 2014-02-15 19:29
    关注

    Your rule should be:

    public static $rules = array(
        'url_title' => 'unique:pages,url_title,{{$id}}'
    );
    

    As I guessed from your code Input::get('url_title') You have to use the field name used in the form.

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装